2013-11-04 83 views
1

基於一個示例,我試圖創建一個顯示自定義ListItems的ListView。我在XML定義列表項:Android ListView:自定義ListItem顯示不正確

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:padding="6dip" > 

    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginRight="6dip" 
     android:contentDescription="TODO" 
     android:src="@drawable/icon" /> 

    <TextView 
     android:id="@+id/secondLine" 
     android:layout_width="fill_parent" 
     android:layout_height="26dip" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@id/icon" 
     android:ellipsize="marquee" 
     android:singleLine="true" 
     android:text="Description" 
     android:textSize="12sp" /> 

    <TextView 
     android:id="@+id/firstLine" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_above="@id/secondLine" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:layout_alignWithParentIfMissing="true" 
     android:layout_toRightOf="@id/icon" 
     android:gravity="center_vertical" 
     android:singleLine="true" 
     android:text="Example application" 
     android:textSize="16sp" /> 

</RelativeLayout> 

我創建了一個類來保存在一個列表項中顯示的數據:

public class UserRecord { 
    public String username; 
    public String email; 

    public UserRecord(String username, String email) { 
     this.username = username; 
     this.email = email; 
    } 
} 

我也有一個自定義的ArrayAdapter:

public class UserItemAdapter extends ArrayAdapter<UserRecord> { 
    private ArrayList<UserRecord> users; 
    public LayoutInflater vi; 
    private Context context; 

    public UserItemAdapter(Context context, int textViewResourceId, ArrayList<UserRecord> users) { 
     super(context, textViewResourceId, users); 
     this.users = users; 
     this.context = context; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if (v == null) { 
      LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = vi.inflate(R.layout.listitem, null); 
     } 

     UserRecord user = users.get(position); 
     if (user != null) { 
      TextView username = (TextView) v.findViewById(R.id.firstLine); 
      TextView email = (TextView) v.findViewById(R.id.secondLine); 

      Log.v(TAG, "user " + user.username); 
      Log.v(TAG, "mail " + user.email); 
      if (username != null) { 
       Log.v(TAG, "username NOT null"); 
       username.setText("user " + user.username); 
      } 

      if (email != null) { 
       Log.v(TAG, "email NOT null"); 
       email.setText("Email: " + user.email); 
      } 
     } 
     return v; 
    } 
} 

初始化:

ArrayList<UserRecord> appointment; 
UserItemAdapter aa; 
ListView lv; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    lv = (ListView) findViewById(R.id.listView1); 

    b1 = (Button) findViewById(R.id.button1); 
    b1.setOnClickListener(this); 

    appointment = new ArrayList<UserRecord>(); 
    aa = new UserItemAdapter(this, android.R.layout.simple_list_item_1, appointment); 
    lv.setAdapter(aa); 

} 

And finall Y,當我按下按鈕:

public void onClick(View v) { 

    if (v == b1) { 
     UserRecord ur = new UserRecord("User " + i, "[email protected]"); 
     Log.v(TAG, "button 1 " + i); 
     aa.add(ur); 
    } else { 
     Log.v(TAG, "unknown"); 
    } 
} 

問題是顯示「電子郵件」,而是「用戶名」不,雖然它同樣的方式處理,我看不出有什麼差別。另外,應該設置它的if()被採用。

有人得到了什麼是錯的提示嗎?

問候 託斯滕

+0

嘗試刪除引用您的用戶陣列(私人的ArrayList 用戶;)在您的適配器。相反,使用getItem(position)來檢索您的項目。 – dymmeh

回答

0

更改此

aa = new UserItemAdapter(this, android.R.layout.simple_list_item_1, appointment); 

此;

aa = new UserItemAdapter(this, R.layout.listitem, appointment); 

而且試試這個:

public void onClick(View v) { 

    if (v == b1) { 
     UserRecord ur = new UserRecord("User " + i, "[email protected]"); 
     Log.v(TAG, "button 1 " + i); 
     appointment.add(ur); 
     aa.notifyDataSetChanged(); 
    } else { 
     Log.v(TAG, "unknown"); 
    } 
} 
+0

我接管了你的第一部分,但也沒有影響行爲,我想我需要閱讀更多關於這一點。此外,我不認爲通知是必要的,項目添加沒有問題,只是用戶名缺失。 –

0

我可能是錯誤的,但我幾乎可以肯定,你應該這樣做:

public void onClick(View v) { 

    if (v == b1) { 
     UserRecord ur = new UserRecord("User " + i, "[email protected]"); 
     Log.v(TAG, "button 1 " + i); 
     users.add(ur); 
    } else { 
     Log.v(TAG, "unknown"); 
    } 
} 

相反的:

public void onClick(View v) { 

    if (v == b1) { 
     UserRecord ur = new UserRecord("User " + i, "[email protected]"); 
     Log.v(TAG, "button 1 " + i); 
     aa.add(ur); 
    } else { 
     Log.v(TAG, "unknown"); 
    } 
} 

我從來沒有加入項目添加到我的ADAPTER類中,但總是添加到對象的LIST中。在這種情況下,「用戶」而不是「aa」。

+0

感謝您的提示,我也可能被誤認爲是錯誤的,但在示例中描述了適配器也可用於此目的。此外,我不會錯過列表中的條目,如果我添加的項目工作正常,只是用戶名缺失(本身並沒有跡象表明代碼是好的)。 –

0

問題出在您的listitem.xml文件中。嘗試改變這一點:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="?android:attr/listPreferredItemHeight" 
android:padding="6dip" > 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_marginRight="6dip" 
    android:contentDescription="TODO" 
    android:src="@drawable/icon" /> 


<TextView 
    android:id="@+id/firstLine" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@id/icon" 
    android:gravity="center_vertical" 
    android:text="Example application" 
    android:textSize="16sp" /> 

<TextView 
    android:id="@+id/secondLine" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/icon" 
    android:ellipsize="marquee" 
    android:text="Description" 
    android:layout_below="@id/firstLine" 
    android:textSize="12sp" /> 

</RelativeLayout> 
+0

謝謝,這使它工作。 –