如何在使用自定义适配器时删除数据库数据?

时间:2017-08-05 16:02:00

标签: android listview android-sqlite

我在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 个答案:

答案 0 :(得分:1)

您必须在匹配值之间添加AND条件,'" + name也不匹配'

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

注意:您只需使用一些唯一的列值删除一行,或者可以将id设为INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL