3

Using Chris Banes' PullToRefresh library, the getView() method of my custom adapter is not being called. This code works fine without using his library and getView() is called. I've been looking into this for a couple days now and cannot figure out what's wrong. Any feedback would be greatly appreciated!

MyActivity.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);

MyAdapter.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
        ...
    }
}
jah15
  • 123
  • 1
  • 2
  • 8
  • I don't see any reason for your problem. Do you have same result when you use this adapter with android's standart ListView? – Devrim Nov 13 '13 at 17:51
  • I have no problems when i use android's standard list view :( .. only when i use the pull-to-refresh list view. Also, this is working for a different activity in my app, but not for a few others i'm trying to implement. There's nothing different I'm doing in the activity where it's working too. – jah15 Nov 13 '13 at 18:43

2 Answers2

9

Google IO 2013 has made many changes in ListView to optimize results. I had the similar problem as yours, my getView() function of custom adapter was not called.

According to new changes, if you are using "wrap_content" as layout_height of your view, getView() will be called only when user scrolls the list. So instead use "fill_parent" as layout_height of your view. It will solve your problem.

Use,

android:layout_height="fill_parent"

instead of using

android:layout_height="wrap_content"
rutulPatel
  • 320
  • 5
  • 12
0

@RTL

Your answer is not correct,I tried that but failed. Later I find the right solution.

We lacked getCount()

So we should add

@Override
public int getCount() {
    return yourArr.length;
}
David Wang
  • 934
  • 1
  • 12
  • 16