Android 开发即时聊天工具 YQ :(六) 最近会话列表
|
admin
2013年2月25日 14:26
本文热度 3916
|
实现的效果如图:
RecentActivity:
- "FONT-SIZE: 14px">public class RecentActivity extends Activity{
- ListView listView;
- List chatEntityList=new ArrayList();
- String[] mes;
-
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_recent);
-
- IntentFilter myIntentFilter = new IntentFilter();
- myIntentFilter.addAction("org.yhn.yq.mes");
- registerReceiver(new MyBroadcastReceiver(), myIntentFilter);
-
- listView = (ListView) findViewById(R.id.lv_recent);
- listView.setOnItemClickListener(new OnItemClickListener(){
- public void onItemClick(AdapterView> arg0, View arg1, int position,
- long arg3) {
-
- Intent intent=new Intent(RecentActivity.this,ChatActivity.class);
- intent.putExtra("account", Integer.parseInt(mes[0]));
- intent.putExtra("nick", "");
- startActivity(intent);
- }
- });
- }
-
-
- public class MyBroadcastReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- mes = intent.getStringArrayExtra("message");
- Toast.makeText(context, "收到["+Integer.parseInt(mes[0])+"]的消息:"+mes[1], Toast.LENGTH_SHORT).show();
-
- Iterator it=chatEntityList.iterator();
- if(chatEntityList!=null && chatEntityList.size()!=0){
- while(it.hasNext()){
- RecentEntity re=(RecentEntity) it.next();
- if(re.getAccount()==Integer.parseInt(mes[0])){
- chatEntityList.remove(re);
- }
- }
- }
- chatEntityList.add(new RecentEntity(5, Integer.parseInt(mes[0]), mes[0]+"", mes[1],mes[2], false));
- listView.setAdapter(new RecentAdapter(RecentActivity.this, chatEntityList));
- unregisterReceiver(this);
- }
- }
- }
-
public class RecentActivity extends Activity{
ListView listView;
List chatEntityList=new ArrayList();
String[] mes;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_recent);
//注册广播
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction("org.yhn.yq.mes");
registerReceiver(new MyBroadcastReceiver(), myIntentFilter);
listView = (ListView) findViewById(R.id.lv_recent);
listView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView> arg0, View arg1, int position,
long arg3) {
//打开聊天页面
Intent intent=new Intent(RecentActivity.this,ChatActivity.class);
intent.putExtra("account", Integer.parseInt(mes[0]));
intent.putExtra("nick", "");
startActivity(intent);
}
});
}
//广播接收器
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
mes = intent.getStringArrayExtra("message");
Toast.makeText(context, "收到["+Integer.parseInt(mes[0])+"]的消息:"+mes[1], Toast.LENGTH_SHORT).show();
//更新最近会话列表, 检测chatEntityList,防止同一个好友的消息出现多个会话实体
Iterator it=chatEntityList.iterator();
if(chatEntityList!=null && chatEntityList.size()!=0){
while(it.hasNext()){
RecentEntity re=(RecentEntity) it.next();
if(re.getAccount()==Integer.parseInt(mes[0])){
chatEntityList.remove(re);
}
}
}
chatEntityList.add(new RecentEntity(5, Integer.parseInt(mes[0]), mes[0]+"", mes[1],mes[2], false));
listView.setAdapter(new RecentAdapter(RecentActivity.this, chatEntityList));
unregisterReceiver(this);
}
}
}
RecentActivity中listview的Adapter:
- "FONT-SIZE: 14px">public class RecentAdapter extends BaseAdapter{
- private Context context;
- private List list;
- LayoutInflater inflater;
-
- public RecentAdapter(Context context,List list){
- this.context = context;
- this.list = list;
- inflater = LayoutInflater.from(context);
- }
-
- public View getView(int position, View convertView, ViewGroup root) {
- convertView = inflater.inflate(R.layout.recent_listview_item, null);
-
- ImageView avatar=(ImageView) convertView.findViewById(R.id.iv_avatar_r);
- TextView nick=(TextView) convertView.findViewById(R.id.tv_nick_r);
- TextView content=(TextView) convertView.findViewById(R.id.tv_chat_content_r);
- ImageView isRead=(ImageView) convertView.findViewById(R.id.iv_tip_mes_r);
- TextView time=(TextView) convertView.findViewById(R.id.tv_time_r);
-
- RecentEntity re=list.get(position);
- nick.setText(re.getNick());
- content.setText(re.getContent());
- isRead.setImageResource(R.drawable.tips_message);
- time.setText(re.getTime());
-
- return convertView;
- }
- public int getCount() {
- return list.size();
- }
-
- public Object getItem(int position) {
- return list.get(position);
- }
-
- public long getItemId(int position) {
- return position;
- }
- }
-
public class RecentAdapter extends BaseAdapter{
private Context context;
private List list;
LayoutInflater inflater;
public RecentAdapter(Context context,List list){
this.context = context;
this.list = list;
inflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup root) {
convertView = inflater.inflate(R.layout.recent_listview_item, null);
ImageView avatar=(ImageView) convertView.findViewById(R.id.iv_avatar_r);
TextView nick=(TextView) convertView.findViewById(R.id.tv_nick_r);
TextView content=(TextView) convertView.findViewById(R.id.tv_chat_content_r);
ImageView isRead=(ImageView) convertView.findViewById(R.id.iv_tip_mes_r);
TextView time=(TextView) convertView.findViewById(R.id.tv_time_r);
RecentEntity re=list.get(position);
nick.setText(re.getNick());
content.setText(re.getContent());
isRead.setImageResource(R.drawable.tips_message);
time.setText(re.getTime());
return convertView;
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return list.get(position);
}
public long getItemId(int position) {
return position;
}
}
消息实体类,就不贴代码了,很简单,你懂的
--------------------------------------------------分割线----------------------------------------------------------
完善了部分界面,以及一些细节,
后台运行,
增加了用户头像(简易方式,暂不能自定义头像),
用户个人信息
。。。
具体就不说了看图吧:
该文章在 2013/2/25 14:26:16 编辑过