2016-05-02 198 views
0

我想從listView和SQLite數據庫中用contextMenu刪除項目(按住項目並按住,刪除按鈕出現,按下它和項目刪除),我的代碼不刪除任何東西。我嘗試按下刪除時,出現文本烤麪包,它工作。從SQLite和listView刪除一個項目

DBAdapter.java

public void delete(String name)throws SQLException { 
     SQLiteDatabase db = helper.getWritableDatabase(); 
     if (db == null) { 
      return; 
     } 
     String[] whereArgs = new String[] { name }; 
     db.delete("m_TB", "NAME"+ "=?", whereArgs); 
     db.close(); 
    } 

MainActivity.java

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     menu.add("Delete"); 
    } 

    public boolean onContextItemSelected(MenuItem item) { 
     super.onContextItemSelected(item); 
     AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 
     String name = info.toString(); 
     if (item.getTitle().equals("Delete")) { 
      db.delete(name); 
      books.remove(item); 
      adapter.notifyDataSetChanged(); 

     } 
     return true; 
    } 

全碼:

MainActivity.java

public class MainActivity extends AppCompatActivity { 


    ListView lv; 
    EditText nameTxt; 
    Button savebtn, retrievebtn; 
    ArrayList<String> books = new ArrayList<String>(); 
    ArrayAdapter<String> adapter; 
    SearchView sv; 
    /** 
    * ATTENTION: This was auto-generated to implement the App Indexing API. 
    * See https://g.co/AppIndexing/AndroidStudio for more information. 
    */ 
     private GoogleApiClient client; 

     final DBAdapter db = new DBAdapter(this); 

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

      nameTxt = (EditText) findViewById(R.id.editText); 

      savebtn = (Button) findViewById(R.id.saveBtn); 
      retrievebtn = (Button) findViewById(R.id.retrieveBtn); 

      lv = (ListView) findViewById(R.id.listView1); 

      adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, books); 



      registerForContextMenu(lv); 

      savebtn.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        db.openDB(); 
        long result = db.add(nameTxt.getText().toString()); 

        if (result > 0) { 
         nameTxt.setText(""); 
        } else { 
         Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_SHORT).show(); 
        } 
        db.close(); 
       } 
      }); 
      retrievebtn.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        books.clear(); 
        db.openDB(); 
        Cursor c = db.getAllNames(); 

        while (c.moveToNext()) { 
         String colIndex = c.getString(1); 
         books.add(colIndex); 
        } 
        lv.setAdapter(adapter); 
        db.close(); 
       } 
      }); 
      lv.setOnItemClickListener(new OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
        Toast.makeText(getApplicationContext(), books.get(position), Toast.LENGTH_SHORT).show(); 
       } 
      }); 
      // ATTENTION: This was auto-generated to implement the App Indexing API. 
      // See https://g.co/AppIndexing/AndroidStudio for more information. 
      client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 



    } 
     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 
      super.onCreateContextMenu(menu, v, menuInfo); 
      menu.add("Delete"); 
     } 

     public boolean onContextItemSelected(MenuItem item) { 
      super.onContextItemSelected(item); 
      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); 
      String name = info.toString(); 
      if (item.getTitle().equals("Delete")) { 
       books.remove(info.position); 
       adapter.notifyDataSetChanged(); 

      } 
      return true; 
     } 
    } 

DBAdapter.java

public class DBAdapter { 

    static final String ROW_ID ="id"; 
    static final String NAME ="name"; 
    static final String TAG = "DBAdapter"; 

    static final String DBNAME="m_DB"; 
    static final String TBNAME="m_TB"; 
    static final int DBVERSION='1'; 

    static final String CREATE_TB="CREATE TABLE m_TB(id INTEGER PRIMARY KEY AUTOINCREMENT," 
      + "name TEXT NOT NULL);"; 

    final Context c; 
    SQLiteDatabase db; 
    DBHelper helper; 
    public DBAdapter(Context ctx) { 
     this.c = ctx; 
     helper = new DBHelper(c); 
    } 


    private static class DBHelper extends SQLiteOpenHelper { 
     public DBHelper(Context context) { 
      super(context, DBNAME, null, DBVERSION); 
     } 

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

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      Log.w("DBAdapter","Upgrading DB"); 
      db.execSQL("DROP TABLE IF EXISTS m_TB"); 
      onCreate(db); 
     } 
    } 
    public DBAdapter openDB() 
    { 
     try { 
      db=helper.getWritableDatabase(); 
     } catch (SQLException e) 
     { 
      Toast.makeText(c, e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
     return this; 
    } 

    public void close() 
    { 
     helper.close(); 
    } 

    public long add(String name) 
    { 
     try { 
      ContentValues cv = new ContentValues(); 
      cv.put(NAME,name); 
      return db.insert(TBNAME,ROW_ID,cv); 
     } catch (SQLException e) 
     { 
      e.printStackTrace(); 
     } 
     return 0; 
    } 

    public Cursor getAllNames() 
    { 
     String[] columns={ROW_ID,NAME}; 
     return db.query(TBNAME,columns,null,null,null,null,null); 
    } 

    public void delete(String name)throws SQLException 
    { 
     SQLiteDatabase db = helper.getWritableDatabase(); 
     if(db == null) 
     { 
      return; 
     } 
     String[] whereArgs = new String[]{name}; 
     db.delete("m_TB", "NAME"+ "=?", whereArgs); 
     db.close(); 
    } 


} 

回答

0

你是不是從列表中刪除books的項目。如果booksArrayList然後 更改您的代碼行

books.remove(info.position); 

,並從數據庫中刪除(如果書籍是自定義類型的ArrayList)。

db.delete(books.get(info.position).name); 

如果書籍是字符串的ArrayList然後

db.delete(books.get(info.position)); 
+0

我的應用程序是保存和檢索按鈕(當你按下檢索,似乎所有的列表)。它刪除項目,但當按下時再次檢索btn,它再次出現,所以這意味着它不會從數據庫中刪除。當我試圖使用db.delete(books.get(info.position));這是「不幸的是,圖書館已經停止」。我編輯我的帖子並插入完整的代碼 – Deividito

+0

它刪除,但現在我發現了一個錯誤:例如,我有3項刪除(「1」,「2」和「3」)我按刪除項目「1」,但它刪除項目「2」,我按刪除項目「2」,它刪除「3」,但如果我按刪除「3」應用程序崩潰 – Deividito

+0

我不明白髮生了什麼:D – Deividito