删除ListView中的ArrayAdapter <string>中的项</string>

时间:2013-08-26 12:55:12

标签: android listview android-arrayadapter

我想删除ListView中单击的项目。

  

解决方案是使用ArrayList而不是String [];

这是我的代码:

public class MyDialogFragment扩展了DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Critères Supplémentaires");
    builder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    onCancel(dialog);
                }
            }
    );

    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View mView = inflater.inflate(R.layout.fragment_dialog, null);
    ListView mListView = (ListView) mView.findViewById(R.id.lvListe);

    String[] mValues = {"Orientation", "Nombre de Chambres", "Nombre de Salle de bains", "Nombre de toilettes", "Cave", "Parking", "Garage", "Jardin"};

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mValues);
    mListView.setAdapter(adapter);
    mListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String item = (String) parent.getItemAtPosition(position);
            adapter.remove(adapter.getItem(position));
            adapter.notifyDataSetChanged();
            Toast.makeText(getActivity(), "You selected : " + item, Toast.LENGTH_SHORT).show();
        }
    });
    builder.setView(mListView);

    return builder.create();
}

但是adapter.remove()会使应用程序因此错误而崩溃:

  

java.lang.UnsupportedOperationException

如果我想要删除项目该怎么办?

提前致谢:)

2 个答案:

答案 0 :(得分:14)

      String[] mValues = {"Orientation", "Nombre de Chambres", "Nombre de Salle de bains", "Nombre de toilettes", "Cave", "Parking", "Garage", "Jardin"};

       final ArrayList<String> list =new ArrayList<String>(Arrays.asList(mValues));  
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String item = list.get(position);
                list.remove(position);
                adapter.notifyDataSetChanged();
                Toast.makeText(getActivity(), "You selected : " + item, Toast.LENGTH_SHORT).show();
            }
        });

答案 1 :(得分:6)

这样做

mListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String item = (String) adapter.getItem(position);
                adapter.remove(adapter.getItem(position));
                adapter.notifyDataSetChanged();
                Toast.makeText(getActivity(), "You selected : " + item, Toast.LENGTH_SHORT).show();
            }
        });