實(shí)現(xiàn)的效果如圖:
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);
//注冊(cè)廣播
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) {
//打開(kāi)聊天頁(yè)面
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();
//更新最近會(huì)話列表, 檢測(cè)chatEntityList,防止同一個(gè)好友的消息出現(xiàn)多個(gè)會(huì)話實(shí)體
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中l(wèi)istview的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;
}
}
消息實(shí)體類,就不貼代碼了,很簡(jiǎn)單,你懂的
--------------------------------------------------分割線----------------------------------------------------------
完善了部分界面,以及一些細(xì)節(jié),
后臺(tái)運(yùn)行,
增加了用戶頭像(簡(jiǎn)易方式,暫不能自定義頭像),
用戶個(gè)人信息
。。。
具體就不說(shuō)了看圖吧:
該文章在 2013/2/25 14:26:16 編輯過(guò)