Android:PullToRefresh ListView没有显示

时间:2013-02-21 07:30:05

标签: android android-listview

我已将Chris Banes implementation of pull to refresh list view 用于我的应用。问题是如果我将列表视图的可见性设置为已删除或不可见并使其在java代码中可见,则列表不会显示。另一方面,如果我将其可见性设置为可见或不设置其可见性,则每件事都可以正常工作。我的要求是我在同一个活动中有两个列表视图。我必须设置可见性,因为一旦从服务器获取数据,它将首先出现。另一个将出现在搜索功能上。我已将搜索结果列表视图的可见性设置为xml代码中的可见性,并且只有在获得搜索结果后才能将其显示。尽管对此列表视图使用 setVisibility(),但它从不显示屏幕。我也检查了服务器响应。它在logcat上显示搜索结果。

我在下面发布我的代码:

来自活动的

代码段

//The result from this async task will populate the first list view

if(NetworkConnection.isOnline(MainCategory.this))
{
    new MainMenuAsyncTask(dataUrl, MainCategory.this, listMainMenu, false).execute();
}
else
{
    Log.v(TAG, "no network available");
    SeattleNightLifeUtility.OpenWiFiDialog(MainCategory.this, getResources().getString(R.string.no_internet_msg));
}

loadListView();

//This will populate the list view that I have created for search results

_txtAutoSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() 
{
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
    {
        String term = _txtAutoSearch.getText().toString().trim();
        if(! term.equals(""))
        {
            SeattleNightLifeUtility.hideSoftKeyboard(MainCategory.this, _txtAutoSearch);
            if(NetworkConnection.isOnline(MainCategory.this))
            {
                search(term, false);
            }
            else
            {
                SeattleNightLifeUtility.OpenWiFiDialog(MainCategory.this, getResources().getString(R.string.no_internet_msg));
            }
        }
        return true;
        }//onEditorAction
    });

    listMainMenu.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() 
    {
        @Override
        public void onRefresh() 
        {
            if(NetworkConnection.isOnline(MainCategory.this))
            {
                new MainMenuAsyncTask(dataUrl, MainCategory.this, listMainMenu, true).execute();
            }
            else
            {
                SeattleNightLifeUtility.OpenWiFiDialog(MainCategory.this, getResources().getString(R.string.no_internet_msg));
            }
        }
    });

    listViewSearch.setOnRefreshListener(new PullToRefreshListView.OnRefreshListener() 
    {
        public void onRefresh() 
        {
            if(NetworkConnection.isOnline(MainCategory.this))
            {
                search(_txtAutoSearch.getText().toString().trim(), true);
            }
            else
            {
                SeattleNightLifeUtility.OpenWiFiDialog(MainCategory.this, getResources().getString(R.string.no_internet_msg));
            }
        }
    });

搜索结果异步任务

public class GetSearchAsyncTask extends AsyncTask<Void, Void, String> 
{
Context ctx;
ProgressDialog pd;
PullToRefreshListView listViewSearch;
public static final String TAG = "GetSearchAsyncTask";
public static ArrayList<SearchDAO> searchArrayList;

private String term, callingclass;
private TextView txtNoData;
boolean flag;

public GetSearchAsyncTask(String term, Context ctx, 
        PullToRefreshListView listViewSearch, TextView txtNoData, 
        String callingclass, boolean flag) 
{
    this.term = term;
    this.ctx = ctx;
    this.listViewSearch = listViewSearch;
    this.txtNoData = txtNoData;
    this.callingclass = callingclass;
    this.flag = flag;
}//Constructor 

@Override
protected void onPreExecute() 
{
    if(flag == false)
    {
        pd = new ProgressDialog(ctx);
        pd.setMessage(ctx.getResources().getString(R.string.please_wait));
        pd.show();
    }
}//onPreExecute

protected String doInBackground(Void... params) 
{
    String parsed = ServerConnection.getSearchedData(term);
    try
    {
        if(flag == true)
        {
            Log.v(TAG, "doInBackground isListRefreshed is true");
            Thread.sleep(2000);
        }
    }
    catch(Exception e){}
    return parsed;
}//doInBackground

@Override
protected void onPostExecute(String result) 
{
    searchArrayList = ParsedSearchData.getSearchedData(result);
    listViewSearch.setVisibility(View.VISIBLE);

    if(searchArrayList != null && searchArrayList.size() > 0)
    {
        Log.v(TAG, "searcharraylist not null");
        for(int i = 0; i < searchArrayList.size(); i++)
        {
            Log.v(TAG, "Name: "+searchArrayList.get(i).getMerchant());
        }
        SearchAdapter mSearchAdapter = new SearchAdapter(ctx, searchArrayList);
        mSearchAdapter.notifyDataSetChanged();
        listViewSearch.setAdapter(mSearchAdapter);

        if(callingclass.equals("EventActivity"))
        {
            Log.v(TAG, "callingclass EventActivity");
            if(txtNoData.getVisibility() == View.VISIBLE)
            {
                Log.v(TAG, "txtNoData VISIBLE");
                txtNoData.setVisibility(View.GONE);
            }
            if(((EventsActivity)ctx).txtNoEvent.getVisibility() == View.VISIBLE)
            {
                Log.v(TAG, "txtNoEvent VISIBLE");
                ((EventsActivity)ctx).txtNoEvent.setVisibility(View.GONE);
            }
        }
        else
        {
            Log.v(TAG, "callingclass not EventActivity");
            if(txtNoData.getVisibility() == View.VISIBLE)
            {
                Log.v(TAG, "else loop txtNoData VISIBLE");
                txtNoData.setVisibility(View.GONE);
            }
            if(listViewSearch.getVisibility() == View.VISIBLE)
            {
                Log.v(TAG, "listViewSearch VISIBLE");

            }
            else
            {
                Log.v(TAG, "listViewSearch INVISIBLE");
            }
        }
    }
    else
    {
        Log.v(TAG, "searcharraylist null");
        if(callingclass.equals("EventActivity"))
        {
            Log.v(TAG, "callingclass EventActivity");
            txtNoData.setVisibility(View.VISIBLE);
            listViewSearch.setVisibility(View.GONE);

            if(((EventsActivity)ctx).txtNoEvent.getVisibility() == View.VISIBLE)
            {
                Log.v(TAG, "searcharraylist null else txtNoEvent VISIBLE");
                ((EventsActivity)ctx).txtNoEvent.setVisibility(View.GONE);
            }
        }
        else
        {
            Log.v(TAG, "callingclass not EventActivitysearcharraylist null else txtNoEvent VISIBLE");
            txtNoData.setVisibility(View.VISIBLE);
            listViewSearch.setVisibility(View.GONE);
        }
    }

    if(flag == false)
    {
        if(pd != null)
        {
            Log.v(TAG, "onPostExecute pd not null");
            if(pd.isShowing())
            {
                Log.v(TAG, "onPostExecute pd is showing");
                pd.dismiss();
            }
        }
    }
    else
    {
        listViewSearch.onRefreshComplete();
    }
}//onPostExecute
}

搜索方法

protected void search(String term, boolean result) 
{
    listMainMenu.setVisibility(View.GONE);
    //listViewSearch.setVisibility(View.VISIBLE);
    new GetSearchAsyncTask(term, MainCategory.this, listViewSearch , txtNoData, "MainCategory", result).execute();
}//search

1 个答案:

答案 0 :(得分:5)

之前我在XML中设置了gone的可见性,在java代码中,我将其设为VISIBLE。那时,名单没有出现。当我从XML布局文件中删除visibility属性,并且仅使用 setVisibility()在java代码中设置它时,它工作得很完美。我无法弄清楚这背后的原因。可能是,我需要看看库的实现,以便找到我出错的地方。但是,就目前而言,这对我有用。