我想删除数据库表中的一行 我使用listview并在longclick listitem之后你将获得警告对话框以显示编辑和删除方法 当我点击编辑方法时,模拟器运行正常,但是当我点击删除方法时,会出现强制关闭通知,.im新增到android编程, 你能解释我的代码有什么问题吗? 这是我的代码 for database helper:
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
class PuraHelper extends SQLiteOpenHelper {
private SQLiteDatabase db;
public final String TABLE_NAME = "pura";
public final String TABLE_ROW_ID = "_id";
public final String NAMA_PURA = "nama_pura";
public final String KABUPATEN = "kabupaten";
public final String JALAN = "jalan";
public final String LONGITUDE = "longitude";
public final String LATITUDE = "latitude";
public final String KETERANGAN = "keterangan";
private static final String DATABASE_NAME="PPB.db";
private static final int SCHEMA_VERSION=1;
public PuraHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CreateTablePura="create table"+TABLE_ROW_ID+"INTEGER PRIMARY KEY AUTOINCREMENT,"+ NAMA_PURA + "TEXT," + KABUPATEN + "TEXT,"+ JALAN + "TEXT," +LONGITUDE + "TEXT," + LATITUDE + "TEXT," + KETERANGAN + "TEXT";
db.execSQL(CreateTablePura);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// no-op, since will not be called until 2nd schema
// version exists
}
public Cursor getAll() {
String SelectAll = "SELECT _id, nama_pura, kabupaten, jalan, longitude, latitude, keterangan FROM pura ORDER BY nama_pura";
return(getReadableDatabase()
.rawQuery(SelectAll,
null));
}
public Cursor getById(String id) {
String SelectById = "SELECT _id, nama_pura, kabupaten, jalan, longitude, latitude, keterangan FROM pura WHERE _ID=?";
String[] args={id};
return(getReadableDatabase()
.rawQuery(SelectById, args));
}
public void insert(String nama_pura, String kabupaten, String jalan, String longitude, String latitude, String keterangan) {
ContentValues cv=new ContentValues();
cv.put("nama_pura", nama_pura);
cv.put("kabupaten", kabupaten);
cv.put("jalan", jalan);
cv.put("longitude", longitude);
cv.put("latitude", latitude);
cv.put("keterangan", keterangan);
getWritableDatabase().insert("pura", "nama", cv);
}
public void update(String id, String nama_pura, String kabupaten, String jalan, String longitude, String latitude, String keterangan) {
ContentValues cv=new ContentValues();
String[] args={id};
cv.put("nama_pura", nama_pura);
cv.put("kabupaten", kabupaten);
cv.put("jalan", jalan);
cv.put("longitude", longitude);
cv.put("latitude", latitude);
cv.put("keterangan", keterangan);
getWritableDatabase().update("pura", cv, "_ID=?", args);
}
public void deleteRow(long rowId)
{
// ask the database manager to delete the row of given id
//db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowId, null);
db.delete(TABLE_NAME, TABLE_ROW_ID + "="+rowId,null);
}
public String getId(Cursor c) {
return(c.getString(0));
}
public String getNama(Cursor c) {
return(c.getString(1));
}
public String getKab(Cursor c) {
return(c.getString(2));
}
public String getJalan(Cursor c) {
return(c.getString(3));
}
public String getLong(Cursor c) {
return(c.getString(4));
}
public String getLat(Cursor c) {
return(c.getString(5));
}
public String getKet(Cursor c) {
return(c.getString(6));
}
我的java类:
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemLongClickListener;
public class db_list extends ListActivity {
protected static final String ID_EXTRA = "com.jigler.ppb._ID";
PuraHelper helper=null;
Cursor model=null;
PuraAdapter adapter=null;
EditText etNama, etKabupaten,etJalan, etLong, etLat, etKeterangan;
Button bSave, bCancel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_list);
helper=new PuraHelper(this);
etNama = (EditText)findViewById(R.id.etNama);
etKabupaten = (EditText)findViewById(R.id.etKabupaten);
etJalan = (EditText)findViewById(R.id.etJalan);
etLong = (EditText)findViewById(R.id.etLong);
etLat = (EditText)findViewById(R.id.etLat);
etKeterangan = (EditText)findViewById(R.id.etKeterangan);
model=helper.getAll();
startManagingCursor(model);
adapter=new PuraAdapter(model);
setListAdapter(adapter);
ListView lv = getListView();
lv.setLongClickable(true);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> list, View view,
int position, long id) {
// TODO Auto-generated method stub
alert_editlist(id);
return true;
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
helper.close();
}
@Override
public void onListItemClick(ListView list, View view, int position, long id) {
Intent i=new Intent(db_list.this, db_new_man.class);
i.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(i);
}
private void alert_editlist(final long id) {
// TODO Auto-generated method stub
final CharSequence[] items = {"Edit", "Delete"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("What do you want to do?");
builder.setIcon(R.drawable.ic_setting_36);
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item]=="Edit") {
Intent i=new Intent(db_list.this, db_new_man.class);
i.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(i);
}
else if(items[item]=="Delete"){
//this is where my app force close
helper.deleteRow(id);
model.requery();
}
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.option, menu);
return(super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.add) {
//startActivity(new Intent(db_list.this, db_new.class));
alert_db_new();
return(true);
}
return(super.onOptionsItemSelected(item));
}
private void alert_db_new() {
// TODO Auto-generated method stub
final CharSequence[] items = {"Auto", "Manual"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Use Your Configuration Setting By");
builder.setIcon(R.drawable.ic_setting_36);
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (items[item]=="Auto") {
startActivity(new Intent(db_list.this, db_new_auto.class));
}
else if(items[item]=="Manual"){
startActivity(new Intent(db_list.this, db_new_man.class));
}
finish();
}
});
AlertDialog alert = builder.create();
alert.show();
}
/*
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
helper.insert
(
etNama.getText().toString(),
etKabupaten.getText().toString(),
etJalan.getText().toString(),
etLong.getText().toString(),
etLat.getText().toString(),
etKeterangan.getText().toString()
);
model.requery();
}
};
*/
class PuraAdapter extends CursorAdapter {
PuraAdapter(Cursor c) {
super(db_list.this, c);
}
@Override
public void bindView(View row, Context ctxt, Cursor c) {
// TODO Auto-generated method stub
PuraHolder holder=(PuraHolder)row.getTag();
holder.populateFrom(c, helper);
}
@Override
public View newView(Context ctxt, Cursor c, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
PuraHolder holder=new PuraHolder(row);
row.setTag(holder);
return(row);
}
}
static class PuraHolder {
private TextView nama=null;
private TextView alamat=null;
private ImageView icon=null;
PuraHolder(View row) {
nama=(TextView)row.findViewById(R.id.nama_pura);
alamat=(TextView)row.findViewById(R.id.jalan);
icon=(ImageView)row.findViewById(R.id.icon_pura);
}
void populateFrom(Cursor c, PuraHelper helper) {
nama.setText(helper.getNama(c));
alamat.setText(helper.getJalan(c));
icon.setImageResource(R.id.icon_pura);
}
}
}
谢谢你的关注
答案 0 :(得分:6)
我认为您没有在代码中实例化“db”。
使用
getWritableDatabase().delete(TABLE_NAME, TABLE_ROW_ID + "="+rowId,null)
而不是
db.delete(TABLE_NAME, TABLE_ROW_ID + "="+rowId,null)
可能会有所帮助。
答案 1 :(得分:0)
将其设置为ListView LongClickListener。您也可以尝试创建一个类似于http://www.devx.com/wireless/Article/40842/1954
的DBAdapter助手类private OnItemLongClickListener longClickListener = new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
final long ITEM = id;
// Ask the user if they want to delete
AlertDialog.Builder confirmation = new AlertDialog.Builder(view.getContext());
confirmation.setIcon(android.R.drawable.ic_dialog_alert);
confirmation.setButton(POSITIVE_BUTTON, "Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try {
db.open();
db.deleteItem(ITEM);
updateList();
} catch (Exception ex) { }
finally {
db.close();
}
});
confirmation.setButton(NEGATIVE_BUTTON, "Do nothing", null);
confirmation.setButton(NEURTRAL_BUTTON, "Edit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(view.getContext(), EditActivity.class);
intent.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(intent);
}
});
confirmation.show();
return false;
};