开发者

Problem with refreshing listview from a dialog window

I have a listview and my implementation of the CursorAdapter. As a part of my list item I have a delete button. When the user presses the button I show a dialog asking for confirmation and if the user presses ok I delete the item from the database. The problem is with refreshing the listview. I tried calling cursor.requery() and mAdapter.notifyDataSetChanged() (separately or both) but that doesn't help. Requery clears the list and it re-appears (without the missing item) after re-entering the activity. notifyDataSetChanged does nothing (the item is still on the list) and again it is ok after re- entering the activity. I have managed to make this working after reloading the whole database:

 //in the dialog:
{
DBAdapter db = new DBAdapter( getApplicationContext() );
db.open();

db.deleteTitle( rowid );

db.close();

//cursor.requery();
//mAdapter.notifyDataSetChanged();

fillData();
}


private void fillData() {

                try{
                db.open();
                cursor = db.getAllTitles();
                startManagingCursor(cursor);

                 mAdapter = new MyIDsListCursorAdapters(this, R.layout.myidsrow,
cursor, columns, to);

                setListAdapter(mAdapter);
                db.close();
             }catch (SQLException e){
                showDatabaseErrorDialog ();
             }
         }

But reloading the whole db seems to be a very expensive task and I'm quite sure there must be a better way to do this.

I also have another problem - my listview items are defined by a relativelayout. However the layout seems to be ignoring all 'vertical' attributes, like alignParentBottom or centerVertical. I have seen an google I/O with Romain Guy and 开发者_JAVA技巧he answered a similar question by saying we should pass the parent ViewGroup followed by false, to the inflate function, but that still doesn’t solve my problem. Don’t know what is going on here. I solved this by placing my items below some others and playing with the margin/padding but I don’t really like that solution.


requery is deprecated. In my implementation, I simply recreate the cursor, which seems to work. Here are my code bits. In my case, I'm using a context menu to mark an item in a listview, but the basic approach should work for you.

    private Cursor fetchCursor(){
    Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        return mDbHelper.fetchSpellSearch(query);
    } else {
        return mDbHelper.fetchAllSpells();
    }
}

@Override  
public boolean onContextItemSelected(MenuItem item) {  
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
      case 0:
          mDbHelper.updateFavorite(info.id, 1);
          new bgRequery().execute();
          return true;
      case 1:
          mDbHelper.updateFavorite(info.id, 0);
          new bgRequery().execute();
          return true;
      default:
          return super.onContextItemSelected(item);
      }
}

private class bgRequery extends AsyncTask<Void, Integer, Void> {
    @Override
    protected Void doInBackground(Void... voids ) {
        mSpellCursor = fetchCursor();
        return null;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜