在Button Click上从DB中选择数据库填充的ListView删除

时间:2011-08-07 00:43:53

标签: android

好吧,我仍然对android和eclipse有点新鲜,但标题几乎说明了一切。我有一个ListView,我从DB中的行填充。我想获取所选列表视图项并在单击按钮时从DB中删除行,然后在完成后刷新ListView。到目前为止,我的代码填充了列表,我被困在那里。任何帮助将不胜感激。

public class ViewListingsActivity extends Activity implements OnClickListener {
    SQLiteDatabase db;
    ListView listview;
    Button button;
    SimpleCursorAdapter adapter;
    String value;
    private static final String TAG = DBHelper.class.getSimpleName();
    public static final String DB_ADDRESS = BaseColumns._ID;
    public static final String DB_DESCRIPTION = "Description";
    public static final String DB_URL = "URL";
    final String dbTable = "Realtor_SMS_Table";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewlisting);
        listview = (ListView) findViewById(R.id.list);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);
        DBHelper dbhelper = new DBHelper(ViewListingsActivity.this);
        db = dbhelper.getWritableDatabase();

        Cursor cursor = db.query(dbTable, null, null, null, null, null, null);
        startManagingCursor(cursor);

        String[] from = new String[] { DB_ADDRESS, DB_DESCRIPTION, DB_URL };
        int[] to = new int[] { R.id.textlistaddress, R.id.textlistdescription,
                R.id.textlisturl };
        adapter = new SimpleCursorAdapter(this, R.layout.rowlist, cursor, from,
                to);

        listview.setAdapter(adapter);
        listview.setChoiceMode(1);

    }

    public void onClick(View arg0) {
        db.delete(dbTable, "_id=" + value, null);
        adapter.notifyDataSetChanged();

        Log.d("wtf", "deleted: " + value);

    }

}

好的,这是我新编辑的代码:

public class ViewListingsActivity extends Activity implements OnClickListener {
    SQLiteDatabase db;
    ListView listview;
    Button button;
    SimpleCursorAdapter adapter;
    String value;
    OnItemClickListener listener;
    Cursor newcursor;
    String deleteinfo;

    private static final String TAG = DBHelper.class.getSimpleName();
    public static final String DB_ADDRESS = BaseColumns._ID;
    public static final String DB_DESCRIPTION = "Description";
    public static final String DB_URL = "URL";
    final String dbTable = "Realtor_SMS_Table";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewlisting);
        listview = (ListView) findViewById(R.id.list);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);
        DBHelper dbhelper = new DBHelper(ViewListingsActivity.this);
        db = dbhelper.getWritableDatabase();

        Cursor cursor = db.query(dbTable, null, null, null, null, null, null);
        startManagingCursor(cursor);

        String[] from = new String[] { DB_ADDRESS, DB_DESCRIPTION, DB_URL };
        int[] to = new int[] { R.id.textlistaddress, R.id.textlistdescription,
                R.id.textlisturl };
        adapter = new SimpleCursorAdapter(this, R.layout.rowlist, cursor, from,
                to);

        listview.setAdapter(adapter);
        listview.setChoiceMode(1);
        listview.setOnItemClickListener(listener);

    }
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {

    newcursor = ((SimpleCursorAdapter)listview.getAdapter()).getCursor();
    deleteinfo = newcursor.getString(1);//getString(0);
    }

    public void onClick(View arg0) {
        db.delete(dbTable, "_id=" + deleteinfo, null);
        adapter.notifyDataSetChanged();

        Log.d("wtf", "deleted: " + deleteinfo);

    }

}

我的deleteinfo字符串一直返回null,为什么不检索正确的列值?

1 个答案:

答案 0 :(得分:0)

要删除,只需通过正常查询从数据库中删除行。然后,通过以下方式更新SimpleCursorAdapter:

Cursor cursor = ((SimpleCursorAdapter)listview.getAdapter()).getCursor();
cursor.requery();

注意
这必须在UI线程上完成

修改
设置OnItemClickListener,它为您提供所选项的位置参数。见Adapter.onItemClickListener | Android Developers

此位置可用于抓取指向行的光标:

Cursor cursor = (Cursor) ((SimpleCursorAdapter)listview.getAdapter()).getItem(position);