5

私は現在、愚かな問題に直面しています。カスタムアダプター(CursorAdapterを拡張)を備えたリストビューがあります。

さまざまなボタン (共有、いいね、削除) を備えたカスタム ビューが表示されます。削除ボタンには、アイテムの削除を確認するための警告ダイアログがあります。ユーザーが確認すると、アイテムはデータベースから削除されます。ここまではすべて正常に動作します。

私の質問は、新しいデータセットでリストビューを効率的に更新するにはどうすればよいですか?

どうもありがとうございました。

コード:

public class CommentCursorAdapter extends CursorAdapter{
    (...)
    @Override
    public void bindView(View view, Context arg1, Cursor cursor) {
    (...)
        holder.list_item_comment_discard_btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            final String _id = v.getTag().toString();
            AlertDialog.Builder builder = new    AlertDialog.Builder(mActivity);
            builder.setTitle("Delete");
            builder.setMessage("Do you want to delete "+_id);
            builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User clicked OK button
                    DBAdapter dba = new DBAdapter(mActivity);
                    dba.open();
                    dba.remove(_id);
                    Log.i("TAAG", "removed: "+_id);
                    dba.close();    

                // How to update the listview ??                                    
                }
            });
            builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });

            AlertDialog d = builder.create();
            d.show();
        }
        });
        holder.list_item_comment_discard_btn.setTag(_id);
        (...)
    }
    (...)
}
4

4 に答える 4

9

LoaderManager を使用してカーソルのライフサイクルを管理する場合 (これが正しい方法です)、restartLoader を呼び出してから、アダプターのカーソルを onLoadFinished に (再) 設定するだけです。リストビューは更新されたデータで再読み込みされます。

アクティビティまたはフラグメント内のカーソルの管理は、非常にシンプルで直接的な方法で行う必要があります。

  • カーソルを初期化するには:

    public void onCreate(Bundle savedInstanceState) { // or onStart
    // ...
    this.getLoaderManager().initLoader(MY_CURSOR_ID, null, this);
    // ...
    }
    
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // TODO: create a cursor loader that will load your cursor here
    }
    
    public void onLoadFinished(Loader<Cursor> arg0, final Cursor arg1) {
    // TODO: set your cursor as the cursor of your adapter here;
    }
    
    public void onLoaderReset(Loader<Cursor> arg0) {
    // TODO: set null as the cursor of your adapter and 'free' all cursor
    // references;
    }
    
  • そして、データをリロードする必要がある場合:

    this.getLoaderManager().restartLoader(MY_CURSOR_ID, null, this);
    
于 2013-03-06T04:49:42.590 に答える
1

DB内のアイテムを削除した後、使用しているこの匿​​名の内部クラスで付与notifyDataSetChangedされたを呼び出します。これは、内にあるため、への参照CommentCursorAdapterが必要になります。これにより、の更新がトリガーされます。finalthisCursorAdapterListView

http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()

于 2013-03-05T15:18:08.050 に答える
1

コールし cursor.requery(); notifyDataSetChanged(); ます。

実際のパフォーマンスが必要な場合は、AsyncTaskLoaderを使用してください。

于 2013-03-05T15:17:03.893 に答える
1

Cursor次のように再クエリします。

public void onClick(DialogInterface dialog, int id) {
    // User clicked OK button
    DBAdapter dba = new DBAdapter(mActivity);
    dba.open();
    dba.remove(_id);
    Log.i("TAAG", "removed: "+_id);
    dba.close();    
    cursor.requery();
    notifyDataSetChanged();                                   
}   

を呼び出して、データが変更されたことnotifyDataSetChanged()を通知し、アダプタを再度構築する必要があります。Adapter

再クエリに時間がかかる場合、これは負荷の高い操作になります。別のスレッドで行うことを検討してください。

于 2013-03-05T15:17:22.797 に答える