开发者社区> 问答> 正文

ListView Items 的点击问题

ListView获取信息保存在本地数据库然后再显示信息。我想点击listview的items时删除它们,如何实现?
screenshot
screenshot
ListView:
screenshot

展开
收起
蛮大人123 2016-02-15 17:53:26 2495 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    在程序中重写SimpleCursorAdapter.setViewBinder(),使用数据库中的 ID 来设置 ListView 里 Views 的 Tag,删除 setOnItemClickListener() 中数据库的 ID, 并刷新 Adapter。

     SimpleCursorAdapter notes = new SimpleCursorAdapter(this, 
                    R.layout.notes_row, c, from, to); 
        notes.setViewBinder(new SimpleCursorAdapter.ViewBinder() { 
    public boolean setViewValue(View view, Cursor cursor, int column) 
    { 
    TextView tv = (TextView) view; 
    view.setTag=cursor.getInt(cursor.getColumnIndex ("_id")); // You need to include the _id in the query
    tv.setText(String.Valueof(cursor.getInt(cursor.getColumnIndex (NotesDbAdapter.KEY_TITLE ))));
    return true; 
    } 
    });
             lv.setOnItemClickListener(new OnItemClickListener() { 
             public void onItemClick(AdapterView<?> parent, View view, 
             int position, long id) { 
             TextView tv=(TextView) view;
         String ID=view.getTag();
        // Delete ID from the DB
        notes.notifyDataSetChanged(); 
             }; 
             });    
        setListAdapter(notes); 
        } 
    } 
    2019-07-17 18:43:31
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载