我正在构建一个收件箱类型的应用程序,它从收件箱中提取所有邮件,并在我的应用程序的列表视图中显示它。我在列表视图中获取发件人名称时遇到困难,它只显示发件人的号码。请帮忙。
公共类SMSListAdapter扩展了BaseAdapter {
private Context mContext;
Cursor cursor;
public SMSListAdapter(Context context,Cursor cur)
{
super();
mContext=context;
cursor=cur;
}
public int getCount()
{
// return the number of records in cursor
return cursor.getCount();
}
// getView method is called for each item of ListView
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//为ListView的每个项目调用getView方法
public View getView(int position, View view, ViewGroup parent) {
// inflate the layout for each item of listView
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.listview_each_item, null);
// move the cursor to required position
cursor.moveToPosition(position);
// contact name
// fetch the sender number and sms body from cursor
String senderNumber=cursor.getString(cursor.getColumnIndex("address"));
String smsBody=cursor.getString(cursor.getColumnIndex("body"));
//long timeMillis = cursor.getColumnIndex("date");
//Date date = new Date();
//SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
//String dateText = format.format(date);
//15022014
// String senderName=cursor.getString(cursor.getColumnIndex("name"));
// get the reference of textViews
TextView textViewConatctNumber=(TextView)view.findViewById(R.id.textViewSMSSender);
TextView textViewSMSBody=(TextView)view.findViewById(R.id.textViewMessageBody);
TextView textViewDate=(TextView)view.findViewById(R.id.textViewDate);
//15022014
// TextView textViewSMSName=(TextView)view.findViewById(R.id.textViewSMSName);
// Set the Sender number and smsBody to respective TextViews
textViewConatctNumber.setText(senderNumber);
textViewSMSBody.setText(smsBody);
textViewDate.setText(dateText);
//15022014
// textViewSMSName.setText(name);
return view;
}
}
答案 0 :(得分:3)
试试这个
// display name
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME))
查找给定联系人的给定类型的所有数据
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Data.CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
new String[] {String.valueOf(contactId)}, null);
了解有关插入,更新,删除和查询操作的更多详细信息