简体   繁体   English

更新ListView上的项目删除或重命名

[英]Update ListView on item delete or rename

I have the following code in a tabbed Fragment : 我在选项卡式Fragment中包含以下代码:

    private SetRowsCustomAdapter adapter;
private ArrayList<SetRows> rowsArray = new ArrayList<SetRows>();

    for (Map.Entry<String, String> current_file:filesInFolder.entrySet()) {
     rowsArray.add(new SetRows(R.drawable.ic_launcher, current_file.getKey().toString(), current_file.getValue().toString()));
        }
         adapter = new SetRowsCustomAdapter(getActivity(), R.layout.customlist, rowsArray);
     dataList = (ListView) mFrame3.findViewById(R.id.lvFiles);
     dataList.setAdapter(adapter);
     }

getActivity().deleteFile(txt + ".trp");
adapter.remove(adapter.getItem(info.position));
adapter.notifyDataSetChanged();
startActivity(new Intent(getActivity(), MainActivity.class).putExtra("tab", 2));

which deletes a row within my ListView and starts a new activity to show the changes. 它会删除ListView的一行并启动一个新活动以显示更改。 Although I have the adapter.notifyDataSetChanged() , it doesn't display the changes once a row is deleted without the last line on the code above. 尽管我有adapter.notifyDataSetChanged() ,但是如果删除了没有上面代码最后一行的行,它就不会显示更改。

Can someone please help me fix that so it shows the changes without starting a new activity. 有人可以帮助我解决此问题,以便在不启动新活动的情况下显示更改。

Use of custom adapter is better for it, these link will help you for it Remove ListView items in Android How to remove arraylist item and then update listview 使用自定义适配器比较合适,这些链接将为您提供帮助。 在Android中删除ListView项。 如何删除arraylist项,然后更新listview

I think you require to delete from arraylist then call adapter setNotifyDataChange(), b'cos when you call this function it reload the adapter from arraylist. 我认为您需要从arraylist中删除,然后在调用此函数时调用适配器setNotifyDataChange(),b'cos,它会从arraylist重新加载适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM