我有一个SearchActivity(扩展ListActivity)和一个SearchAdapter,它将获取搜索结果。我可以在logcat中看到,cursor.getCount()随着每次命中而增加,而cursor.requery()返回true,甚至getListView()。getCount()随着每个searchresult而增加,但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);
}
}
答案 0 :(得分:1)
我有这个问题并修复了它。关键是检查getReadableDatabase(),getWritableDatabase()和close()调用。请记住,根据文档,getReadableDatabase()返回的数据库对象是“有效的,直到调用getWritableDatabase()或close()。”
答案 1 :(得分:0)
恕我直言setListAdapter( mSearchAdapter )
缺失
您应该在onCreate
答案 2 :(得分:0)
我有类似的问题,刷新空ListView的状态。
这帮助了我:
if( myAdapter.getCount() == 0 ) {
myAdapter.changeCursor(newCursor);
myAdapter.notifyDataSetChanged();
}
我只是在listView为空时添加了更改游标的代码。
希望这会有所帮助。