刷新ArrayAdapter onResume [notifyDataSetChanged()不工作] [英] Refreshing ArrayAdapter onResume [notifyDataSetChanged() not working]

查看:246
本文介绍了刷新ArrayAdapter onResume [notifyDataSetChanged()不工作]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建联系人列表应用程序中使用的片段,其中一个断枝是联系人列表,另一个在名称列表中的细节的其余部分。

I am creating a contact list app using Fragments where one frag is a list of names in the contact list and the other is the rest of the details.

下面是一个显示名列表的类

Here is the class that displays the list of names

public class MyListFragment extends ListFragment {

private ContactStorage contactStorage = new ContactStorage();

public final static String TAG = "FRAGMENTS";
private MainActivity parent;
ArrayAdapter<String> adapter;

ArrayList<String> entries = new ArrayList<String>();

String array[];

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.list_layout, null);
    parent = (MainActivity) getActivity();
    entries = contactStorage.getContactListNames();

    adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), 
            android.R.layout.simple_list_item_1, entries);

    setListAdapter(adapter);
    Log.d(TAG, "Adapter created");
    array = contactStorage.getContactDetails();
    return v;

}

@Override
public void onResume() {

    super.onResume();
    entries = contactStorage.getContactListNames();
    adapter.notifyDataSetChanged();
    Log.d(TAG, "List Frag Resumed");

}
}

我遇到的问题是,一个ArrayAdapter不会在简历刷新。

The problem I am having is that the ArrayAdapter does not refresh on resume.

当屏幕旋转时,其细如onCreateView()再次跑了,但我需要它来刷新onResume。我已经看过了这个网站负载,发现不过是用notifyDataSetChanged(),这是行不通的。

When the screen is rotated, its fine as onCreateView() is ran again, but I need it to refresh onResume. I have looked over this site loads and found nothing but "use notifyDataSetChanged()" which doesn't work.

下面是完整的项目,如果它的任何帮助

here is the full project if it's of any help

<一个href="http://www.numyspace.co.uk/~unn_w11001476/FragmentExample.zip">www.numyspace.co.uk/~unn_w11001476/FragmentExample.zip

推荐答案

您遇到的问题是,你覆盖项参考和它没有得到适配器上改变。这里是你如何解决这个问题

the issue you're having is that you're overwriting the entries reference and it's not getting changed on the adapter. Here's how you can fix this

@Override
public void onResume() {

    super.onResume();
    entries.clear();
    entries.addAll(contactStorage.getContactListNames());
    adapter.notifyDataSetChanged();
    Log.d(TAG, "List Frag Resumed");

}

这是一个常见的​​错误之作,它造成的,因为当你第一次创建一个列表(在内存中),你的输入字段指向,你再告诉适配器来看看,当你创建它,但 onResume 创建一个新的列表在内存(当你的内存位置再次联系人列表名称),你告诉条目指向新的列表在内存中,你需要做的是替换原始列表与新列表中的条目,这样的适配器中的条目还是会参考同一个列表中。

this is a common mistake to make, it's caused because when you first create a list (in memory) and your entries field points to that, you then tell the adapter to look at that memory location when you create it, but onResume you create a new list in memory (when you get the contact list names again) and you tell entries to point to that new list in memory, what you need to do is replace the entries in the original list with the entries in the new list, that way the adapter will still reference the same list.

这篇关于刷新ArrayAdapter onResume [notifyDataSetChanged()不工作]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆