· 8 years ago · Aug 10, 2018, 01:42 AM
1How to merge all sms came from a sender into one in a ListView, like in the default messaging app?
2public class MessageList extends ListActivity {
3
4 protected void onCreate(Bundle savedInstanceState) {
5
6 super.onCreate(savedInstanceState);
7 setContentView(R.layout.main);
8
9 inboxcursor = getContentResolver().query(Uri.parse("content://sms/inbox"),new String [] {"person","address","body","date","_id","read", "status", "type", "reply_path_present", "subject","thread_id"} , null, null,"date DESC");
10 smsadapter = new MessageListAdapter(this,inboxcursor);
11 getListView().setAdapter(smsadapter);
12}
13
14public class MessageListAdapter extends CursorAdapter {
15
16 Context mcontext;
17 LayoutInflater inflater;
18
19 public MessageListAdapter(Context context, Cursor cursor) {
20 super(context, cursor, true);
21
22 inflater = LayoutInflater.from(context);
23 mcontext = context;
24
25 }
26
27 @Override
28 public void bindView(View view, Context context, Cursor cursor) {
29
30 String number = cursor.getString(cursor.getColumnIndex("address"));
31 TextView numbertext = (TextView) view.findViewById(R.id.number);
32 numbertext.setText(number);
33
34 String message = cursor.getString(cursor.getColumnIndex("body"));
35 TextView messagetext = (TextView) view.findViewById(R.id.message);
36 messagetext.setText(message);
37
38 }
39
40 @Override
41 public View newView(Context context, Cursor cursor, ViewGroup parent) {
42
43 View view = inflater.inflate(R.layout.listitems, null);
44
45 return view;
46 }
47
48 }
49
50cursor = getContentResolver().query(<sms table, assuming all are incoming. if not, you need to filter it on your where clause>, new String[] {sender column name}, null, null, null};
51
52while(cursor.moveToNext()) {
53 String sender = cursor.getString(cursor.getColumnName(<sender column name>));
54 // check if the sender string exists in your arraylist. if not, create a new entry and set count to 1. else, get the position of the existing number and increment count by one.
55};