Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SQLite删除Android ListView中的一行_Android_Sqlite_Listview_Android Listview_Android Sqlite - Fatal编程技术网

使用SQLite删除Android ListView中的一行

使用SQLite删除Android ListView中的一行,android,sqlite,listview,android-listview,android-sqlite,Android,Sqlite,Listview,Android Listview,Android Sqlite,我试图在长时间单击后在SQLite数据库中删除一行。我成功地在ListView中删除了一行,但每次我尝试这段代码时,应用程序都会崩溃。我在这里尝试了许多解决方案,但都没有成功。我也尝试过: menuItems.remove0 adapter.notifyDataSetChanged 只删除ListView中的项,而不是数据库中的行 以下是我的列表活动: public class showUserInfoListActivity extends ListActivity { ArrayList&

我试图在长时间单击后在SQLite数据库中删除一行。我成功地在ListView中删除了一行,但每次我尝试这段代码时,应用程序都会崩溃。我在这里尝试了许多解决方案,但都没有成功。我也尝试过:

menuItems.remove0

adapter.notifyDataSetChanged

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

以下是我的列表活动:

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);
}
 }

只需阅读记事本教程练习1 LogCat中显示的POST异常消息
  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;
}
}