我无法弄清楚如何从列表视图和数据库中删除列表

时间:2017-05-19 17:58:48

标签: java sql listview android-studio-2.3

这是我目前未能删除列表的尝试。所选列表似乎已被删除,但一旦重新启动应用程序,已删除的列表将再次出现在我的列表视图中。我现在已经挣扎了大约一个小时了。我是Android Studio或Android开发的新手

这是我的DataBaseHelper类

public DatabaseHelper(Context context){super(context,DATABASE_NAME,null,1);}
SQLiteDatabase db = this.getWritableDatabase();
@Override
public void onCreate(SQLiteDatabase db) {
    String createTable= "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "ITEM1 TEXT)";
    db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
    }

public boolean addData(String item1){

    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, item1);

    long result = db.insert(TABLE_NAME, null, contentValues);

    if (result== -1){
        return false;
    } else {
        return true;

    }
}

public Cursor getListContents(){
    SQLiteDatabase db= this.getWritableDatabase();
    Cursor data = db.rawQuery("SELECT * FROM  " + TABLE_NAME, null);
    return data;
}



public DatabaseHelper(Context context){super(context,DATABASE_NAME,null,1);}
SQLiteDatabase db = this.getWritableDatabase();
@Override
public void onCreate(SQLiteDatabase db) {
    String createTable= "CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "ITEM1 TEXT)";
    db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP IF TABLE EXISTS " + TABLE_NAME);
    }

public boolean addData(String item1){

    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, item1);

    long result = db.insert(TABLE_NAME, null, contentValues);

    if (result== -1){
        return false;
    } else {
        return true;

    }
}

public Cursor getListContents(){
    SQLiteDatabase db= this.getWritableDatabase();
    Cursor data = db.rawQuery("SELECT * FROM  " + TABLE_NAME, null);
    return data;
}

public boolean deleteRow(long RowId){
   return  db.delete(TABLE_NAME,COL1 + "=" + RowId,null)>0;
}

}

我的主要课程

DatabaseHelper myDB;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
protected  void onCreate( Bundle saveInstanceState){
    super.onCreate(saveInstanceState);
    setContentView(listview_item);

    final ListView listView= (ListView) findViewById(R.id.listView);
    myDB = new DatabaseHelper(this);

    final ArrayList<String> theList= new ArrayList<>();
    Cursor data = myDB.getListContents();
    final ArrayAdapter listAdapter= new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, theList);



    listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                                       int position, long id) {
              myDB.deleteRow(id);
            theList.remove(position);
            listAdapter.notifyDataSetChanged();

            Toast.makeText(ViewListContents.this, "Deleted", Toast.LENGTH_LONG).show();
            return true;
        }

    });


    if (data.getCount()==0){
        Toast.makeText(ViewListContents.this, "Database was empty", Toast.LENGTH_LONG).show();
    } else {
        while (data.moveToNext()) {
            theList.add(data.getString(1));
            listView.setAdapter(listAdapter);
        }
        Collections.reverse(theList);
    }





    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ViewListContents.this, Note.class);
                startActivity(intent);
            }
        });


}



@Override
public boolean onOptionsItemSelected(MenuItem item){
    // Take appropriate action for each action item click<br />
    switch (item.getItemId()) {
        case R.id.action_credits:
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

}

0 个答案:

没有答案