3

I'm trying to implement chrisbanes's Android-PullToRefresh in my app. But so far all I'm getting is an empty view. If I change the ListView to an Android widget it works fine, if I use PullToRefreshListView the list shows up empty. I'm using a custom ListAdapter.

Here's the XML part:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical" >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/listview_jogos"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null" >
    </com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>

Here is the code in the activity (it's being called, at least according to the debug information):

private void constroiLista(ArrayList<Jogo> lista)
{
    PullToRefreshListView lv = (PullToRefreshListView)findViewById(R.id.listview_jogos);
    if(lv != null && lista != null)
    {
        lv.setAdapter(new ListAdapter(this, lista));
        lv.setOnRefreshListener(new OnRefreshListener<ListView>()
        {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView)
            {
                (dnt = new DownloadJogosTask()).execute();
            }
        });
        // Call onRefreshComplete when the list has been refreshed.
        lv.onRefreshComplete();
    }
}

And if it makes a difference the layout containing this listview is being inflated into a base one:

((LinearLayout)findViewById(R.id.ll_base_layout)).addView(View.inflate(this, R.layout.lista_jogos, null));

Is there any known bugs associated with this library? I searched around but haven't found anything similar. Thanks in advance for your help :)

Marco Batista
  • 1,410
  • 1
  • 20
  • 38

3 Answers3

4

After trying what was suggested by Tarek, I noticed that the PullToRefreshListView's height was always 0.

If I use setContentView to set the layout instead of inflating it into another.. it works or if I set a fixed height new LayoutParams(LayoutParams.MATCH_PARENT, 1000)

After messing around I ended up adding this block of code on my customOnCreate method to make it work:

((LinearLayout)findViewById(R.id.ll_base_layout)).addView(View.inflate(this, R.layout.lista_jogos, null));
LinearLayout ll = (LinearLayout)findViewById(R.id.ll_jogos);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)ll.getLayoutParams();
params.weight = 1;
ll.setLayoutParams(params);

Thanks for your help :)

Marco Batista
  • 1,410
  • 1
  • 20
  • 38
3

I assume that your (dnt = new DownloadJogosTask()).execute() will download Jogos and add them to lista.

If this is your scenario than try the following:

ListAdapter la = null; //make your ListAdapter global in your activity
PullToRefreshListView lv = null; //also the same for the list

private void constroiLista(ArrayList<Jogo> lista) {

    lv = (PullToRefreshListView) findViewById(R.id.listview_jogos);
    la = new ListAdapter(this, lista); // initialize the list adapter
    lv.setAdapter(la);

    lv.setOnRefreshListener(new OnRefreshListener<ListView>() {
        @Override
        public void onRefresh(PullToRefreshBase<ListView> refreshView) {
            if (lv != null && lista != null) { //if null do not execute the task
                (dnt = new DownloadJogosTask()).execute();
            }
        }
    });
    // here is not when the list refresh is complete so remove this line

}

in the onPostExecute of DownloadJogosTask you call la.notifyDataSetChanged(); to notify the adapter that you have new values added to your lista and then call lv.onRefreshComplete(); which only hides the loading animation.

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    la.notifyDataSetChanged();
    lv.onRefreshComplete();
}

hope this help.

Tarek K. Ajaj
  • 2,461
  • 1
  • 19
  • 24
2

Replace match_parent from wrap_content in PullToRefreshListView

 <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/listview_jogos"
        android:layout_width="match_parent"
        android:layout_height="match_parent" <= Replace This Attribute
        android:divider="@null" >
    </com.handmark.pulltorefresh.library.PullToRefreshListView>
yuki tamazawa
  • 21
  • 1
  • 4