如何从列表视图,并删除数据库中的一个项目 [英] how can i delete an item from listview and also database
问题描述
这是马program.in这个程序删除按钮有剪掉执行的onclick侦听器,请解决这个问题,或者我怎样才能获得一个特定项目的ID。
this is ma program.in this program on delete button there didnt perform onclick listener please solve this problem or how can i get the id of an particular item.
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>
请告诉我的解决方案
感谢
推荐答案
当您设置这个
为的onClick
监听器按钮,还必须实现您的活动中的的onClick
方法。运行你的对话框,并执行删除操作的code将永远不会被调用你的情况。
When you set this
as the onClick
listener for a button, you must also implement the onClick
method within your activity. The code that runs your dialog and performs the delete action will never be called in your case.
重新构建code到是这样的:
Refactor the code to something like:
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);
}
请注意:我还没有选中此code,它可能无法编译,但它给你一个想法
Note: I've not checked this code, it might not compile, but it gives you an idea.
这篇关于如何从列表视图,并删除数据库中的一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!