3

使用 Chris Banes 的 PullToRefresh 库,我的自定义适配器的 getView() 方法没有被调用。这段代码在使用他的库的情况下工作正常,并且调用了 getView()。我已经研究了几天了,无法弄清楚出了什么问题。任何反馈将不胜感激!

我的活动.java

MyAdapter adapter = new MyAdapter(this, data);
// the following toast displays the correct count
Toast.makeText("MyActivity", adapter.getCount(), Toast.LENGTH_SHORT).show(); 
myListView.setAdapter(adapter);

我的适配器.java

public class MyAdapter extends ArrayAdapter<Object> {

    private ArrayList<Object> data;
    private LayoutInflater vi;
    private Context context;

    public MyAdapter(Context context, ArrayList<Object> data) {
        super(context, 0);
        this.context = context;
        this.data = data;
        this.vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    static class ViewHolder {
        ...
    }

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

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

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // not getting called
        ...
    }
}
4

2 回答 2

9

Google IO 2013 对 ListView 进行了许多更改以优化结果。我遇到了和你类似的问题,我的自定义适配器的 getView() 函数没有被调用。

根据新的更改,如果您使用“wrap_content”作为视图的 layout_height,则只有在用户滚动列表时才会调用 getView()。因此,请改为使用“fill_parent”作为视图的 layout_height。它会解决你的问题。

利用,

android:layout_height="fill_parent"

而不是使用

android:layout_height="wrap_content"
于 2014-01-03T02:13:41.857 回答
0

@RTL

您的答案不正确,我尝试过但失败了。后来我找到了正确的解决方案。

我们缺少getCount()

所以我们应该添加

@Override
public int getCount() {
    return yourArr.length;
}
于 2015-10-04T06:07:30.217 回答