安卓:无法从ListView和SQLite的删除记录 [英] Android: Can't delete record from ListView and SQLite

查看:190
本文介绍了安卓:无法从ListView和SQLite的删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图删除从项目长按一个列表视图记录的几种方法,但没有任何反应,没有错误,没有删除什么,只是举杯显示出来... 这里是code:

I've tried several methods of deleting the record from a listview on item long click but, nothing happens, no errors, no delete nothing, just the toast shows up... Here is the code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.layout.push_left_in, R.layout.push_left_out);
    setContentView(R.layout.moje_ure);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    datasource = new VnosiDataSource(this);
    datasource.open();


    final List<VnosiDB> values = datasource.getAllDela();
    final ArrayAdapter<VnosiDB> adapter = new ArrayAdapter<VnosiDB>(this,
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
    ListView ureList = getListView();
    adapter.notifyDataSetChanged();
    ureList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int pos,
            long id) {
            // TODO Auto-generated method stub

            //some code here...

            String posit = values.get(pos).toString();
            Toast.makeText(Ure.this, posit, Toast.LENGTH_SHORT).show();
        }
    });
    ureList.setOnItemLongClickListener(new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View view,
            int pos, long id) {
        // TODO Auto-generated method stub

        datasource.deleteVnos((int)values.get(pos).getId());
        Toast.makeText(Ure.this, "Vnos " + values.get(pos).toString() + " izbrisan!", Toast.LENGTH_SHORT).show();
        adapter.notifyDataSetChanged();
        return true;
    }
});

和数据库的删除方法:

public void deleteVnos(int _id){

    database.delete(DatabaseManidzer.TABLE_VNOSI, DatabaseManidzer.COLUMN_ID + " = " + _id, null);
} 

public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
}

更新:用于填充类VnosiDataSource.java列表视图方式:

UPDATE: Method for populating the list view in class VnosiDataSource.java:

    public List<VnosiDB> getAllDela() {

    List<VnosiDB> dela = new ArrayList<VnosiDB>();

    Cursor cursor = database.rawQuery(
            "SELECT delo from vnosi ORDER BY vnos DESC", null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        VnosiDB curdela = cursorToDela(cursor);
        dela.add(curdela);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return dela;
}

我在想什么?

What am I missing?

推荐答案

您需要再次重新查询数据库中删除的项目之后。一旦你得到的结果再返回设置适配器的结果。

You need to requery the database again after you delete the item. Once you get the results back set the adapter again with the results.

@Override
protected void onCreate(Bundle savedInstanceState) {
  ....
updateListView();
ureList.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View view, int pos,
        long id) {
        // TODO Auto-generated method stub

        //some code here...

        String posit = values.get(pos).toString();
        Toast.makeText(Ure.this, posit, Toast.LENGTH_SHORT).show();
    }
 });
ureList.setOnItemLongClickListener(new OnItemLongClickListener() {

  @Override
  public boolean onItemLongClick(AdapterView<?> arg0, View view,
        int pos, long id) {
    VnosiDB item = (VnosiDB) getListAdapter().getItem(pos);
    int itemId = item.getId();
    datasource.deleteVnos(itemId);
    Toast.makeText(Ure.this, "Vnos " + item.toString() + " izbrisan!", Toast.LENGTH_SHORT).show();
    updateListView();
    return true;
  }
});

public void updateListView() {
    final List<VnosiDB> values = datasource.getAllDela();
    final ArrayAdapter<VnosiDB> adapter = new ArrayAdapter<VnosiDB>(this,
       android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
}

public void deleteVnos(long itemId){
  database.delete(DatabaseManidzer.TABLE_VNOSI, "_id = ?", new String[]{Long.toString(itemId)});
} 

更新
你是不是选择 _id 当你查询数据库为您的项目。我不知道你在做cursorToDela(光标)什么,但你没有 _id 在光标来填充什么是curdela.getId返回();

UPDATE
You aren't selecting the _id when you are querying the database for your items. I'm not sure what you are doing in cursorToDela(cursor) but you don't have the _id in that cursor to populate what is returned by curdela.getId();

public List<VnosiDB> getAllDela() {

  List<VnosiDB> dela = new ArrayList<VnosiDB>();

  Cursor cursor = database.rawQuery(
        "SELECT _id, delo from vnosi ORDER BY vnos DESC", null);

  while (cursor.moveToFirst()){
    //cursorToDela needs to grab the _id from the cursor and set it on the created VnosiDB.
    VnosiDB curdela = cursorToDela(cursor);
    dela.add(curdela);
  }
  // Make sure to close the cursor
  cursor.close();
  return dela;
}

这篇关于安卓:无法从ListView和SQLite的删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆