带删除的SQLiteDB ListView

时间:2011-07-21 20:03:39

标签: android sqlite android-listview

我已经研究过这个问题,但我仍然坚持一些事情。首先,我还没有完全掌握如何使用3字段数据库并在ListView中显示它(我以前只是在可滚动的文本视图中显示它)。就目前而言,我有2个编辑文本,这两个字段数据与添加按钮一起添加。我想要做的是在ListView中显示数据库,并让ListView在右边的每一行包含一个X来删除该行。最初我有用户查看表,匹配右边的rid,输入并点击删除按钮。

我的DataHelper.java

public class DataHelper {

private static final String DATABASE_NAME = "AdultSwim.db";
private static final int DATABASE_VERSION = 3;
private static final String TABLE_NAME = "Requests";
private static final String KEY_ID = "_id";
private static final String field1 = "Show";
private static final String field2 = "Want";
private Context context;
private SQLiteDatabase db;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + TABLE_NAME + "(Show) values (?)" + "(Want) values (?)";

public DataHelper(Context context) {
        this.context = context;

        OpenHelper openHelper = new OpenHelper(this.context);
        this.db = openHelper.getWritableDatabase();
        this.insertStmt = this.db.compileStatement(INSERT);
    }

    public long insert(String name) {
        this.insertStmt.bindString(1, name);
        return this.insertStmt.executeInsert();
    }

    public void deleteOne(Long rowId) {
        String rownum = rowId.toString();
        db.delete(TABLE_NAME, KEY_ID + "=" + rowId, null);
    }

    public  void deleteAll() {
        this.db.delete(TABLE_NAME, null, null);
    }

    public List<String> selectTableData() {
        List<String> list = new ArrayList<String>();
        Cursor cursor = this.db.query(TABLE_NAME, null, null, null, null, null, null, null);

        if (cursor.moveToFirst()) {
            do {
                list.add(cursor.getString(0) + " " + cursor.getString(1));
            } while (cursor.moveToNext());
        }

        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
         return list;
    }

    private static class OpenHelper extends SQLiteOpenHelper {
        OpenHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE_NAME + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, Show VARCHAR NOT NULL, Want VARCHAR NOT NULL)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }
}

我认为这不完全准确。我有一个只有主键和一个字段,我不认为我的第二个字段正在添加。

现在主要是Java

public class Item2 extends Activity {

    private DataHelper dh;
    private ImageView addButton;
    private ImageView displayNames;
    private ImageView delButton;
    private EditText show;
    private EditText want;
    private Editable varTextShow;
    private Editable varTextWant;
    private TextView outputTable;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item2xml);
        dh = new DataHelper(this);
        addButton = (ImageView) this.findViewById(R.id.btnAdd);
        displayNames = (ImageView) this.findViewById(R.id.btnDisp);
        delButton = (ImageView) this.findViewById(R.id.btnDel);
        show = (EditText) this.findViewById(R.id.showTitle);
        want = (EditText) this.findViewById(R.id.wantDegree);
        outputTable = (TextView) this.findViewById(R.id.output);

    addButton.setOnClickListener(new Button.OnClickListener(){

    @Override
    public void onClick(View v) {
        varTextShow = show.getEditableText();
        String nameEntered = varTextShow.toString();
        dh.insert(nameEntered);
        show.setText("");
    }
});

displayNames.setOnClickListener(new Button.OnClickListener(){
    @Override
    public void onClick(View v) {
        List<String> names = dh.selectTableData();
        StringBuilder sb = new StringBuilder();
        sb.append("Shows Requested:\n");
        for (String name : names) {
            sb.append(name);
            sb.append("\n");
        }

            outputTable.setText(sb.toString());
    }

});

delButton.setOnClickListener(new Button.OnClickListener(){

    @Override
    public void onClick(View v){
        varTextShow = show.getEditableText();
        String rowToDelete;
        rowToDelete = varTextShow.toString();
        try{
            Long deleteRow = Long.parseLong(rowToDelete.trim());
            dh.deleteOne(deleteRow);
            show.setText("");
        }
        catch (NumberFormatException e)
        {
            Toast toast = Toast.makeText(getBaseContext() ,"You must enter a number.",Toast.LENGTH_LONG);
            toast.show();
            show.setFocusable(true);
        }
    }

});
}
}

如果之前有人询问,我很抱歉。我看过答案,看过例子,我无法绕过它。就目前而言,数据库只显示在用户输入数据的同一可滚动窗口上。

哦,主Java还没有完全合并第二个字段(field2)。

我是Android编程的初学者。

2 个答案:

答案 0 :(得分:0)

这不一定是您所有问题的答案,但它可以帮助您排除故障。

您可以查看数据库,以便在连接AVD时通过在终端中启动adb来查找已添加的内容。

  1. 打开终端并输入adb shell
  2. cd进入包含数据库的文件夹(通常是data / data / com.yourpackage / databases /)
  3. 输入sqlite3 <database_file_name>
  4. 如果您到目前为止所做的一切都正确,输入.tables应该会显示所有表格。
  5. 输入select * from <table_name>;查看您的所有数据。
  6. 这对于数据库开发来说至关重要,您可以查看您的数据库以及如何输入数据。

答案 1 :(得分:0)

在deleteIco.setOnClickListener()

中的列表视图适配器类中编写删除查询