删除 ListView 中 ArrayAdapter<String> 中的项目 [重复]
Posted
技术标签:
【中文标题】删除 ListView 中 ArrayAdapter<String> 中的项目 [重复]【英文标题】:Remove item in ArrayAdapter<String> in ListView [duplicate] 【发布时间】:2013-08-28 23:23:50 【问题描述】:我想删除 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
如果我想删除一个项目我应该怎么做?
提前致谢:)
【问题讨论】:
【参考方案1】: 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();
);
【讨论】:
【参考方案2】:这样做
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();
);
【讨论】:
以上是关于删除 ListView 中 ArrayAdapter<String> 中的项目 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
单击 ListView 上的删除按钮后删除 ListView 上的项目