2015-09-16 15 views
0

我正在構建一個收件箱中的應用程序,它可以從收件箱獲取所有郵件,並在我的應用程序的列表視圖中顯示它。我在列表視圖中難以獲取發件人名稱,它只顯示發件人的編號。請幫忙。從聯繫人中獲取發件人名稱以及郵件到達我的應用程序的時間和日期

公共類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; 
} 

// getView方法被調用的ListView的每個項目

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; 
} 

}

回答

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); 

瞭解插入,更新,更多的細節刪除,然後從ContactsContract.Data

查詢操作
相關問題