2014-02-10 3 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); 
        */ 
       } 
      }); 
     } 

내가하고 싶은 것은 연결된 전화 번호 (기본 전화 번호)와 함께 연락처의 이름을 출력하는 것입니다. 그래서 나는 SMS 문자 메시지가 전화 번호와 내 연락처의 이름을 기반으로 미리 정의 된 새로운 의도를 만들 수 있습니다.

아무에게도 어떻게 할 수 있습니까? 감사합니다

답변

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 array]를 넣는 대신에 이것들을 넣으십시오.

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

는 그런 다음 세터 - 게터 클래스의 ArrayList를 확인하고 데이터로 충전 후 ArrayList의에 모든 세터 - 게터 클래스를 추가합니다.

이 ArrayList를 Adapter 클래스에 전달하십시오.

와의

당신의 OnItemClickListener 휴대폰이 같은 & 이름 가질 수 있습니다

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

을 그리고 당신은 행해진 다. 이해하기 힘들지 만 언급 한 StepByStep 구현을 시도하면 목표를 달성하게됩니다.

+0

안녕하세요, 제안한 솔루션이 내 것과 유사하게 작동합니다. 내가 뭘하려고하는 대신 개체로 연락을 받고, 나는 그 개체의 이름 (문자열)과 전화 번호 (문자열)를 얻고 싶습니다. – Jeremy

+0

업데이트 된 답변보기 –