Android:ListActivity与CursorAdapter即使在重新查询时也不更新

Android:ListActivity与CursorAdapter即使在重新查询时也不更新,android,listactivity,android-cursoradapter,Android,Listactivity,Android Cursoradapter,我有一个SearchActivity(扩展ListActivity)和一个SearchAdapter,用于获取搜索结果。我可以在logcat中看到,cursor.getCount()随着每次点击而增加,cursor.requery()返回true,甚至getListView().getCount()随着每次搜索结果而增加,但ListView保持为空。对于适配器中的每个新搜索结果,还将调用update方法 我不知道还有什么可能是错的。这里还有谁有过这样的问题吗 public class Searc

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

我不知道还有什么可能是错的。这里还有谁有过这样的问题吗

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);
}
公共类SearchableActivity扩展ListActivity实现Observer{
私有静态最终字符串标记=“SearchableActivity”;
私有上下文;
私有搜索游标适配器msearchapter;
私有Uri-cpUri;
私有列表视图;
私有可运行更新;
私人处理手不可命名;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
mContext=这个;
mListView=getListView();
setText(getString(R.string.search_results));
cpUri=com.pd.database.MyProvider.CONTENT_URI.buildon().appendPath(“searchresults”).build();
Cursor Cursor=this.managedQuery(cpUri,null,null,null);
msearchapter=newsearchcursoradapter(mContext,cursor);
setAdapter(msearchapter);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Intent podcastDetails=新的Intent(getApplicationContext(),MPDetails.class);
podcastDetails.putExtra(“id”,id);
星触觉(pDetails);
}
});
handleRunnable=新处理程序();
updateUI=newrunnable(){
@凌驾
公开募捐{
Log.d(标记“in Runnable()”);
Log.d(标记“cursor.getCount()beforde:”+String.valueOf(msearchapter.mCursor.getCount());
Log.d(标记“requery returns:”+String.valueOf(mSearchAdapter.mCursor.requery());
Log.d(标记“+String.valueOf(mSearchAdapter.mCursor.getCount())”之后的cursor.getCount()”;
Log.d(标记“count ListViewItems:+String.valueOf(getListView().getCount()));
msearchapter.notifyDataSetChanged();
}
};
handleIntent(getIntent());
}
@凌驾
受保护的void onNewIntent(意图){
设定意图(intent);
手册内容(意图);
}
私人无效手册内容(意图){
if(Intent.ACTION_SEARCH.equals(Intent.getAction())){
String query=intent.getStringExtra(SearchManager.query);
SearchForPodcast(查询);
}
}
私有void searchForPD(字符串查询){
试一试{
URL searchURL=新URL(MDirectories.SEARCH+query);
RSSHandlerSearch searchHandler=新的RSSHandlerSearch(mContext,cpUri);
searchHandler.addObserver(this);
UrlHandlerBar stuff=新的UrlHandlerBar(mContext、searchURL、searchHandler、mProgressbar、cpUri、this);
新建NetworkData().execute(stuff);
}捕获(格式错误){}
}
@凌驾
公共无效更新(可观测、对象数据){
handleRunnable.post(updateUI);
}
}IMHO
setListAdapter(mSearchAdapter)
缺失


您应该在创建时将其添加到
的末尾

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

这帮助了我:

                if( myAdapter.getCount() == 0 ) {
                    myAdapter.changeCursor(newCursor);
                    myAdapter.notifyDataSetChanged();
                }
我只是添加了只在listView为空时才更改光标的代码


希望这会有所帮助。

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

适配器设置在onCreate的第9行