Android如何从listview中删除列表项?

时间:2014-01-27 17:45:18

标签: android android-listview

我有一个目录中的文件夹列表,onListItemClick事件调用show alert对话框,然后单击是按钮我处理删除文件。我想在成功删除文件后,还要从列表中删除所选项目或使用新值刷新视图。

我怎样才能做到最简单?

包裹活动代码:

public class PackageListActivity extends ListActivity
{
    // create instance of App helper class
    AppHelper helper = new AppHelper();
    String folderNameToDelete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // get arrayListOf folders
        ArrayList<String> folderArrayList =  helper.getListOfFileInDirectory(null);
        // convert array listo to simple array
        String[] arr = folderArrayList.toArray(new String[folderArrayList.size()]);
        // set to aray adapter
        setListAdapter(new PackageArrayAdapter(this, arr));
    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        //get selected items
        String selectedValue = (String) getListAdapter().getItem(position);
        //Toast.makeText(this, selectedValue, Toast.LENGTH_SHORT).show();
        this.showAlerDialog(selectedValue);
    }

1 个答案:

答案 0 :(得分:0)

通过给出要删除的位置来删除arraylist项,然后使用适配器调用notifyDataSetChanged()方法

folderArrayList.remove(<index of element to remove>);
PackageArrayAdapter.notifyDataSetChanged();