gpt4 book ai didi

java - 如何从 ListView 中删除选定的项目?

转载 作者:行者123 更新时间:2023-12-02 08:09:51 26 4
gpt4 key购买 nike

对于这种困惑,我们深表歉意:更新的问题:我正在尝试从 ListView 中删除列表项。单击该项目时,将显示警报对话框。如果我单击“确定”,则必须从 ListView 中删除所选项目。我的代码如下:

case R.id.lvinc:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete Event ");

builder.setMessage("Delete this Event ?");
builder.setPositiveButton("Ok, Delete",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
try
{
???? //What code to delete the selected list item?




}catch(Exception e)
{
e.printStackTrace();
}
}
});

AlertDialog alert = builder.create();
alert.show();
displaylist();
break;

非常感谢任何帮助,并提前致谢...

最佳答案

我尝试解决它并得到了一个解决方案,请查看下面的代码:

listview.java

public class listview extends Activity implements OnItemClickListener{

ListView list;
ListAdapter adapter;
ArrayList<String> nameArray;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//xml should have an ListView element
nameArray = new ArrayList<String>();
nameArray.add("Item1");
nameArray.add("Item2");
nameArray.add("Item3");
nameArray.add("Item4");
nameArray.add("Item5");
list = (ListView) findViewById(R.id.listView);
list.setOnItemClickListener(listview.this);
adapter=new ListAdapter(listview.this, nameArray);
list.setAdapter(adapter);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
showDialog(arg2);
}
@Override
protected Dialog onCreateDialog(final int id) {
Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Delete Event")
.setCancelable(true)
.setPositiveButton("Ok, Delete",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
nameArray.remove(id);
adapter=new ListAdapter(listview.this, nameArray);
list.setAdapter(adapter);
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});
AlertDialog dialog = builder.create();
dialog.show();
return super.onCreateDialog(id);
}
}

//适配器类

ListAdapter.java

public class ListAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<String> name;
private static LayoutInflater inflater=null;

public ListAdapter(Activity a, ArrayList<String> nameArray) {
activity = a;
name = nameArray;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

public int getCount() {
return name.size();
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public static class ViewHolder{
public TextView text;
}

public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.list_item, null);
holder=new ViewHolder();
holder.text=(TextView)vi.findViewById(R.id.title);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();
holder.text.setText(name.get(position));
return vi;
}
}

关于java - 如何从 ListView 中删除选定的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7633135/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com