猿问

在notifyDataSetChanged之后Android ListView没有刷新

我的ListFragment代码


public class ItemFragment extends ListFragment {


    private DatabaseHandler dbHelper;

    private static final String TITLE = "Items";

    private static final String LOG_TAG = "debugger";

    private ItemAdapter adapter;

    private List<Item> items;



    @Override

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

        View view = inflater.inflate(R.layout.item_fragment_list, container, false);        

        return view;

    }


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.setHasOptionsMenu(true);

        super.onCreate(savedInstanceState);

        getActivity().setTitle(TITLE);

        dbHelper = new DatabaseHandler(getActivity());

        items = dbHelper.getItems(); 

        adapter = new ItemAdapter(getActivity().getApplicationContext(), items);

        this.setListAdapter(adapter);


    }




    @Override

    public void onResume() {

        super.onResume();

        items.clear();

        items = dbHelper.getItems(); //reload the items from database

        adapter.notifyDataSetChanged();

    }


    @Override

    public void onListItemClick(ListView l, View v, int position, long id) {

        super.onListItemClick(l, v, position, id);

        if(dbHelper != null) { //item is edited

            Item item = (Item) this.getListAdapter().getItem(position);

            Intent intent = new Intent(getActivity(), AddItemActivity.class);

            intent.putExtra(IntentConstants.ITEM, item);

            startActivity(intent);

        }

    }

}


有人可以帮忙吗?


慕容森
浏览 727回答 3
3回答

精慕HU

看看你的onResume方法ItemFragment:@Overridepublic void onResume() {&nbsp; &nbsp; super.onResume();&nbsp; &nbsp; items.clear();&nbsp; &nbsp; items = dbHelper.getItems(); // reload the items from database&nbsp; &nbsp; adapter.notifyDataSetChanged();}你刚刚调用之前更新的notifyDataSetChanged()不是适配器的字段,private List<Item> items;而是片段的相同声明的字段。适配器仍然存储对您在创建适配器时传递的项目列表的引用(例如,在fragment的onCreate中)。最短(在变化的数量意义上)但不是优雅的方式使您的代码按预期运行只是替换行:&nbsp; &nbsp; items = dbHelper.getItems(); // reload the items from database同&nbsp; &nbsp; items.addAll(dbHelper.getItems()); // reload the items from database更优雅的解决方案:1)private List<Item> items;从中删除项目ItemFragment- 我们只需要在适配器中继续引用它们2)将onCreate改为:@Overridepublic void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; super.setHasOptionsMenu(true);&nbsp; &nbsp; getActivity().setTitle(TITLE);&nbsp; &nbsp; dbHelper = new DatabaseHandler(getActivity());&nbsp; &nbsp; adapter = new ItemAdapter(getActivity(), dbHelper.getItems());&nbsp; &nbsp; setListAdapter(adapter);}3)在ItemAdapter中添加方法:public void swapItems(List<Item> items) {&nbsp; &nbsp; this.items = items;&nbsp; &nbsp; notifyDataSetChanged();}4)将你的onResume更改为:@Overridepublic void onResume() {&nbsp; &nbsp; super.onResume();&nbsp; &nbsp; adapter.swapItems(dbHelper.getItems());}

梵蒂冈之花

您将重新加载的项目分配给全局变量项目onResume(),但这不会反映在ItemAdapter类中,因为它有自己的实例变量名为“items”。要刷新ListView,请在ItemAdapter类中添加一个refresh(),该类接受列表数据,即项目class ItemAdapter{&nbsp; &nbsp; .....&nbsp; &nbsp; public void refresh(List<Item> items)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.items = items;&nbsp; &nbsp; &nbsp; &nbsp; notifyDataSetChanged();&nbsp; &nbsp; }&nbsp;}onResume()使用以下代码更新@Overridepublic void onResume(){&nbsp; &nbsp; super.onResume();&nbsp; &nbsp; items.clear();&nbsp; &nbsp; items = dbHelper.getItems(); //reload the items from database&nbsp; &nbsp; **adapter.refresh(items);**}

MMMHUHU

在onResume()中更改此行items = dbHelper.getItems(); //reload the items from database至items.addAll(dbHelper.getItems()); //reload the items from database问题是你永远不会告诉适配器有关新项目列表的信息。如果您不想将新列表传递给适配器(因为您似乎没有),那么只需items.addAll在您的适配器之后使用clear()。这将确保您修改适配器引用的相同列表。
随时随地看视频慕课网APP

相关分类

Android
我要回答