我的问题是:如何将项目位置传递给另一个班级。请参阅我附带的代码。
当我在AlertDialog中按下正按钮时,我转到HandleAlertDialog类,然后我按ID删除Name(在数据库中我有两个变量:id和name)。
我不知道如何在HandleAlertDialog类中将id从项目位置传递到sqhelper.deleteNameByNumber( id )。
感谢您的帮助。
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog dialog = new AlertDialog.Builder(this)
.setMessage("Choose one of the options")
.setPositiveButton("Yes", new HandleAlertDialog())
.setNeutralButton("No",new HandleAlertDialog())
.setCancelable(false)
.create();
dialog.show();
return false;
}
public class HandleAlertDialog implements DialogInterface.OnClickListener {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which==-1){
sqhelper.deleteNameByNumber(**???**);
}
}
}
答案 0 :(得分:1)
您可以在id
类中定义HandleAlertDialog
属性(我假设这是String
我的例子):
public class HandleAlertDialog implements DialogInterface.OnClickListener {
private String id;
public HandleAlertDialog(String id){
this.id = id;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which==-1){
sqhelper.deleteNameByNumber(id);
}
}
}
然后在onItemLongClick
:
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
// Retrieve id here (based on view, position ?)
String id = "THIS IS YOUR ID";
HandleAlertDialog handleAlertDialog = new HandleAlertDialog(id);
AlertDialog dialog = new AlertDialog.Builder(this)
.setMessage("Choose one of the options")
.setPositiveButton("Yes", handleAlertDialog)
.setNeutralButton("No", handleAlertDialog)
.setCancelable(false)
.create();
dialog.show();
return false;
}
答案 1 :(得分:0)
只需在构造函数中传递id。
public boolean onItemLongClick (AdapterView parent, View view,int position, final long id){
AlertDialog dialog = new AlertDialog.Builder(this)
.setMessage("Choose one of the options")
.setPositiveButton("Yes", new HandleAlertDialog(id))
.setNeutralButton("No", new HandleAlertDialog(id))
.setCancelable(false)
.create();
dialog.show();
return false;
}
添加构造函数。
public class HandleAlertDialog implements DialogInterface.OnClickListener
{
long id;
public HandleAlertDialog(long id) {
this.id = id;
}
@Override
public void onClick(DialogInterface dialog, int which) {
if (which==-1){
sqhelper.deleteNameByNumber(id);
}
}
}
如果不清楚,请告诉我。