9

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

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

这是我的代码:

公共类 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

如果我想删除一个项目,我应该怎么做?

提前致谢 :)

4

2 回答 2

17
      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();
            }
        });
于 2013-08-26T13:06:44.700 回答
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();
            }
        });
于 2013-08-26T13:04:23.257 回答