如何使Android ListView刷新



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

    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);

你正在使用startActivity。为什么不使用startActivityForResult呢?这个问题的答案详细说明了如何调用第二个Activity。一旦它返回,您就必须使用notifyDataSetChanged()调用。

startManagingCursor已弃用。请使用CursorLoader。

这是非常简单的使用和更新你的数据在ListView立即当ContentProvider的数据改变。

下面是一个很好的例子:http://www.vogella.com/articles/AndroidSQLite/article.html todo_activities

您只显示了部分应用程序代码,所以我可能在这里偏离基础,但似乎您正在使用游标返回列表并使用列表创建ArrayAdapter…

如果你想要数据是"新鲜的",为什么不使用一个游标加载器与LoaderManager?LoaderManager有几个好处(比如将查询移出UI线程),并且在监视DB的更改时,LoaderManager为您做了一些繁重的工作。

有一篇博客文章我觉得很有帮助- http://www.androiddesignpatterns.com/2012/07/understanding-loadermanager.html

最新更新