2012-03-10 4 views
0

Définition:Comment le look xml pour SimpleAdapter

peopleList = new ArrayList<Map<String, String>>(); 
PopulatePeopleList(); 


mAdapter = new SimpleAdapter(this, peopleList, R.layout.row ,new String[] { "Name", "Phone" }, new int[] { R.id.text1, R.id.text2 }); 

txtPhoneName.setAdapter(mAdapter); 

Quelle devrait être la mise en page de document XML peut ressembler pour que cela fonctionne correctement:

Voici le mien:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 
<TextView 
android:id="@+id/text1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:padding="10dip" ></TextView> 

<TextView 
android:id="@+id/text2" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
android:padding="10dip" > 

</TextView> 
</LinearLayout> 

ici est ma classe qui remplit le champ:

public void PopulatePeopleList() { 

peopleList.clear(); 

Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 

while (people.moveToNext()) 
{ 
String contactName = people.getString(people.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID)); 
String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

if ((Integer.parseInt(hasPhone) > 0)) 
{ 

// You know have the number so now query it like this 
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); 
while (phones.moveToNext()) { 

//store numbers and display a dialog letting the user select which. 
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

    Map<String, String> mapa = new HashMap<String, String>(); 

mapa.put("Name",contactName); 
    mapa.put("Phone", phoneNumber); 

    //Then add this map to the list. 
peopleList.add(mapa); 
} 
phones.close(); 
} 
} 
people.close(); 

startManagingCursor(people); 
} 

Répondre

3

Comme ceci:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 
<TextView 
android:id="@+id/text1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:padding="10dip" > 
<TextView 
android:id="@+id/text2" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:padding="10dip" > 
</Linearlayout 

Pour travailler, vous devez avoir dans votre mise en page de la ligne TextViews avec le Ids défini dans l'adaptateur.

+0

android.content.res.Resources $ NotFoundException: ID de ressource # 0x7f030001 – denza

+3

@denza essayez de nettoyer le projet. Menu 'Project-> Clean'. – Luksprog

+0

WOOW ça a vraiment fait l'affaire? qu'est-ce que clean a fait? – denza

Questions connexes