使用android中的sql数据库删除listview中的行

时间:2014-09-15 12:50:09

标签: android sql listview

我有一个应用程序,它接受用户输入并将其添加到数据库并将其显示在列表视图中。我能够删除整个数据库,但我试图删除所选项目,但无法搞清楚。我已经尝试了我在这个网站上找到的一切,但没有任何作用。如果有人可以提供帮助或有链接我没有找到任何帮助表示赞赏。

这是我的listview代码:

public class ClassList extends Activity {

ListView lv;
String query;
Cursor c;
String kids[] = {"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""};
int i = 0;
int t = 0;
SQLiteDatabase db;
final Context context = this;

//ADD OVERFLOW MENU TO APP
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main2, menu);
    return true;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.class_list);
    lv = (ListView) findViewById(R.id.listView1);
    PreferenceManager.setDefaultValues(this, R.xml.preference, false);
    Button btnDel = (Button) findViewById(R.id.btnDel);

    // Declaring arrayadapter to store the items and return them as a view
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, kids);

    db = openOrCreateDatabase("CLASS", Context.MODE_PRIVATE, null);

    //READING THE DATA FROM THE TABLE CLASS AND SETTING IN AN ARRAYADAPTER
    c = db.rawQuery("SELECT * FROM CLASS; ", null);
    i = 0;

    while (c.moveToNext()) {

        kids[i] = c.getString(0);
        i++;
    }
    t = i;

    lv.setAdapter(adapter);

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        //GOING BACK TO MAIN ACTIVITY
        case R.id.back:
            startActivity(new Intent(getApplicationContext(),
                 MainActivity.class));
        return true;

        //CLEAR ALL CHECK MARKS
        case R.id.clear:
            lv.clearChoices();
            for (int i = 0; i < lv.getCount(); i++)
                lv.setItemChecked(i, false);
            return true;

        //ERASE SELECTED STUDENTS
        case R.id.erase_single:



            return true;

        //ERASE CLASS LIST
        case R.id.erase:

            final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_multiple_choice, kids);

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

            // set title
            alertDialogBuilder.setTitle("Erase Class List");

            // set dialog message
            alertDialogBuilder
                    .setMessage("Are you sure you want to ERASE the whole class list?")
                    .setCancelable(false)
                    .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, erase
                            // the entire class list

                            db = openOrCreateDatabase("CLASS", Context.MODE_PRIVATE,
                                    null);

                            //DELETE ALL THE DATA FROM TABLE
                            db.delete("CLASS", null, null);

                            db.close();

                            for (int j = 0; j < t; j++) {
                                kids[j] = "";
                                lv.setAdapter(adapter);
                            }
                        }
                    })
                    .setNegativeButton("No",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing
                            dialog.cancel();
                        }
                    });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();
            return true;

        //GOING TO SETTINGS
        case R.id.settings:
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

}

1 个答案:

答案 0 :(得分:1)

您必须修改我的代码以满足您的需求,但这是一个从我的数据库中删除某条记录的示例方法:

    public <T> void deleteRecord(T obj)
    {
        synchronized(GaggleApplication.LK_Database){
            SQLiteDatabase db = super.getHelper().getWritableDb();
            try
            {
                db.delete(super.tableName, "ID = ? ", 
                    new String[] { String.valueOf(((Image)obj).getId()) });
            }
            catch(SQLiteException e)
            {
                e.printStackTrace();
            }
        }
    }

您感兴趣的部分是:

db.delete(super.tableName, "ID = ? ", 
    new String[] { String.valueOf(((Image)obj).getId()) });

哪个会删除表“tableName”中的特定行,其中ID =?的getId()

为了完整性,这里还有一个更新方法,以防你最终还需要其中一个:

    public <T> void updateRecord()
    {
        synchronized(GaggleApplication.LK_Database){
            SQLiteDatabase db = super.getHelper().getWritableDb();
            try
            {
                ContentValues values = new ContentValues();
                values.put("REPORTED", this.reported);
                values.put("REPORTED_FULL", this.reportedfully);
                values.put("PATH", this.path);

                db.update(super.tableName, values, "ID = ?", 
                        new String[] { String.valueOf(this.id) });

                values = null;
            }
            catch(SQLiteException ex)
            {
                ex.printStackTrace();
            }
        }
    }

希望这有帮助!