2012-12-21 20 views
3

使用以下班級我想從聯繫人姓名和號碼列表中選擇聯繫人。使用ListAdapter使用simple_list_item_2在列表視圖中顯示聯繫人名稱和號碼,如何僅提取onItemClick()中的數字?

public class MakeCallView extends Activity implements OnItemClickListener { 

    Button callButtn; 
    EditText number2call; 
    private ListView lv1 = null; 
    Cursor cursor = null; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.makeacall); 

     cursor = getContentResolver().query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, 
       null, null); 
     callButtn = (Button) findViewById(R.id.CallBtn); 
     number2call = (EditText) findViewById(R.id.number2Call); 
     lv1 = (ListView) findViewById(R.id.listContact); 

     ListAdapter adapter1 = new SimpleCursorAdapter(this, 


      android.R.layout.simple_list_item_2, cursor, new String[] { 
         ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, 
         ContactsContract.CommonDataKinds.Phone.NUMBER }, 
       new int[] { android.R.id.text1, android.R.id.text2 }); 

     lv1.setAdapter(adapter1); 
     lv1.setOnItemClickListener(this); 

     callButtn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 


       // TODO Auto-generated method stub 

      } 

     }); 
    } 



    @Override 
    public void onItemClick(AdapterView<?> adapter, View arg1, int pos, 
      long arg3) { 

     try { 

      Cursor c = (Cursor) adapter.getItemAtPosition(pos); 

      View v = adapter.getFocusedChild(); 
      // TwoLineListItem row = (TwoLineListItem) 
      // adapter.getAdapter().getView(pos, arg1, 
      // (ViewGroup)adapter.getParent()); 
      // String a = (String)adapter.getItemAtPosition(pos); 
      // number2call.setText(c.g); 
      Log.v("GPS", "arg3 : " + c.getColumnCount()); 
      Log.v("GPS", "arg3 : " + c.getColumnNames()); 
      Log.v("GPS", "arg3 : " + c.toString()); 
      Log.v("GPS", "arg3 : " + c.getShort(1)); 

      Log.v("GPS", "arg3 : " + c.getColumnIndex("text2")); 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      Log.v("GPS", e.getMessage(), e); 
     } 

     // number2call.setText(((TextView)arg1).getText().toString()); 

     // Handle list2 click even 

    } 

} 

從上面的代碼,在onItemClick(),所選擇的聯繫人號碼應檢索哪我不能夠得到的。請幫忙。

回答

1

試試這個在您的OnItemClickListener

TextView txtNumber = (TextView) arg1.findViewById(android.R.id.text2) 
String str = txtNumber.getText() 
1
 
     This will give you back the Number of selected contact 
@Override 
     public void onItemClick(AdapterView<?> adapter, View arg1, int pos, 
       long arg3) { 

      try { 

       String x = ((TwoLineListItem) arg1).getText2().getText() 
          .toString(); 
        number2call.setText(x); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       Log.v("GPS", e.getMessage(), e); 
      } 


    } 
相關問題