基于一个示例,我尝试创建一个显示自定义 ListItems 的 ListView。我在 XML 中定义 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>
我创建了一个类来保存 ListItem 中显示的数据:
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);
}
最后,当我按下按钮时:
public void onClick(View v) {
if (v == b1) {
UserRecord ur = new UserRecord("User " + i, "mail@mail.net");
Log.v(TAG, "button 1 " + i);
aa.add(ur);
} else {
Log.v(TAG, "unknown");
}
}
问题是显示了“电子邮件”,但没有显示“用户名”,虽然处理方式相同,但我看不出有什么区别。此外,它应该被设置的 if() 被采用。
有没有人暗示出了什么问题?
最好的问候托斯滕