我已经问过类似的问题 here一位用户建议对 ArrayAdapter
进行子类化,但我对此有疑问。
这是我的问题:
我使用HashMap<String, String>
和一个 SimpleAdapter
对象在我的 ListView
中显示用户数据,但现在我想替换 HashMap<String, String>
与 ArrayList<User>();
包含真实的用户对象。
这是旧代码:
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);
任何帮助将不胜感激。
最佳答案
我建议完全绕过ArrayAdapter
,因为您将进行自定义工作,因此您将在内部重复适配器所做的工作。无论如何,实现都是微不足道的。只是子类BaseAdapter
:
public class UserAdapter extends BaseAdapter {
private final List<User> mUsers;
private final LayoutInflater mInflater;
public UserAdapter(Context ctx, Collection<User> users) {
mUsers = new ArrayList<User>();
mInflater = LayoutInflater.from(ctx);
if (users != null) {
mUsers.addAll(users);
}
}
@Override
public int getCount() {
// Just return the number of users in your data set
return mUsers.size();
}
@Override
public long getItemId(int pos) {
// Mostly irrelevant if you're not using Cursors
return pos;
}
@Override
public User getItem(int pos) {
// Just return the item at the specified position
return mUsers.get(pos);
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
UserViewHolder uvh;
if (convertView == null) {
// Only inflate the layout if there's not already a recycled view
convertView = mInflater.inflate(R.layout.single_user, parent, false);
// Tag the view with a class holding the views found with
// findViewById() to prevent lookups later
uvh = new UserViewHolder(convertView);
convertView.setTag(uvh);
} else {
// If the view is non-null, then you will have already
// created the view holder and set it as a tag
uvh = (UserViewHolder) convertView.getTag();
}
// Now just get the user at the specified position and
// set up the view as necessary
User user = getItem(pos);
uvh.usernameTxt.setText(user.getUsername());
return convertView;
}
public static class UserViewHolder {
public final TextView usernameTxt;
public UserViewHolder(View v) {
usernameTxt = (TextView) v.findViewById(R.id.textViewUsername);
}
}
}
关于java - android中的子类ArrayAdapter用于显示ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26303519/