public void initData() { lastMsgs = msgDbHelper.getLastMsg(); adapter.clear(); adapter.addAll(lastMsgs); if (adapter.getCount() == 0) { listView.setVisibility(View.GONE); } else { listView.setVisibility(View.VISIBLE); } }
public void initView() { msgDbHelper = MsgDbHelper.getInstance(getActivity()); adapter = new MsgAdapter(getActivity()); listView.setAdapter(adapter); listView.setOnItemClickListener( new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ChatItem chatItem = lastMsgs.get(position); Intent intent = new Intent(); intent.setClass(getActivity(), ChatActivity.class); intent.putExtra("chatName", chatItem.chatName); intent.putExtra("chatType", chatItem.chatType); getActivity().startActivity(intent); } }); listView.setOnItemLongClickListener( new OnItemLongClickListener() { @Override public boolean onItemLongClick( AdapterView<?> parent, View view, final int position, long id) { new AlertDialog.Builder(getActivity()) .setTitle("提示") .setMessage("确认删除信息?删除后不可恢复?") .setPositiveButton( "是", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String username = adapter.getItem(position).chatName; NewMsgDbHelper.getInstance(getActivity()).delNewMsg(username); MyApplication.getInstance().sendBroadcast(new Intent("ChatNewMsg")); MsgDbHelper.getInstance(getActivity()).delChatMsg(username); adapter.notifyDataSetChanged(); } }) .setNegativeButton("否", null) .show(); return true; } }); // 接收到新消息的事件监听 newMsgReceiver = new NewMsgReceiver(); getActivity().registerReceiver(newMsgReceiver, new IntentFilter("ChatNewMsg")); searchText.addTextChangedListener( new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void afterTextChanged(Editable s) { if (s.toString().equals("")) { initData(); } else { lastMsgs = msgDbHelper.getLastMsg(s.toString()); adapter.clear(); adapter.addAll(lastMsgs); } } }); }