how can i delete an item from listview and also database
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);
}
this is my xml layout
<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"
开发者_JAVA技巧android:text=""
android:layout_toRightOf="@+id/catagory"
/>
</RelativeLayout>
please tell me solution
Thanks
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.
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);
}
Note: I've not checked this code, it might not compile, but it gives you an idea.
精彩评论