我正在使用 SherlockListFragment(与 ListFragment 相同)来显示短信对话列表
这是代码
public static class TitlesFragment extends SherlockListFragment
{
static ConversationAdapter adapter;
static List<String> msgList;
static Activity activity;
static ListView listView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DataGetters dataGetters = new DataGetters();
activity = getActivity();
msgList = dataGetters.getCONVERSATIONS(activity.getApplicationContext());
adapter = new ConversationAdapter(activity, msgList);
setListAdapter(adapter);
}
上面的代码打印所有当前的短信对话,如下所示:
我通过调用 adapter.notifyDataSetChanged(); 从下面的代码中刷新适配器;收到新短信时会调用女巫
public class ReceiverClass extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
Thread.sleep(2000);
return null;
}
@Override
protected void onPostExecute(String result) {
TitlesFragment.adapter.notifyDataSetChanged();
}
但是闲置代码会覆盖现有的列表视图项,以添加新的列表项位置,如下所示:
当我重新创建活动时,我得到了我想要的,但只有这样:例如: