2017-08-05 42 views
0

我在我的AnaMenu活動中定製了listview,這個listview是由我的數據庫數據創建的。我試圖刪除我的數據庫中的所有數據,我想去MainActivity。當用戶點擊操作欄圖標時,所有數據都將被刪除,但是當我嘗試時,我收到錯誤消息。我的數據庫有4列。還有一件重要的事情;我的AnaMenu活動有這個,我該如何解決?如何在使用自定義適配器的同時刪除數據庫數據?

此:(我的MainActivity。)

mDatabaseHelper = new DatabaseHelper(this); 
     Cursor data = mDatabaseHelper.getData(); 
     sayı = data.getCount(); 

     if (sayı > 4){ 
      Intent i = new Intent(MainActivity.this,AnaMenu.class); startActivity(i); 
      MainActivity.this.finish(); 
     } 

如何創建我的自定義適配器(在我AnaMenu活動):

while(data.moveToNext()){ 

      listDataId.add(data.getString(0)); 

      listData.add(data.getString(1)); 


      listDatanumber.add(data.getString(2)); 

      listDataoran.add(data.getString(3)); 


     } 
     liste.setAdapter(oyunTextView); 

    } 


    class OyunTextView extends BaseAdapter { 


     @Override 
     public int getCount() { 

      return listData.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return null; 
     } 

     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      convertView = getLayoutInflater().inflate(R.layout.oyuntextview, null); 
      TextView namesbox = (TextView) convertView.findViewById(R.id.isim); 
      sayılar = (TextView) convertView.findViewById(R.id.sayı); 
      namesbox.setText(listData.get(position)); 
      sayılar.setText(listDataoran.get(position)); 


      return convertView; 
     } 
    } 

我的數據庫的方法:

public void deleteAllName(int id, String name ,String numara, String oran){ 
     SQLiteDatabase db = this.getWritableDatabase(); 
     String query = "DELETE FROM " + TABLE_NAME + " WHERE " 
       + COL1 + " = '" + id + "'" + COL2 + " = '" + name + COL3 + " = '"+ numara + 
       " AND " + COL4 + " = '" + oran + "'"; 
     Log.d(TAG, "deleteName: query: " + query); 
     Log.d(TAG, "deleteName: Deleting " + name + " from database."); 
     db.execSQL(query); 
    } 

這是我的標誌圖標代碼:

private void geri() { 
     for (int d = 0 ; d<listDatanumber.size() ; d++) { 
     int id = Integer.parseInt(listDataId.get(d)); 
      String isim = listData.get(d); 
      String number = listDatanumber.get(d); 
     String oran = listDataoran.get(d); 
     mDatabaseHelper.deleteAllName(id,isim,number,oran); 
    } 


     Intent ii = new Intent(AnaMenu.this, MainActivity.class); 
     startActivity(ii); 
     AnaMenu.this.finish(); 

    } 

回答

1

你有你的匹配值之間添加AND條件,也'" + name中沒有匹配的'

String query = "DELETE FROM " + TABLE_NAME + " WHERE " 
       + COL1 + " = '" + id + "' AND " 
       // AND clause    ^^^^^ 
       + COL2 + " = '" + name + "' AND " 
       // matching '   ^
       + COL3 + " = '"+ numara +"' AND " 
       + COL4 + " = '" + oran + "'"; 

注意:你可以刪除行使用一些獨特的列值或可以使您的idINTEGER PRIMARY KEY AUTOINCREMENT NOT NULL

+0

感謝您幫助您的答案是真實的。 –

相關問題