2014-02-10 46 views
0

嗨,我有以下代碼,它基本上填充手機的聯繫人列表並將其顯示在列表視圖中。從填充列表視圖中選擇電話簿的聯繫人

listView = (ListView) layout.findViewById(R.id.listView); 

     // GET PHONE BOOK'S CONTENT 
     cursor = getContentResolver().query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
       null, 
       null, 
       null, 
       null); 
     startManagingCursor(cursor); 

     String [] entries = { 
       ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
       ContactsContract.CommonDataKinds.Phone.NUMBER, 
       ContactsContract.CommonDataKinds.Phone._ID 
     }; 

     int [] temp = { 
       android.R.id.text1, 
       android.R.id.text2 
     }; 

     SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(mContext, 
       android.R.layout.simple_list_item_2, 
       cursor, entries, temp); 
     listView.setAdapter(listAdapter); 

     if(listView != null){ 
      listView.setOnItemClickListener(new OnItemClickListener(){ 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 
        // TODO Auto-generated method stub 

        Log.w("TAG", "SELECTING: " + listView.getAdapter().getItem(position).toString()); 

        /* 
        Intent i = new Intent(android.content.Intent.ACTION_VIEW, 
          Uri.fromParts("sms", listView.getAdapter().getItem(position).toString(), null)); 
        i.putExtra("sms_body", "Some message goes here."); 
        i.setType("vnd.android-dir/mms-sms"); 
        startActivity(i); 
        */ 
       } 
      }); 
     } 

我想要做的是輸出聯繫人的名稱以及關聯的電話號碼(主號碼)。因此,我可以創建一個新的意圖,根據電話號碼和我的聯繫人的姓名預先定義短信。

任何人都可以告訴我如何做到這一點?謝謝

回答

0

listView =(ListView)findViewById(R.id.listView1);

ArrayList<String> listOfName = new ArrayList<String>(); 
    ArrayList<String> listOfNumber = new ArrayList<String>(); 
    Cursor cursor = null; 
    try { 
     cursor = getApplicationContext().getContentResolver().query(
       Phone.CONTENT_URI, null, null, null, 
       Phone.DISPLAY_NAME + " ASC"); 
     int contactIdIdx = cursor.getColumnIndex(Phone._ID); 
     int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME); 
     int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER); 
     cursor.getColumnIndex(Phone.PHOTO_ID); 
     cursor.moveToFirst(); 
     do { 
      cursor.getString(contactIdIdx); 
      String name = cursor.getString(nameIdx); 
      String phoneNumber = cursor.getString(phoneNumberIdx); 
      listOfName.add(name); 
      listOfNumber.add(phoneNumber); 
     } while (cursor.moveToNext()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (cursor != null) { 
      cursor.close(); 
     } 
    } 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listOfName); 
    listView.setAdapter(adapter); 

    if (listView != null) { 
     listView.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       String selectedFromList =(String) (listView.getItemAtPosition(position)); 
       Toast.makeText(getApplicationContext(), selectedFromList, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
+0

謝謝,但我想你誤解了我的意思。我試圖輸出(即在Logcat上顯示)名稱和在列表視圖上點擊的項目的電話號碼。 – Jeremy

+0

請現在檢查代碼我希望此代碼可以幫助您 –

0

,你所要做的就是僅僅把這個線你(名單上的項目點擊lisetener):

Log.w("TAG", "SELECTING: " + parent.getItemAtPosition(position).toString()); 

INSTEAD OF

Log.w("TAG", "SELECTING: " + listView.getAdapter().getItem(position).toString()); 

       **Edited** 

要實現你所要求的可以按照以下方式完成:

做一個SetterGetter類並把這些東西放在那裏,而不是放入[String數組]。

String [] entries = { 
       ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
       ContactsContract.CommonDataKinds.Phone.NUMBER, 
       ContactsContract.CommonDataKinds.Phone._ID} 

然後作出這樣的二傳手拼命三郎類的ArrayList和在此ArrayList添加所有二傳手的getter類填充數據後他們。

將此ArrayList傳遞給適配器類。

而在你OnItemClickListener你可以有你的電話&名稱是這樣的:

Your_ArrayList.get(position).getName(); 
Your_ArrayList.get(position).getPhoneNumber(); 

和你做。 它很難理解,但是如果你嘗試使我提到的StepByStep實現,你會得到你的目標。

+0

嗨,您建議的解決方案與我的工作類似。我想要做的是,而不是作爲一個對象獲取聯繫人,我想獲得名稱(字符串)和該對象的電話號碼(字符串)。 – Jeremy

+0

查看我更新的答案。 –

相關問題