基於一個示例,我試圖創建一個顯示自定義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()被採用。
有人得到了什麼是錯的提示嗎?
問候 託斯滕
嘗試刪除引用您的用戶陣列(私人的ArrayList用戶;)在您的適配器。相反,使用getItem(position)來檢索您的項目。 –
dymmeh