Android coustom Listview没有显示

时间:2015-05-03 08:46:39

标签: android listview android-listview models

UserInfo模型有三个字段名称,电子邮件,照片但是只想在列表视图中显示名称。当用户点击列表视图然后获取所有字段,如姓名,照片电子邮件

还想在listview中设置过滤器

我的Listview适配器类

public class UsersAdapter extends  ArrayAdapter<UserInfo> {
    ArrayList<UserInfo> users;
    Context context;
    LayoutInflater inflater;
    public UsersAdapter(Context context, ArrayList<UserInfo> users) {
        super(context,R.id.tvName, users);
       this.users= users;
       this.context= context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.items, null);
            holder = new Holder();
            convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

        holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
        holder.tvName.setText(users.get(position).getName());
        return convertView;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public UserInfo getItem(int position) {
        // TODO Auto-generated method stub
        return users.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }
    private class Holder {
        TextView tvName;
    }

3 个答案:

答案 0 :(得分:2)

问题是getcount方法应该如下所示。

@Override
public int getCount () {
    return users.size();
}

答案 1 :(得分:0)

ArrayAdapter已经实现了方法getItem(),getCount()并存储了显示项的ArrayList,所以

如果您使用ArrayAdapter<UserInfo>,则无需保存

private ArrayList<UserInfo> users;

因为在你的实例中,你的构造函数给了super

super(context,R.id.tvName, users) //users <-- the List<>

所以你可以删除函数,因为它们已经是ArrayAdapter的实现者

@Override
public int getCount() {
    return 0;
}

@Override
public UserInfo getItem(int position) {
    return users.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

在你的createView()中你应该像这样使用getItem(position)

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Holder holder;
    if(convertView == null) {
        convertView = inflater.inflate(R.layout.items, convertView, false);
        holder = new Holder();
        convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

    holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
    holder.tvName.setText(getItem(position).getName()); //<--------------
    return convertView;
}

答案 2 :(得分:0)

正如用户apk所说,您的getCount()方法需要显示适配器上有多少项,而且不需要将[LayoutInflater][1]用作全局变量。最重要的是,你忘了初始化inflater。

所以,这就是你必须要做的事情:

1-修复你的getCount()(感谢apk注意到)

@Override
public int getCount () {
    return users.size();
}

2-您仍然可以将LayoutInflater用作全局,但实际上并不需要。无论如何,在你的getView方法上,尝试这样做:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Holder holder;
    if(convertView == null) {
        inflater = (LayoutInflater) context_.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); // Use this to get the inflater.
        convertView = inflater.inflate(R.layout.items, null);
        holder = new Holder();
        convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

    holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
    holder.tvName.setText(users.get(position).getName());
    return convertView;
}