как XML должен выглядеть для SimpleAdapter - PullRequest
0 голосов
/ 10 марта 2012

Определение:

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);

Как должен выглядеть макет xml для правильной работы:

Вот мое:

  <?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>

Вотмой класс, который заполняет поле:

 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);
}

1 Ответ

3 голосов
/ 10 марта 2012

Как это:

  <?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

Для того, чтобы работать, вы должны иметь в своей структуре строк TextViews с Ids, установленным в адаптере.

...