Android:带有 CursorAdapter 的 ListActivity 即使重新查询也不会更新

发布于 10-24 17:44 字数 3016 浏览 5 评论 0原文

我有一个 SearchActivity (扩展 ListActivity)和一个 SearchAdapter 将获取搜索结果。我可以在 logcat 中看到,cursor.getCount() 随着每次点击而增加,cursor.requery() 返回 true,甚至 getListView().getCount() 随着每次搜索结果而增加,但 ListView 保持为空。对于我的适配器中的每个新搜索结果,也会调用更新方法。

我不知道还有什么可能是错误的。这里还有其他人遇到过这样的问题吗?

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沉睡月亮2024-10-31 17:44:54

我遇到了这个问题并修复了它。关键是检查 getReadableDatabase()、getWritableDatabase() 和 close() 调用。请记住,根据文档, getReadableDatabase() 返回的数据库对象“在调用 getWritableDatabase() 或 close() 之前一直有效”。

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."

给妤﹃绝世温柔2024-10-31 17:44:54

恕我直言,缺少 setListAdapter( mSearchAdapter )

您应该将其添加到 onCreate 的末尾

IMHO setListAdapter( mSearchAdapter ) is missing

You should add it at the end of onCreate

网白2024-10-31 17:44:54

我在刷新空 ListView 状态时遇到了类似的问题。

这对我很有帮助:

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

我简单地添加了仅当 listView 为空时才更改光标的代码。

希望这会有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文