我想讀取對話並將其放入ListView中。我該怎麼做?
我有兩個TextViews每行。我想通過使用BaseAdapter來設置它們。如何在Android中讀取SMS對話
通過使用此, 內容://彩信,短信/通話/
* 更新: *
我試着這樣做:
public ArrayList<String> number = new ArrayList<String>();
public ArrayList<String> body = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(new BaseAdapter(){
public int getCount() {
return number.size();
}
public Object getItem(int position) {
return number.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.contact_row, null);
TextView textView = (TextView) view.findViewById(R.id.textView1);
textView.setText(number.get(position));
return view;
}});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void getcon()
{
Uri uriSMSURI = Uri.parse("content://mms-sms/conversations/");
Cursor cursor = getContentResolver().query(uriSMSURI, null, null, null, "date desc");
cursor.moveToFirst();
while (cursor.moveToNext())
{
String address = cursor.getString(cursor.getColumnIndex("address"));
//String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
//String read = cursor.getString(cursor.getColumnIndexOrThrow("read"));
String contactName = address;
Uri Nameuri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(address));
Cursor cs= getContentResolver().query(Nameuri, new String[]{PhoneLookup.DISPLAY_NAME},PhoneLookup.NUMBER+"='"+address+"'",null,null);
if(cs.getCount()>0)
{
cs.moveToFirst();
contactName = cs.getString(cs.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
number.add(contactName);
}
}
在顯示列表中的任何問題..? –
@BlackTiger表示 – dave
顯示記錄中是否有任何錯誤。 –