我有一个使用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()
方法调用的。
任何指针都表示赞赏!
答案 0 :(得分:1)
这最终对我有用。正如@Kirill Shalnov所提到的,startManagingCursor()
已被弃用,但这是我需要使用的。说实话,这一切都是猜测,但唉,做了我需要的。
@SuppressWarnings("deprecation")
private void refreshListView()
{
DataUtils.log("In ContactDetailsActivity refreshListView");
ContactDetailsCursorAdapter adapter = (ContactDetailsCursorAdapter)getListAdapter();
Cursor oldCursor = null;
if(adapter != null) {
oldCursor = adapter.getCursor();
}
Cursor newCursor = getContactDetailsCursor();
if(oldCursor != null) {
stopManagingCursor(oldCursor);
oldCursor.close();
}
startManagingCursor(newCursor);
adapter = null;
setListAdapter(new ContactDetailsCursorAdapter(this, newCursor));
ListView listView = getListView();
listView.setBackgroundResource(R.color.white);
listView.invalidate();
listView.invalidateViews();
}
答案 1 :(得分:0)
好像你忘了注册接收器了: 注册接收器而不注册它不会发射,如下所示注册接收器。
实施例: 在OnCreate中:
ContactDetailsBroadcastReceiver receiver=new ContactDetailsBroadcastReceiver();
IntentFilter filter=new IntentFilter("android.net.wifi.STATE_CHANGE")
registerReceiver(receiver, filter);
在OnDestroy中:
unregisterReceiver(receiver);
答案 2 :(得分:0)
startManagingCursor(光标);
使用游标是不推荐使用的方式,你应该使用Loaders,它可以自动重新加载数据