基于一个例子我试图创建一个显示自定义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()。
有没有人对错误提示?
祝你好运 托
答案 0 :(得分:0)
我可能会弄错,但我几乎肯定你应该这样做:
public void onClick(View v) {
if (v == b1) {
UserRecord ur = new UserRecord("User " + i, "mail@mail.net");
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, "mail@mail.net");
Log.v(TAG, "button 1 " + i);
aa.add(ur);
} else {
Log.v(TAG, "unknown");
}
}
我从来没有在我的ADAPTER类中添加一个项目,但总是添加到对象的LIST中。在这种情况下,“用户”而不是“aa”。
答案 1 :(得分: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>
答案 2 :(得分: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, "mail@mail.net");
Log.v(TAG, "button 1 " + i);
appointment.add(ur);
aa.notifyDataSetChanged();
} else {
Log.v(TAG, "unknown");
}
}