Android - PullToRefresh ListView始终为空

时间:2013-06-16 01:06:49

标签: android android-listview listadapter pull-to-refresh

我正在尝试在我的应用中实施chrisbanes's Android-PullToRefresh。 但到目前为止,我所得到的只是一个空洞的看法。 如果我将ListView更改为Android小部件它工作正常,如果我使用PullToRefreshListView列表显示为空。 我正在使用自定义ListAdapter。

这是XML部分:

<?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>

以下是活动中的代码(至少根据调试信息调用它):

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();
    }
}

如果它有所不同,那么包含此列表视图的布局将被夸大为基础布局:

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

是否有与此库相关的已知错误?我四处搜索但没有找到类似的东西。在此先感谢您的帮助:)

3 个答案:

答案 0 :(得分:4)

在尝试了Tarek的建议之后,我注意到PullToRefreshListView的高度始终为0.

如果我使用setContentView来设置布局而不是将其扩展到另一个布局......它可以正常工作,或者如果我设置固定高度new LayoutParams(LayoutParams.MATCH_PARENT, 1000)

在搞乱之后我最终在我的customOnCreate方法上添加了这段代码以使其正常工作:

((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);

感谢您的帮助:)

答案 1 :(得分:3)

我认为您的(dnt = new DownloadJogosTask()).execute()会下载Jogo并将其添加到lista

如果这是您的方案,请尝试以下方法:

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

}

onPostExecute DownloadJogosTask中,您致电la.notifyDataSetChanged();通知适配器您已将新值添加到lista,然后仅拨打lv.onRefreshComplete();隐藏加载动画。

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    la.notifyDataSetChanged();
    lv.onRefreshComplete();
}
希望这有帮助。

答案 2 :(得分:2)

从PullToRefreshListView

中的wrap_content替换match_parent
 <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>