如何让Android ListView刷新

时间:2013-01-08 20:14:55

标签: android listview refresh onresume

我目前无法在不重新启动整个应用程序的情况下从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);

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

startManagingCursor已弃用。 请改用CursorLoader

当ContentProvider的数据发生变化时,立即使用和更新ListView中的数据非常简单。

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

答案 2 :(得分:0)

您只显示了应用程序代码的一部分,因此我可能会在此处使用,但是您似乎正在使用Cursor返回List并使用List创建ArrayAdapter ...

如果您希望数据“新鲜”,为什么不将CursorLoader与LoaderManager一起使用? LoaderManager有几个好处(比如从UI线程移开查询),并且在监视数据库的变化时,LoaderManager会为您做一些繁重的工作。

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