Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从对话框窗口刷新listview时出现问题_Android_Database_Listview_Dialog_Refresh - Fatal编程技术网

Android 从对话框窗口刷新listview时出现问题

Android 从对话框窗口刷新listview时出现问题,android,database,listview,dialog,refresh,Android,Database,Listview,Dialog,Refresh,我有一个列表视图和CursorAdapter的实现。作为一个 在我的列表项中,我有一个删除按钮。当用户按下 按钮我显示一个对话框,询问用户是否按下 好的,我从数据库中删除该项。问题在于提神 列表视图。我尝试调用cursor.requery()和 mAdapter.notifyDataSetChanged() 救命啊。“重新查询”将清除列表并重新显示(不丢失 项目)重新进入活动后。NOTIFYDATASETCHETCHANGED做什么 什么都没有(项目仍然在列表中),并且在重新设置后仍然正常- 进

我有一个列表视图和CursorAdapter的实现。作为一个 在我的列表项中,我有一个删除按钮。当用户按下 按钮我显示一个对话框,询问用户是否按下 好的,我从数据库中删除该项。问题在于提神 列表视图。我尝试调用cursor.requery()和 mAdapter.notifyDataSetChanged() 救命啊。“重新查询”将清除列表并重新显示(不丢失 项目)重新进入活动后。NOTIFYDATASETCHETCHANGED做什么 什么都没有(项目仍然在列表中),并且在重新设置后仍然正常- 进入活动。我已经设法使这项工作后 重新加载整个数据库:

 //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 ();
             }
         }
但是重新加载整个数据库似乎是一项非常昂贵的任务,我很抱歉 肯定有更好的办法

我还有另一个问题-我的listview项是由 相对论。但是,布局似乎忽略了所有“垂直” 属性,如alignParentBottom或centerVertical。我看过一本书 谷歌I/O和Romain Guy,他回答了一个类似的问题 表示应该将父视图组后跟false传递给 充气功能,但这仍然不能解决我的问题。不知道 这是怎么回事。我通过将我的物品放在下面来解决这个问题 其他人和玩边距/填充,但我真的不喜欢 这个解决方案

不推荐重新查询。 在我的实现中,我只是简单地重新创建光标,它似乎可以工作。这是我的代码。在我的例子中,我使用上下文菜单在listview中标记一个项目,但基本方法应该适合您

    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;
    }
private Cursor fetchCursor(){
Intent=getIntent();
if(Intent.ACTION_SEARCH.equals(Intent.getAction())){
String query=intent.getStringExtra(SearchManager.query);
返回mDbHelper.fetchSpellSearch(查询);
}否则{
返回mDbHelper.fetchAllSpells();
}
}
@凌驾
公共布尔值onContextItemSelected(MenuItem项){
AdapterContextMenuInfo信息=(AdapterContextMenuInfo)项。getMenuInfo();
开关(item.getItemId()){
案例0:
mDbHelper.updateFavorite(信息id,1);
新建bgRequery().execute();
返回true;
案例1:
mDbHelper.updateFavorite(info.id,0);
新建bgRequery().execute();
返回true;
违约:
返回super.onContextItemSelected(项目);
}
}
私有类bgRequery扩展异步任务{
@凌驾
受保护的空位背景(空位…空位){
mSpellCursor=fetchCursor();
返回null;
}

您可以在这里发布您的适配器实现吗。