簡體   English   中英

數據獲取后,notifyDataSetChanged無法正常工作,第二次-ListView無法刷新

[英]notifyDataSetChanged not working after data fetch, the second time - ListView won't refresh

我有一個包含listview的片段,該片段使用Volley從網絡中獲取數據,並且在響應時,使用自定義arrayadapter將檢索到的列表顯示在listview中。

問題是,第一次加載時,一切正常; 但是關閉活動並返回時,列表視圖永遠不會在響應時刷新。 我需要強制關閉該應用程序才能使其再次運行。 我注意到的一件事是,getActivity()在此階段為null。 this.activity不是。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_interests, container, false);

    // Set the adapter
    mListView = (AbsListView) view.findViewById(android.R.id.list);
    ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);

    // Set OnItemClickListener so we can be notified on item clicks
    mListView.setOnItemClickListener(this);

    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    SelectionListContent.getItems().clear();
    SelectionListContent.ITEMS.add(new SelectionListContent.Item("0", "Loading...", false));
    mAdapter = new SelectionListViewAdapter(this.activity,
            R.layout.listviewcheckbox, SelectionListContent.ITEMS);
}

@Override
public void onResume() {
    super.onResume();
    CategoriesData.getInstance(this.activity, new CategoriesData.CategoriesDataListener() {
        @Override
        public void gotCategories(List<HashMap<String, Object>> categories) {
            InterestsFragment.this.categories = categories;
            reloadtable();
        }
    }).getCategories();
}

public void reloadtable() {
    SelectionListContent.ITEMS.clear();
    Object interests = ParseUser.getCurrentUser().get("interests");
    List<HashMap<String, String>> selected = new ArrayList<HashMap<String, String>>();
    if (interests != null) {
        selected = (List<HashMap<String, String>>) interests;
    }
    for (HashMap<String, Object> obj : categories) {
        boolean found = false;
        for (HashMap<String, String> o : selected) {
            if (o.containsKey(obj.get("id").toString())) {
                found = true;
            }
        }
        SelectionListContent.Item i = new SelectionListContent.Item((String) obj.get("id"), (String) obj.get("name"), found);
        SelectionListContent.ITEMS.add(i);
    }

    mListView.destroyDrawingCache();
    mListView.setVisibility(ListView.INVISIBLE);
    mListView.setVisibility(ListView.VISIBLE);
    ((ArrayAdapter)mAdapter).notifyDataSetChanged();
}

更新1:似乎正在調用notifyDataSetChanged(),適配器中的getView()在第二次獲取數據后停止被調用。

更新2:嘗試將notifyDataSetChanged()更改為

    mAdapter = new SelectionListViewAdapter(getActivity(),
            R.layout.listviewcheckbox, SelectionListContent.ITEMS);
    mListView = (AbsListView) view.findViewById(android.R.id.list);
    ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);

它在第一行崩潰。 getActivity()在此階段為null。 如果使用片段中作為屬性保留的指針引用,則不會發生這種情況。 不知道這是否可能導致原始問題

((ArrayAdapter)mAdapter).notifyDataSetChanged();

在此之前使用此

mAdapter.clear();
mAdapter.addAll(newlist);
((ArrayAdapter)mAdapter).notifyDataSetChanged();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM