我從Restfull
服務請求獲得XML
,並需要將其顯示在listView
中。ListView顯示父視圖,將不會採取正確的看法?
我決定仿效我在在線博客上找到的類似功能,它似乎反饋原始視圖,而不是我分配給元素的視圖格式。現在
你可以看到它的兩次重複.....我的移動名片夾欄是在ListView
開始,然後重複之後。
將XML爲ListView:
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/button6"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true" >
</ListView>
除了我使用適配器的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingLeft="5dp" >
<TextView
android:id="@+id/ContactTitle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"/>
<TextView
android:id="@+id/Name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<TextView
android:id="@+id/MainPhone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<TextView
android:id="@+id/CellPhone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<TextView
android:id="@+id/Fax"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
<TextView
android:id="@+id/Email"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" />
</LinearLayout>
我不是非常好,建設這些ListViews
然而,知道它的東西我做到了,但我不能把手指放在上面。下面是我對數據的處理,以listView
的Java:
protected void onPostExecute(String result){
String xml = result;
String KEY_ITEM = "Contact";
String KEY_CONTACTTITLE = "ContactTitle";
String KEY_NAME = "Name";
String KEY_MAINPHONE = "Phone";
String KEY_CELLPHONE ="Cell";
String KEY_FAX = "Fax";
String KEY_EMAIL= "Email";
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
Document doc = getDomElement(xml);
if (xml != null) {
{
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_CONTACTTITLE, getValue(e, KEY_CONTACTTITLE));
map.put(KEY_NAME, getValue(e, KEY_NAME));
map.put(KEY_MAINPHONE, getValue(e, KEY_MAINPHONE));
map.put(KEY_CELLPHONE, getValue(e, KEY_CELLPHONE));
map.put(KEY_FAX, getValue(e, KEY_FAX));
map.put(KEY_EMAIL, getValue(e, KEY_EMAIL));
menuItems.add(map);
}
ListAdapter adapter = new SimpleAdapter(MoveContactsActivity.this, menuItems,
R.layout.activity_move_contacts, new String[] {
KEY_CONTACTTITLE, KEY_NAME, KEY_MAINPHONE, KEY_CELLPHONE, KEY_FAX, KEY_EMAIL },
new int[] { R.id.ContactTitle, R.id.Name,
R.id.MainPhone, R.id.CellPhone, R.id.Fax, R.id.Email });
setListAdapter(adapter);
}
}
}
會喜歡它,如果有人可以幫助我走出這個問題還是讓我在正確的方向。再次感謝您的時間!
您確定從服務器獲得不同的行嗎? – sandrstar 2013-02-24 05:15:39
它是一個使用Adapter類來填充listview的最佳實踐..我可以試試這個.. – Subburaj 2013-02-24 06:18:38
sandrstar-是的,因爲它通過我的for循環,並使這兩個元素,他們只是不是我告訴它看起來像。如果這甚至是有道理的。 – Keeano 2013-02-26 01:41:34