【发布时间】:2026-02-03 23:35:01
【问题描述】:
我有一个使用 CursorAdapter 的 ListActivity,但我无法让它刷新。这是我正在使用的一些代码:
public class ContactDetailsActivity extends ListActivity
{
private ContactDetailsBroadcastReceiver mLvBroadcastReceiver;
private Contact contact;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
contact = (Contact)getIntent().getSerializableExtra(AwesomeApp.CONTACT_OBJECT);
if(contact == null) {
DataUtils.log("Starting ContactDetailsActivity with NULL contact.");
Toast.makeText(this, getString(R.string.no_contact_selected), Toast.LENGTH_SHORT).show();
finish();
return;
}
DataUtils.log("Starting ContactDetailsActivity with contact:" + contact.toString());
setTitle(contact.getName());
Cursor cursor = getContactDetailsCursor();
startManagingCursor(cursor);
setListAdapter(new ContactDetailsCursorAdapter(this, cursor));
ListView lv = getListView();
lv.setBackgroundResource(R.color.white);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Toast.makeText(ContactDetailsActivity.this, "touched", Toast.LENGTH_SHORT).show();
}
});
//Create broadcast receiver, to detect when a file upload has finished so we can update the pic status.
mLvBroadcastReceiver = new ContactDetailsBroadcastReceiver();
}
private class ContactDetailsBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
DataUtils.log("In BroadcastReceiver - ContactDetails");
refreshListView();
}
}
我的适配器基本上只是扩展了CursorAdapter,在其中我覆盖了newView 和bindView:
public class ContactDetailsCursorAdapter extends CursorAdapter
我在ContactDetailsActivity 中有一个名为refreshListView() 的方法,我在onResume() 和我的BroadcastReceiver 的onReceive() 方法中调用它
private void refreshListView()
{
DataUtils.log("In ContactDetailsActivity refreshListView");
((ContactDetailsCursorAdapter)getListAdapter()).notifyDataSetChanged();
this.getListView().invalidate();
this.getListView().invalidateViews();
}
这是我的getContactDetailsCursor() 方法:
private Cursor getContactDetailsCursor()
{
AwesomeDbAdapter dbAdapter = AwesomeApp.getDbAdapter(this);
Cursor c = dbAdapter.getRecentConversationsFromContact(contact.getId());
DataUtils.log("New ContactDetails cursor has " + c.getCount() + " elements");
return c;
}
如您所见,我尝试了notifyDataSetChanged()、invalidate()、invalidateViews(),但似乎没有任何东西可以刷新列表视图。
我完全不知道如何刷新它。我正在查看日志,并且正在从 onResume() 和 BroadcastReceiver 的 onReceive() 方法调用该方法。
任何指针表示赞赏!
【问题讨论】:
标签: android android-listview listactivity listadapter android-cursoradapter