使用SQLite在Android ListView中删除一行

时间:2014-02-25 15:55:57

标签: android sqlite listview android-listview android-sqlite

我试图在我的SQLite数据库中获取一行,以便在长按时删除。我成功地在ListView中删除了一行,但每次尝试此代码时都会崩溃应用程序。我在这里尝试了很多解决方案,但没有取得任何成功。我也试过了:

menuItems.remove(0);

adapter.notifyDataSetChanged();

这只是删除ListView中的项而不是数据库中的行。

这是我的ListActivity:

public class showUserInfoListActivity extends ListActivity {

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
static final String KEY_ID         = "id";
static final String KEY_COUPON_NAME = "couponName";
static final String KEY_COUPON_INFO= "couponInfo";
private static final String DATABASE_TABLE = "UserInfos";
public static final String KEY_ROWID = "id";
ListView l;
long rowId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_user_info_list);

        //---get all Records---
    final DataBaseAdapter db = new DataBaseAdapter(this);
    db.open();
    Cursor c = db.getAllRecords();
    if (c.moveToFirst())
    {
        do 
        {          
            HashMap<String, String> map = new HashMap<String, String>();
            // adding each child node to HashMap key => value
            map.put(KEY_ID, c.getString(0));
            map.put(KEY_COUPON_NAME, c.getString(1));
            map.put(KEY_COUPON_INFO, c.getString(2));

            // adding HashList to ArrayList
            menuItems.add(map);
        } while (c.moveToNext());
    }
    db.close();

    // Adding menuItems to ListView
    // All filed data are not shown in the list KEY_ID is hidden
    final BaseAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.user_info_list_item,
                          new String[] { KEY_COUPON_NAME, KEY_COUPON_INFO, KEY_ID }, 
                          new int[] {R.id.coupon_name, R.id.coupon_info});
    setListAdapter(adapter);

    getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(showUserInfoListActivity.this,"Longpressed", Toast.LENGTH_LONG).show();
            SQLiteDatabase db= this.getWritableDatabase(); 
            db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null);
            return true;

        }

        private SQLiteDatabase getWritableDatabase() {
            // TODO Auto-generated method stub
            return null;
        }
    });

}

//On select from the list show data
protected void onListItemClick(ListView l, View v, int position, long id) 
{
    super.onListItemClick(l, v, position, id);
    Uri uri = Uri.parse("http://replyrewards.com/m/coupon_user.html");
     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     startActivity(intent);
}
 }

这是我的数据库适配器:

  public class DataBaseAdapter {
public static final String KEY_ROWID = "id";
     public static final String KEY_COUPON_NAME = "couponName";
     public static final String KEY_COUPON_INFO= "couponInfo";
     private static final String TAG = "DBAdapter";

private static final String DATABASE_NAME = "UserInfoDB";
private static final String DATABASE_TABLE = "UserInfos";
private static final int DATABASE_VERSION = 2;

private static final String DATABASE_CREATE =
    "create table if not exists UserInfos (id integer primary key autoincrement, "
    + "couponName VARCHAR not null, couponInfo VARCHAR );";

private final Context context;    

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DataBaseAdapter(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        try {
            db.execSQL(DATABASE_CREATE);    
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS contacts");
        onCreate(db);
    }
}    

//---opens the database---
public DataBaseAdapter open() throws SQLException 
{
    db = DBHelper.getWritableDatabase();
    return this;
}

//---closes the database---    
public void close() 
{
    DBHelper.close();
}

//---insert a record into the database---
public long insertRecord(String couponName, String couponInfo) 
{
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_COUPON_NAME, couponName);
    initialValues.put(KEY_COUPON_INFO, couponInfo);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

//---deletes a particular record---
public boolean deleteRecord(long rowId) 
{
    return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}

//---retrieves all the records---
public Cursor getAllRecords() 
{
    return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_COUPON_NAME,
            KEY_COUPON_INFO}, null, null, null, null, null);
}

//---retrieves a particular record---
public Cursor getRecord(long rowId) throws SQLException 
{
    Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                    KEY_COUPON_NAME, KEY_COUPON_INFO }, 
            KEY_ROWID + "=" + rowId, null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

//---updates a record---
public boolean updateRecord(long rowId, String couponName, String couponInfo) 
{
    ContentValues args = new ContentValues();
    args.put(KEY_COUPON_NAME, couponName);
    args.put(KEY_COUPON_INFO, couponInfo);
    return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}

0 个答案:

没有答案