简体   繁体   中英

Android: PullToRefresh ListView not showing

I have used Chris Banes implementation of pull to refresh list view for my app. The problem is if I set visibility for list view as gone or invisible and make it visible in java code, the list doesn't shows up. On the other hand, if I set its visibility as visible or don't set its visibility, every thing works fine. My requirement is such that I have two list views in the same activity. I have to set the visibility as one will appear first once it get data from server. The other will appear on search function. I had set the visibility for search result's listview as gone in the xml code, and making it visible only once it gets search results. Despite using setVisibility() for this listview, it never shows up screen. I had checked server response as well. It is showing search result on logcat.

I am posting my code below:

Code Snippet from Activity

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

Search result Async Task

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
}

Search Method

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

Earlier I was setting visibility of in the XML as gone and in java code, I was making it VISIBLE . At that time, the list didn't showed up. When I removed the visibility attribute from XML layout file, and only set it in java code with setVisibility() , it worked perfect. I couldn't figured out the reason behind this. May be, I need to take a look at the implementation of library so that I find where did I went wrong. But, for the time being, this is what worked for me.

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