liyang_ 2016-12-04 12:52 采纳率: 0%
浏览 837

数据库利用寻找ID找寻listview中的内容。

自己做了个记事本,用到了listview控件跟轻量级数据库。
在Dao类中创建了findviewbyid的方法
当我单击listview,已将添加好的忘备录无法在编辑页面中显示,我觉得肯定是利用ID寻找数据库中的 内容写错了,但是不知道如何修改,望大神赐教
Dao中方法:public NoteBean findById(Integer id){
SQLiteDatabase db=helper.getWritableDatabase();
Cursor cursor=db.rawQuery("select * from note where id = ?",
new String[]{id.toString()});
NoteBean noteBean=new NoteBean();
if (cursor.moveToFirst()){
int mynewid=cursor.getInt(cursor.getColumnIndex("id"));
String mynewcontent=cursor.getString(cursor.getColumnIndex ("content"));
String mynewdate=cursor.getString(cursor.getColumnIndex("date"));
noteBean.setId(mynewid);
noteBean.setContent(mynewcontent);
noteBean.setDate(mynewdate);
}
cursor.close();
db.close();
return noteBean;
MainActivity方法: /*
* 查看修改内容
* /
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NoteBean noteBean=(NoteBean) parent.getAdapter().getItem(position);
Log.e("liayang","Id是"+position);
int mynewId=noteBean.getId();
//int mynewId=position+1;
// int mynewId=0;
Intent intent=new Intent(MainActivity.this,UpdateNoteActivity.class);
/
把int类型转化为String类型的*/
intent.putExtra("",String.valueOf(mynewId));
startActivity(intent);

  • 写回答

1条回答 默认 最新

  • bdmh 优质创作者: 移动开发技术领域 2016-12-04 12:58
    关注

    你先调试cursor是否查到数据,还有你的id怎么存到数据库中的,是否永远不变

    评论

报告相同问题?