在Listview
删除的最后一个选项中,当我应用选项菜单时,删除任何列表项的选项,它只删除了最后一项我现在可以做什么?选择编辑选项时会发生类似的问题,它会选择或获取最后一项的信息,
final CharSequence[] gender = {"Edit","Delete"};
alert = new AlertDialog.Builder(StikiNotesActivity.this);
final AlertDialog.Builder alert_del = new AlertDialog.Builder(StikiNotesActivity.this);
alert_del.setTitle("Confirmation !");
alert.setTitle("Options");
alert.setItems(gender,new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if(which==0)
{
Toast.makeText(getApplicationContext(), "EditOpetion"+alist.get(0).sid, Toast.LENGTH_SHORT).show();
final Intent i = new Intent(StikiNotesActivity.this,FirstClass.class);
//String txt=new String();
i.putExtra("id",""+alist.get(position).sid);
startActivity(i);
}
else if(which==1)
{
AlertDialog.Builder alertDialog=new AlertDialog.Builder(StikiNotesActivity.this);
alertDialog.setTitle("Selected Subject will be deleted");
alertDialog.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
alist.remove(position);
adap.notifyDataSetChanged();
dialog.cancel();
}
});
alertDialog.setNegativeButton("Cancel", null);
alertDialog.show();
}
}});
请帮我解决这个问题?
答案 0 :(得分:0)
你可以这样做
1 - 在删除AlertDialog中单击“确定”按钮后,您必须获得item position
,我建议您必须实施onItemClickListener
yourListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
alist.remove(position);
adap.notifyDataSetChanged();
}
});
2 - 我建议在你的AlertDialog中添加editText
,如下所示
else if(which==1){
AlertDialog.Builder alertDialog=new AlertDialog.Builder(StikiNotesActivity.this);
EditText txtPosition = new EditText(YourContext);
alertDialog.setView(txtPosition);
alertDialog.setTitle("Selected Subject will be deleted");
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//get the position from txtPosition
int position = Integer.parseInt(txtPosition.getText().toString());
alist.remove(position);
adap.notifyDataSetChanged();
dialog.cancel();
}
});
alertDialog.setNegativeButton("Cancel", null);
alertDialog.show();
}
希望这能帮到你