Reputation: 77
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...
Upvotes: 0
Views: 511
Reputation: 686
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.
Upvotes: 0
Reputation: 3363
To show multiple information in the listView. For beginner, try to learn with CustomAdapter for the list view, see following article:
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!
Upvotes: 0