0

I want to read the sms from inbox and display them in a list view with few sms content providers like id,sender number,body,time. Rite now Im able to read the sms and display in list view only the body of the sms but in need to display other field too. I am using Android Studio for app development.
Here is a code which i can displays the body of sms in listview

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, messageArray);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

public ArrayList<String> readAllData() {
ArrayList<String> sms = new ArrayList<>();
Uri us = Uri.parse("content://sms/inbox");
cr = getContentResolver().query(us, null, null, null, null);
String body = cr.getString(cr.getColumnIndexOrThrow("body"));
sms.add(body);
return sms;
}

I wanted to display Sms id, sender name and time. Please can anybody could help me out...

Prathik
  • 77
  • 1
  • 1
  • 7

2 Answers2

0

To show multiple information in the listView. For beginner, try to learn with CustomAdapter for the list view, see following article:

Custom Adapter for List View

When you understand it deeply, try to work with "new version" of list view is RecyclerView: http://www.androidhive.info/2016/01/android-working-with-recycler-view/

You can also easily find many tutorial on Youtube with keyword "custom listview in android"

I think this helpful for you!

Community
  • 1
  • 1
xxx
  • 3,315
  • 5
  • 21
  • 40
0

First, you need to know, which columns you have regarding an SMS. Vikas Paditar listed them in this post (marked as best answer): https://stackoverflow.com/a/4023737/3953199

Next, you need to use the column names in the getColumnIndexOrThrow(String column)-method, like it was done in this post (also best answer) by qiuping345: https://stackoverflow.com/a/5570463/3953199

Here is a snippet from the second link:

            SmsRep singleSms = new SmsRep();
            singleSms.id = cursor.getInt(cursor.getColumnIndexOrThrow("_id"));
            singleSms.address = cursor.getString(cursor.getColumnIndexOrThrow("address"));
            singleSms.timestamp = cursor.getLong(cursor.getColumnIndexOrThrow("date")) / 1000;   //### the sent time
            singleSms.type = cursor.getInt(cursor.getColumnIndexOrThrow("type"));
            singleSms.protocol = cursor.getInt(cursor.getColumnIndexOrThrow("protocol"));

Basically you have different columns regarding an SMS, which offers a variety of information.

Community
  • 1
  • 1
KJaeg
  • 698
  • 3
  • 7
  • 23
  • I think his problem is to create a custom listview :) – xxx Jun 24 '16 at 08:32
  • I think I have to disagree here @HuyN. He wrote " but in need to display other field too", and "I wanted to display Sms id, sender name and time." So it looks to me that he really wants further SMS-information. – KJaeg Jun 24 '16 at 08:36
  • actually i need to display all those fields in same column according to each sms. – Prathik Jun 24 '16 at 09:49