简体   繁体   中英

Android - PullToRefresh ListView always empty

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 :)

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 :)

I assume that your (dnt = new DownloadJogosTask()).execute() will download Jogo s 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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM