在android中编辑sqlitedatabase后,数据不会更改

在android中编辑sqlitedatabase后,数据不会更改,android,Android,我正在从事一个android项目,我想列出我现有sqlite数据库中的数据(TEST.sqlite)进入我的androidListview。我实现了这一点,但我的问题是,当我更改sqlite数据库中的数据时,我的Listview没有得到更新。当我第一次创建sqlite数据库时,我的Listview仍保留在数据中。当我修改数据库?有人能帮我找到解决方案吗 主要活动 public class MainActivity extends ActionBarActivity { ArrayAdapter

我正在从事一个android项目,我想列出我现有
sqlite
数据库中的数据(
TEST.sqlite
)进入我的android
Listview。我
实现了这一点,但我的问题是,当我更改sqlite数据库中的数据时,我的
Listview
没有得到更新。当我第一次创建
sqlite
数据库时,我的
Listview
仍保留在数据中。当我修改数据库?有人能帮我找到解决方案吗

主要活动

public class MainActivity extends ActionBarActivity {
 ArrayAdapter<String> adapter;
DatabaseHelper dbHeplper;
ListView lvUsers;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dbHeplper = new DatabaseHelper(getApplicationContext());
    try {
        dbHeplper.createDataBase();
    } catch (IOException e) {
        e.printStackTrace();
    }

ListView list=(ListView)findViewById(R.id.lvUsers); 
ArrayList<String> listUsers = new ArrayList<String>();

listUsers = (ArrayList<String>) dbHeplper.getAllUsers();

    if(listUsers != null){
         adapter = new ArrayAdapter<String>(getApplicationContext(),
                android.R.layout.simple_list_item_1, android.R.id.text1,
                listUsers);

        list.setAdapter(adapter);
         adapter.notifyDataSetChanged();


    }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}
公共类MainActivity扩展了ActionBarActivity{
阵列适配器;
数据库助手dbhelper;
ListView用户;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbhelper=newdatabasehelper(getApplicationContext());
试一试{
dbhelper.createDataBase();
}捕获(IOE异常){
e、 printStackTrace();
}
ListView列表=(ListView)findViewById(R.id.lvUsers);
ArrayList listUsers=新建ArrayList();
listUsers=(ArrayList)dbhelper.getAllUsers();
如果(listUsers!=null){
适配器=新的ArrayAdapter(getApplicationContext(),
android.R.layout.simple_list_item_1,android.R.id.text1,
列表用户);
list.setAdapter(适配器);
adapter.notifyDataSetChanged();
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
}
数据库助手类

public class DatabaseHelper extends SQLiteOpenHelper {
public static String DB_PATH = "/data/data/com.example.databaseandlist2/databases/";
public static String DB_NAME = "Test.sqlite";
public static final int DB_VERSION = 1;

public static final String TB_USER = "Users";
private SQLiteDatabase myDB;
private Context context;
public DatabaseHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);    
    this.context = context;
}
public void onCreate(SQLiteDatabase arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub

}
public synchronized void close(){
    if(myDB!=null){
        myDB.close();
    }
    super.close();
}
private boolean checkDataBase() {
    SQLiteDatabase tempDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        tempDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
        Log.e("tle99 - check", e.getMessage());
    }
    if (tempDB != null)
        tempDB.close();
    return tempDB != null ? true : false;
}

public void copyDataBase() throws IOException{
    try {
        InputStream myInput = context.getAssets().open(DB_NAME);
        String outputFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outputFileName);

        byte[] buffer = new byte[1024];
        int length;

        while((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }

        myOutput.flush();
        myOutput.close();
        myInput.close();
    } catch (Exception e) {
        Log.e("tle99 - copyDatabase", e.getMessage());
    }

}

public void openDataBase() throws SQLException{
    String myPath = DB_PATH + DB_NAME;
    myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();        

    if (dbExist) {

    } else {
        this.getReadableDatabase();
        try {
            copyDataBase();
        } catch (IOException e) {
            Log.e("tle99 - create", e.getMessage());
        }
    }
}

public List<String> getAllUsers(){
    List<String> listUsers = new ArrayList<String>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor c;

    try {
        c = db.rawQuery("SELECT * FROM " + TB_USER , null);
        if(c == null) return null;

        String name;
        c.moveToFirst();
        do {            
            name = c.getString(1);            
            listUsers.add(name);
        } while (c.moveToNext()); 
        c.close();
    } catch (Exception e) {
        Log.e("tle99", e.getMessage());
    }

    db.close();        

    return listUsers;
}
}
public类DatabaseHelper扩展了SQLiteOpenHelper{
公共静态字符串DB_PATH=“/data/data/com.example.databaseandlist2/databases/”;
公共静态字符串DB_NAME=“Test.sqlite”;
公共静态最终int DB_版本=1;
公共静态最终字符串TB_USER=“Users”;
私有数据库myDB;
私人语境;
公共数据库助手(上下文){
super(上下文,数据库名称,空,数据库版本);
this.context=上下文;
}
创建公共void(SQLiteDatabase arg0){
//TODO自动生成的方法存根
}
@凌驾
public void onUpgrade(SQLiteDatabase arg0、int arg1、int arg2){
//TODO自动生成的方法存根
}
公共同步作废关闭(){
如果(myDB!=null){
myDB.close();
}
super.close();
}
私有布尔校验数据库(){
SQLiteDatabase tempDB=null;
试一试{
字符串myPath=DB_PATH+DB_NAME;
tempDB=SQLiteDatabase.openDatabase(myPath,null,
SQLiteDatabase.OPEN_READWRITE);
}catch(sqlitee异常){
Log.e(“tle99-check”,e.getMessage());
}
if(tempDB!=null)
tempDB.close();
返回tempDB!=null?真:假;
}
public void copyDataBase()引发IOException{
试一试{
InputStream myInput=context.getAssets().open(DB_NAME);
字符串outputFileName=DB\u路径+DB\u名称;
OutputStream myOutput=新文件OutputStream(outputFileName);
字节[]缓冲区=新字节[1024];
整数长度;
而((长度=myInput.read(缓冲区))>0){
写入(缓冲区,0,长度);
}
myOutput.flush();
myOutput.close();
myInput.close();
}捕获(例外e){
Log.e(“tle99-copyDatabase”,e.getMessage());
}
}
public void openDataBase()引发SQLException{
字符串myPath=DB_PATH+DB_NAME;
myDB=SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN\u READWRITE);
}
public void createDataBase()引发IOException{
布尔值dbExist=checkDataBase();
if(dbExist){
}否则{
这是.getReadableDatabase();
试一试{
copyDataBase();
}捕获(IOE异常){
Log.e(“tle99-create”,e.getMessage());
}
}
}
公共列表getAllUsers(){
List listUsers=new ArrayList();
SQLiteDatabase db=this.getWritableDatabase();
光标c;
试一试{
c=db.rawQuery(“选择*自”+TB\U用户,空);
如果(c==null)返回null;
字符串名;
c、 moveToFirst();
做{
name=c.getString(1);
添加(名称);
}而(c.moveToNext());
c、 close();
}捕获(例外e){
Log.e(“tle99”,e.getMessage());
}
db.close();
返回列表用户;
}
}

当您更改数据库中的某些内容时,您需要调用
适配器。notifyDataSetChanged()
,例如当用户按下
插入
按钮时,将其放入
onClick
中 使用
ArrayAdapter
而不是
ListAdapter
无论何时插入新记录或更新数据库,都需要再次调用

listUsers = dbHeplper.getAllUsers();
之后,您只需使用

 adapter.notifyDataSetChanged();

你在哪里插入记录?我正在使用我现有的数据库,我刚刚将其复制到我的资产文件夹中。我正在现有的数据库表中插入数据。嘿,当你在数据库中进行更改时,以及之后,你还应该更新适配器,并将更新的适配器设置到listview中,它将显示数据库的最新值。你是怎么做到的sqlite数据库中的更新日期?是否将数据插入数据库并将数据库添加到资产中?数据是否未显示?方法notifyDataSetChanged()对于类型ListAdapter是未定义的,它向我显示错误。请尝试使用
ArrayAdapter
您需要使用
adapter.notifyDataSetChanged()在数据库中插入新记录的位置Buddy中插入记录的代码在哪里?如何插入记录?请阅读我的答案并检查我写的内容。亲爱的投票人,请在投票时留下评论