这是ma program.in这个程序在删除按钮那里没有执行onclick听众请解决这个问题或如何获得特定项目的ID。
delete = (Button) findViewById(R.id.delete);
delete.setOnClickListener(this);
if(v.equals(delete))
{
new AlertDialog.Builder(ShareFolioEditActivity.this)
.setTitle("Delete")
.setMessage("Are you sure ??? ")
.setNeutralButton("no",null)
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
delete();
}
}) .show();
}
}
public void delete()
{
db.delete("sharelist", "_id="+ID, null);
Toast.makeText(this, "row deleted"+ID, Toast.LENGTH_SHORT).show();
Bundle b=null;
onCreate(b);
}
这是我的xml布局
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="10sp">
<TextView
android:id="@+id/category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_toRightOf="@+id/catagory"
/>
</RelativeLayout>
请告诉我解决方案
由于
答案 0 :(得分:3)
当您将this
设置为按钮的onClick
侦听器时,您还必须在活动中实施onClick
方法。在您的情况下,将永远不会调用运行对话框并执行删除操作的代码。
将代码重构为:
delete = (Button) findViewById(R.id.delete);
delete.setOnClickListener(this);
}
public void onClick(View v)
{
if(v.equals(delete))
{
new AlertDialog.Builder(ShareFolioEditActivity.this)
.setTitle("Delete")
.setMessage("Are you sure ??? ")
.setNeutralButton("no",null)
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
delete();
}}).show();
}
}
public void delete()
{
db.delete("sharelist", "_id="+ID, null);
Toast.makeText(this, "row deleted"+ID, Toast.LENGTH_SHORT).show();
Bundle b=null;
onCreate(b);
}
注意:我没有检查过这段代码,它可能无法编译,但它会给你一个想法。