简体   繁体   中英

Android: ListActivity with CursorAdapter not updating even with requery

I have a SearchActivity (extends ListActivity) and a SearchAdapter which will fetch the searchresults. I can see in logcat, that cursor.getCount() increases with every hit and cursor.requery() returns true and even getListView().getCount() increases with every searchresult, but the ListView stays just empty. With every new searchresult in my adapter the update method is also called.

I have no idea what possibly else could be wrong. Anybody else here with such a problem ever before?

public class SearchableActivity extends ListActivity implements Observer {

private static final String TAG = "SearchableActivity";
private Context mContext;
private SearchCursorAdapter mSearchAdapter;
private Uri cpUri;
private ListView mListView;
private Runnable updateUI;
private Handler handleRunnable;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);
    mContext = this;
    mListView = getListView();
    tvTitle.setText(getString(R.string.search_results));
    cpUri = com.pd.database.MyProvider.CONTENT_URI.buildUpon().appendPath("searchresults").build();
    Cursor cursor = this.managedQuery(cpUri, null, null, null, null);
    mSearchAdapter = new SearchCursorAdapter(mContext, cursor);
    mListView.setAdapter(mSearchAdapter);
    mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent podcastDetails = new Intent(getApplicationContext(), MPDetails.class);
            podcastDetails.putExtra("id", id);
            startActivity(pDetails);
        }
    });
    handleRunnable = new Handler();
    updateUI = new Runnable() {

        @Override
        public void run() {
            Log.d(TAG, "in Runnable()");
            Log.d(TAG, "cursor.getCount() beforde: " + String.valueOf(mSearchAdapter.mCursor.getCount()));
            Log.d(TAG, "requery returns: " + String.valueOf(mSearchAdapter.mCursor.requery()));
            Log.d(TAG, "cursor.getCount() after: " + String.valueOf(mSearchAdapter.mCursor.getCount()));
            Log.d(TAG, "count ListViewItems: " + String.valueOf(getListView().getCount()));
            mSearchAdapter.notifyDataSetChanged();
        }
    };
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        searchForPodcasts(query);
    }
}

private void searchForPD(String query) {
    try {
        URL searchURL = new URL(MDirectories.SEARCH+query);
        RSSHandlerSearch searchHandler = new RSSHandlerSearch(mContext, cpUri);
        searchHandler.addObserver(this);
        UrlHandlerBar stuff = new UrlHandlerBar(mContext, searchURL, searchHandler, mProgressbar, cpUri, this);
        new NetworkData().execute(stuff);
    } catch (MalformedURLException e) {}
}

@Override
public void update(Observable observable, Object data) {
    handleRunnable.post(updateUI);
}

}

I had this issue and fixed it. The key is to check your getReadableDatabase(), getWritableDatabase() and close() calls. Remember that according to the documentation the database object returned by getReadableDatabase() is "valid until getWritableDatabase() or close() is called."

IMHO setListAdapter( mSearchAdapter ) is missing

You should add it at the end of onCreate

I had similar issue with refreshing state of empty ListView.

That helped me:

                if( myAdapter.getCount() == 0 ) {
                    myAdapter.changeCursor(newCursor);
                    myAdapter.notifyDataSetChanged();
                }

Simply I added code that was changing cursor only when listView was empty.

Hope that will help.

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