listview与数据库的同步问题

WilliamQ 发布于 2012/11/12 21:16
阅读 2K+
收藏 1

我想用sqlite开发一个android日记本,并实现增删改查功能。现在大部分功能已经完善,但是在实现删除功能时,总出现问题:delete语句执行后,数据库中的记录删除了,listview中显示的记录却不能同时删除,必须重启软件,listview中才会删除那条记录!哪位高手能指点一下我要如何实现listview与数据库的同步!!!相应代码如下:

package com.example.mydiary;


import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;


public class MainActivity extends Activity {

private Button addBtn;
private Button delateBtn;
private ListView diaryList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //找到每个组件的id
        findid();
        //将数据库的内容在主界面的listview中显示
        listview();
        //为listview设置监听
        listOnClick();
        //为主界面上的button设置监听事件
        onclicklistenner();
    }
    //获得数据库的实例
    public SQLiteDatabase getdb(){
    SQLiteDatabase db;
    return (new DiaryDate(MainActivity.this).getWritableDatabase());
    }
    //获得表中内容并返回一个游标Cursor
    public Cursor getcursor(){
    SQLiteDatabase db = getdb();
Cursor cursor = db.rawQuery( 
        "SELECT * FROM diary", null);
return cursor;
    }


private void listview() {
// TODO Auto-generated method stub
    Cursor cursor = getcursor();       
       //使用simpleCursorAdapter适配器
        SimpleCursorAdapter listAdapter = new SimpleCursorAdapter(MainActivity.this, R.layout.text_01,  
               cursor, 
               new String[]{"title","time"},  
               new int[]{R.id.text_title,R.id.text_time});          
       diaryList.setAdapter(listAdapter);
}
public void listOnClick(){
       //设定listview的点击监听事件
       diaryList.setOnItemClickListener(new OnItemClickListener(){


public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub

Cursor cursor = getcursor();
//游标指定到鼠标所在的arg2位置
cursor.moveToPosition(arg2);
Intent intent = new Intent();
intent.setClass(MainActivity.this, C_diary.class);
//为intent绑定数据
intent.putExtra("title",cursor.getString(cursor.getColumnIndex("title")));
intent.putExtra("body",cursor.getString(cursor.getColumnIndex("body")));
intent.putExtra("_id",cursor.getInt(cursor.getColumnIndex("_id")));
startActivity(intent);
}
       
       });
       
       //设定listview的长点击监听事件

diaryList.setOnItemLongClickListener(new OnItemLongClickListener() {


public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
 final int arg2, long arg3) {


new AlertDialog.Builder(MainActivity.this)
.setTitle("Message")
.setPositiveButton("删除",new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Cursor cursor = getcursor();
cursor.moveToPosition(arg2);
int i = cursor.getInt(cursor.getColumnIndex("_id"));
  
   Log.i("i", String.valueOf(i));
   //删除数据库中的数据
   SQLiteDatabase db = getdb();
db.delete("diary", "_id="+i,null);

}
})
.setNegativeButton("取消", null)
.show();
return true;
}
});
}


private void onclicklistenner() {
// TODO Auto-generated method stub
addBtn.setOnClickListener(sbtn1);
delateBtn.setOnClickListener(sbtn2);
}


private void findid() {
// TODO Auto-generated method stub
addBtn = (Button) findViewById(R.id.addBtn);
delateBtn = (Button) findViewById(R.id.delateBtn);
diaryList = (ListView) findViewById(R.id.diaryview);
}

OnClickListener sbtn1 = new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, CreatDiary.class);
startActivity(intent);
}
};

OnClickListener sbtn2 = new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

}
};


}

加载中
0
cookqq
cookqq
SimpleCursorAdapter listAdapter改成全局的变量,在你删除的时候,调用一下这个方法notifyDataSetChanged()就好使了。
0
F
FakeOS

引用来自“bugeasy”的答案

SimpleCursorAdapter listAdapter改成全局的变量,在你删除的时候,调用一下这个方法notifyDataSetChanged()就好使了。
+1024
0
Polly蜀黍
Polly蜀黍
从新载入一下adapter就行了
返回顶部
顶部