2013-01-08 93 views
1

我目前无法从我的ListView中查看新插入的sqlite数据,而无需重新启动我的整个应用程序。目前,当选择ListView项目时,新的活动开始显示详细信息。一旦详细信息被更新,编辑或项目被删除,ListView将再次显示,但包含所有原始数据,除非应用程序关闭并重新启动。列表视图OnResume方法如下。如何在屏幕上显示ListView时刷新ListView。我尝试添加.notifyDataSetChanged失败。不知道我是否正确实施。如何使Android ListView刷新

public List<String> populateList(){ 

List<String> webNameList = new ArrayList<String>(); 

dataStore openHelperClass = new dataStore (this); 

SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase(); 

Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null); 

startManagingCursor(cursor); 

while (cursor.moveToNext()){ 

String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE)); 
String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS)); 
String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME)); 
String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD)); 
String lNotes = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_NOTES)); 

LoginDetails lpDetails = new LoginDetails(); 

    lpDetails.setsName(sName); 
    lpDetails.setwUrl(wUrl); 
    lpDetails.setuName(uName); 
    lpDetails.setpWord(pWord); 
    lpDetails.setlNotes(lNotes); 

    loginArrayList.add(lpDetails); 
    webNameList.add(sName); 
} 

    sqliteDatabase.close(); 
    return webNameList; 
} 



    @Override 
    protected void onResume() { 
    super.onResume(); 
    loginListAdapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, populateList()); 
    loginList.setAdapter(loginListAdapter); 
} 

    @Override 
    public void onItemClick(AdapterView<?> arg0 , View arg1, int arg2, long arg3) { 
    Toast.makeText(getApplicationContext(), "Selected ID :" + arg2,  Toast.LENGTH_SHORT).show(); 

Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class); 

LoginDetails clickedObject = loginArrayList.get(arg2); 

    Bundle loginBundle = new Bundle(); 
loginBundle.putString("clickedWebSite",clickedObject.getsName()); 
loginBundle.putString("clickedWebAddress",clickedObject.getwUrl()); 
loginBundle.putString("clickedUserName",clickedObject.getuName()); 
loginBundle.putString("clickedPassWord",clickedObject.getpWord()); 
loginBundle.putString("clickedNotes",clickedObject.getlNotes()); 

updateDeleteLoginInfo.putExtras(loginBundle); 

startActivity(updateDeleteLoginInfo); 
+0

是什么'dataStore.COLUMN_NAME_SITE'在你的代码?你如何添加/ /更新/删除项目?我认为你的问题是因为你同时使用getReadableDatabase和getWriteableDatabase方法。 – vorrtex

+0

请首先将您的数据库相关代码放在与活动类别不同的​​课程中 –

回答

0

您正在使用startActivity。为什么不使用startActivityForResult呢? this question的答案详细说明了您将如何称为第二个活动。一旦它返回,你将不得不使用notifyDataSetChanged()调用。

+0

与活动无关。很明显,主要问题是数据的同步。 – vorrtex

+0

嗯,我看到他用startActivity调用来调用updateDeleteLoginInfo。从那以后,我假设这是他用来更新/删除信息的活动的名称,并且由于他没有阻止当前活动,所以对列表的任何更改都不会反映出来。我有类似的代码,有类似的问题,这就是为什么我回答他。 – DigCamara

+0

@DigCamera他使用startActivity方法就够了。如果要将数据存储在可供所有活动访问的全局对象中,它的工作方式与startActivityForResult相同。 SQLite数据库就是这种对象的一个​​很好的例子,尽管它比一些内存中的ArrayList更难使用。 – vorrtex

0

你只显示应用程序代码的一部分,所以我在这里可以offbase,但它似乎您使用游标返回一个列表并使用该列表创建一个ArrayAdapter ...

如果您希望数据是'新鲜'的,为什么不用LoaderManager使用CursorLoader? LoaderManager有几个好处(例如将查询从UI线程移出),并且LoaderManager在监控数据库更改时为您做了一些繁重的工作。

还有我发现非常有帮助的一篇博客文章 - http://www.androiddesignpatterns.com/2012/07/understanding-loadermanager.html