2016-04-29 155 views
0

我想知道,如何從listview和SQLite數據庫中刪除項目。我有這個代碼。我應該更改什麼,因爲項目不會從listview中刪除。這是我的全部代碼,我不知道在它SQLite刪除項目

DBAdapter.java

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

MainActivity.java

public class MainActivity extends AppCompatActivity { 


    ListView lv; 
    EditText nameTxt; 
    Button savebtn,retrievebtn; 
    ArrayList<String> books = new ArrayList<String>(); 
    ArrayAdapter<String> adapter; 

    @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_selectable_list_item,books); 
     final DBAdapter db = new DBAdapter(this); 

     savebtn.setOnClickListener(new View.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 View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       books.clear(); 
       db.openDB(); 
       Cursor c = db.getAllNames(); 

       while (c.moveToNext()) 
       { 
        String name = getString(1); 
        books.add(name); 
       } 
       lv.setAdapter(adapter); 
       db.close(); 
      } 
     }); 
     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       Toast.makeText(getApplicationContext(),books.get(position), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 


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

     if(item.getTitle()== "Delete") 
     { 
      books.remove(item); 
      adapter.notifyDataSetChanged(); 
     } 
     return true; 
    } 

我插入這DPAdapter

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

和這MainActivity

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

但什麼都沒有發生。我怎樣才能調用這個刪除方法?

+0

有什麼'books'變量?這是缺少代碼的重要部分。 – Nathanael

+0

我編輯並插入了以上所有代碼 –

回答

1

比較.equals字符串,而不是「==」:

if(item.getTitle().equals("Delete")) 
     { 
      books.remove(item); 
      adapter.notifyDataSetChanged(); 
     } 

還需要添加刪除查詢刪除從SQlite的項目你目前正從只列表視圖刪除項目。

+0

我應該寫什麼代碼才能從SQLite中刪除它? –

+0

請參閱此處瞭解如何編寫代碼:http://developer.android.com/training/basics/data-storage/databases.html#DeleteDbRow – WannaBeGeek

+0

我應該寫什麼而不是COLUMN_NAME? –

1

這是如何從SQLite Database中刪除項目,您需要將它包含在DBAdapter中。

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

,並把它從刪除您ListView您需要從列表中刪除項,然後通知你的適配器這樣

YourArraylist.remove([index]); 
YourAdapter.notifyDataSetChanged(); 
+0

所以我只需要將它插入到DBAdapter中,並通過我自己更改變量以及我應該在MainActivity中編寫什麼? –

+0

第二部分是'MainActivity',你想刪除項目 – Max

+0

,但方法被稱爲刪除,所以我應該寫YourArraylist.delete([index]); ? –