我已经问了一个类似的问题here和一个用户建议子类ArrayAdapter
,但我有问题。 这里是我的问题:Android中的子类ArrayAdapter显示ArrayList
我使用HashMap<String, String>
和SimpleAdapter
对象在我ListView
显示的用户数据,但现在我想用含有真正的用户对象的ArrayList<User>();
更换HashMap<String, String>
。
这里是旧代码:
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_USERNAME, c.getString(TAG_USERNAME));
map.put(TAG_FIRSTNAME, c.getString(TAG_FIRSTNAME));
map.put(TAG_LASTNAME, c.getString(TAG_LASTNAME));
map.put(TAG_ADDRESS, c.getString(TAG_ADDRESS));
usersList.add(map);
[...]现在
ListAdapter adapter = new SimpleAdapter(this, usersList,
R.layout.single_user, new String[] { TAG_USERNAME,
TAG_FIRSTNAME, TAG_LASTNAME, TAG_ADDRESS },
new int[] { R.id.textViewUsername, R.id.textViewFirstName,
R.id.textViewLastName, R.id.textViewAddress });
,作为建议,我想继承ArrayAdapter
,但我没有计划如何getView(int, View, ViewGroup)
应该看喜欢,达到我的目标。
这里是我当前的代码:
public class UserArrayAdapter extends ArrayAdapter<User> {
private Context mcon;
private List<User> userList;
public UserArrayAdapter(Context context, int resource,
List<User> userList) {
super(context, resource);
mcon = context;
this.userList = userList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
for (User usr : userList) {
findViewById(R.id.textViewUsername).setText(usr.getFirstName());
}
return super.getView(position, convertView, parent);
}
}
下面是用户等级:
public class User {
private String username, firstName, lastName, address;
public User(String username, String firstName, String lastName,
String address) {
setUsername(username);
setFirstName(firstName);
setLastName(lastName);
setAddress(address);
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
if (username == null) {
throw new IllegalArgumentException("username is null.");
}
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
if (firstName == null) {
throw new IllegalArgumentException("firstName is null.");
}
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
if (lastName == null) {
throw new IllegalArgumentException("lastName is null.");
}
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
if (address == null) {
throw new IllegalArgumentException("address is null.");
}
this.address = address;
}
}
而且我以为我会这样称呼它:
ListAdapter ad = new UserArrayAdapter(this, R.layout.single_user, usersArrList);
任何帮助将不胜感激。
向我们展示您的用户类 – 2014-10-10 16:04:19
当然。编辑完成。 – user3475602 2014-10-10 16:07:20