0

当我按下按钮两次时,我试图阻止我的列表视图项目附加。我尝试使用 .Clear() 但我不知道将它放在哪里或如何放置,有时它甚至会阻止项目甚至填充或根本不清除项目。

全球的东西:

ArrayAdapter<CharSequence> adapter;
ArrayList<MessageDetails> details;

我的按钮:

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            try
            {
                //Tried clearing adapter first before code runs but items do not even get displayed
                adapter.clear();
                adapter.notifyDataSetChanged();
                //sendPostData calls refreshListView method
                new sendPostData().execute(nric.getText().toString());
            }
            catch (Exception e)
            {

            }
        }

我的 refreshListView() 方法:

private void refreshListView()
{       
    adapter = new ArrayAdapter<CharSequence>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, items);
    myInfoList.setAdapter(adapter);

    details = new ArrayList<MessageDetails>();
    MessageDetails Detail;

    Detail = new MessageDetails();

    for (int i =0; i < itemName.size(); i++)
    {
        float totalprice = itemPrice.get(i) * Quantity.get(i);
        Detail = new MessageDetails();
        Detail.setitemName(itemName.get(i));
        Detail.setItemDescription(description.get(i));
        Detail.setitemPrice(String.valueOf(totalprice));
        Detail.setQuantity(Quantity.get(i).toString() + " sets");
        Detail.setDate(purchaseDate.get(i).toString());
        details.add(Detail);
    }   

    myInfoList.setAdapter(new CustomAdapter(details , getActivity()));
}

编辑删除了第一个适配器。

全局项目:

CustomAdapter mCustomAdapter;

刷新列表视图:

private void refreshListView()
{           
    details = new ArrayList<MessageDetails>();
    MessageDetails Detail;

    Detail = new MessageDetails();

    for (int i =0; i < itemName.size(); i++)
    {
        float totalprice = itemPrice.get(i) * Quantity.get(i);
        Detail = new MessageDetails();
        Detail.setitemName(itemName.get(i));
        Detail.setItemDescription(description.get(i));
        Detail.setitemPrice(String.valueOf(totalprice));
        Detail.setQuantity(Quantity.get(i).toString() + " sets");
        Detail.setDate(purchaseDate.get(i).toString());
        details.add(Detail);
    }   

    mCustomAdapter = new CustomAdapter(details , getActivity());
    myInfoList.setAdapter(mCustomAdapter);
}
4

2 回答 2

0

这对我有用:

        listview.removeAllViewsInLayout();
        listview.removeAllViewsFromAdapter();
        listview.setAdapter(null);
        listview.refreshDrawableState();
        listview.removeViews(0, listview.getChildCount());
于 2013-07-08T09:06:02.377 回答
0

您向您的 提交两个不同的Adapter实例ListView,后者覆盖前一个实例。因此,您应该保留对数据集的引用CustomAdapter并编写一个方法来清除数据集。

CustomAdapter mCustomAdpater = new CustomAdapter(details , getActivity()); 
myInfoList.setAdapter(mCustomAdpater); 

在 CustomAdapter 中,编写自己的clear()方法,然后在里面onClick()调用

mCustomAdpater.clear();
于 2013-07-08T09:03:12.110 回答