简体   繁体   中英

Update ListView on item delete or rename

I have the following code in a tabbed 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. 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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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