2012-06-26 2 views
0

나는 listView에 콘택트를 가져오고 싶다. 나는 SocialAuth의 코드 하나를 너무 reutilitzate하고 싶다. 이 코드에서 LogCat에서 ContactList를 얻을 수 있지만 listView에서는 볼 수 없으며이를 수행하는 방법을 모른다. 이 listView는 다른 XML에 있습니다.ListView 안드로이드에 연락처보기

나는 더 goggling있어하지만 난

public void Events(String provider) { 

    setContentView(R.layout.contact_list); 

    List <Contact> contactsList = adapter.getContactList(); 




    if (contactsList != null && contactsList.size() > 0) { 
     for (Contact p: contactsList) { 

      if (TextUtils.isEmpty(p.getFirstName()) && TextUtils.isEmpty(p.getLastName())) { 
       p.setFirstName(p.getDisplayName()); 
      } 

      Log.d("Custom-UI", "Display Name = " + p.getDisplayName()); 
      String ContactNAme = p.getDisplayName(); 
      //ContactName = new String[] {p.getDisplayName()}; 
      //mapTo = new int[] {android.R.id.text1}; 
      Log.d("Custom-UI", "First Name = " + p.getFirstName()); 
      String ContactFisrtName = p.getFirstName(); 
      Log.d("Custom-UI", "Last Name = " + p.getLastName()); 
      String ContactLastName = p.getLastName(); 
      Log.d("Custom-UI", "Contact ID = " + p.getId()); 
      String ContactId = p.getId(); 
      Log.d("Custom-UI", "Profile URL = " + p.getProfileUrl()); 
      String ContactProfileUrl = p.getProfileUrl(); 

     } 
     // Log.d("ContactList",mAdapter.toString()); 
    } 
    Toast.makeText(CustomUI.this, "View Logcat for Contacts Information", Toast.LENGTH_SHORT).show(); 


} 

내가 문제가 커서이라고 생각 코드를 적용하는 방법을 모르는 나는 사람이 없기 때문에,이 기능이 그 I 그 커서가 좋아하는 작품이라고 생각하십시오.

public List<Contact> getContactList() 
{ 
    try 
    { 
     contactsList = new contactTask().execute().get(); 
    } 
    catch (InterruptedException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (ExecutionException e) 
    { 
     e.printStackTrace(); 
    } 

    return contactsList; 
} 

누군가 제발 저를 도울 수 있다면, 고마워요.

+0

포스트 목록보기 코드와 오류가 다음 –

+0

를 기록하면 내가 어떤 목록보기 코드가없는, 내가 contactsList 잡아 목록보기에 넣을 수 있습니다? – user1459089

답변

0

입니다. 여기서 원하는 결과를 얻을 수 있습니다.

+0

귀하의 인스 트루먼트에 따라 ContactMAnager.java를 utlitzate했지만 성공 결과가 없습니다. NullPointerExecption 오류가 있습니다. 목록 의 반품을 캐치 할 수 있습니까? 감사합니다. – user1459089

0

목록보기를 위해 사용자 지정 어댑터를 작성해야합니다.

여기에 아래의 링크를

http://developer.android.com/tools/samples/index.html

이 안드로이드 SDK의 샘플 예제를 데에서 참조하시기 바랍니다 좋은 예

http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-ii-%E2%80%93-custom-adapter-and-list-item-view/

+0

내 질문에 아직 해결되지 않았습니다. ListView에 List 을 사용할 수 있습니까? – user1459089

0
// Call contact thread 

    contact_thread = new Contact_thread(); 
     contact_thread.start(); 

private Cursor getContacts() { 
    // Run query 
    Uri uri = ContactsContract.Contacts.CONTENT_URI; 
    String[] projection = new String[] { ContactsContract.Contacts._ID, 
      ContactsContract.Contacts.DISPLAY_NAME }; 
    String[] selectionArgs = null; 
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME 
      + " COLLATE LOCALIZED ASC"; 

    return managedQuery(uri, projection, null, selectionArgs, sortOrder); 
} 

class Contact_thread extends Thread { 

    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     // Build adapter with contact entries 
     Cursor cursor = getContacts(); 

     cursor.moveToFirst(); 
     contactName = new String[cursor.getCount()]; 
     contactNo = new String[cursor.getCount()]; 
     checkedPosition = new boolean[cursor.getCount()]; 



     ContentResolver contect_resolver = getContentResolver(); 
     int i = 0; 
     if (cursor.getCount() > 0) { 
      do { 
       String id = cursor 
         .getString(cursor 
           .getColumnIndexOrThrow(ContactsContract.Contacts._ID)); 

       Cursor phoneCur = contect_resolver.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
           + " = ?", new String[] { id }, null); 
       if (phoneCur.moveToFirst()) { 
        contactName[i] = phoneCur 
          .getString(phoneCur 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
        contactNo[i] = phoneCur 
          .getString(phoneCur 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

        if (contactName[i] == null) { 
         contactName[i] = "Unknown"; 
        } 

       } else { 
        contactName[i] = "Unknown"; 
        contactNo[i] = ""; 
       } 

       db.AddContact(contactName[i], contactNo[i]); 

       i++; 
       phoneCur.close(); 
      } while (cursor.moveToNext()); 


     } 
     cursor.close(); 
     runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       // TODO Auto-generated method stub 
       // mContactList.setAdapter(cursorAdapter); 

       mContactList.setAdapter(new ContactAdapter(
         ContactManager.this, R.layout.contact_entry)); 


      } 
     }); 
    } 
} 

private class ContactAdapter extends ArrayAdapter<String> implements 
     Filterable { 

    public ContactAdapter(Context context, int textViewResourceId) { 
     super(context, textViewResourceId); 
     // TODO Auto-generated constructor stub 
    } 

    public int getCount() { 
     return contactName.length; 
    } 

    public String getItem(int position) { 
     return contactName[position]; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     View v; 
     if (convertView == null) { 
      LayoutInflater li = getLayoutInflater(); 
      v = li.inflate(R.layout.contact_entry, null); 
     } else { 
      v = convertView; 
     } 
     CheckedTextView text = (CheckedTextView) v.findViewById(R.id.text1); 
     text.setText(contactName[position] + " (" + contactNo[position] 
       + ") "); 
     text.setChecked(checkedPosition[position]); 
     return v; 
    } 

} 

contact_entry.xml

<?xml version="1.0" encoding="utf-8"?> 
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/text1" 
     android:layout_width="fill_parent" 
     android:layout_height="?android:attr/listPreferredItemHeight" 
     android:background="#AA0114" 
     android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
     android:gravity="center_vertical" 
     android:paddingLeft="6dip" 
     android:paddingRight="6dip" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textStyle="bold" />