3

複数のローダーを起動して、カーソルからリストビューにデータをロードしています。リストビューにすべてのカーソルのデータを表示したいのですが、リストビューのデータは 2 秒間隔ですぐに変更され、最後のカーソルのデータのみが表示されます。私を助けてください。どこが間違っているのですか?カーソルをマージする必要がありますか?

私のフラグメントの onCreateView() 内:

  for (int i = 0; i < idsArray.length; i++) {
        Bundle b = new Bundle();
        b.putInt("id", idsArray[i]);
        getLoaderManager().initLoader(idsArray[i], b, this);  //initiating multiple loaders
    }

    adapter = new CustomAdapter(getActivity(), R.layout.list_row, null, column_names_array, ui_ids_array, 0);
    listView.setAdapter(adapter);

およびローダーのコールバック メソッド:

@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle b) {
    Builder uriBuilder = MY_PROVIDER_URI.buildUpon();
    uriBuilder.appendPath("categories"); 
    uriBuilder.appendPath(String.valueOf(b.getInt("id")));  
    uriBuilder.appendPath("data");
    return new CursorLoader(getActivity(), uriBuilder.build(), null, null, null, null); 
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    if(adapter != null && data != null){ 
        adapter.swapCursor(data); 
        adapter.notifyDataSetChanged(); 
    } 
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
        adapter.swapCursor(null);
}

そして、ここに私のアダプタークラスがあります:

public class CustomAdapter extends SimpleCursorAdapter {
Cursor cursor;
Context context;
Activity activity;
int layout;

public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags ) {
    super(context, layout, c, from, to, flags);
    this.cursor = c;
    this.context = context;
    this.activity = (Activity) context;
    this.layout = layout;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(this.layout, parent, false);
}    

@Override 
public void bindView(View view, Context context, Cursor cursor) {
    super.bindView(view, context, cursor);
    ViewHolder holder = (ViewHolder) view.getTag();
    if (holder == null) {
        holder = new ViewHolder();
        //finding UI elements in list row here
        view.setTag(holder);
        }  
         //binding data to views of viewholder here
     } 

    static class ViewHolder {
    //variables of all my UI elements
     }
  }
4

1 に答える 1

2

あなたのループ:

for (int i = 0; i < idsArray.length; i++) {
    Bundle b = new Bundle();
    b.putInt("id", idsArray[i]);
    getLoaderManager().initLoader(idsArray[i], b, this);  //initiating multiple loaders
}

ローダーがデータのロードを完了すると、メソッドは複数のローダーを作成します。

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    if(adapter != null && data != null){ 
        // This line result in replacing the last Cursor by the new one
        adapter.swapCursor(data); 
        adapter.notifyDataSetChanged(); 
    } 
}

が呼び出されます。最後の Loader が Cursor のロードを完了すると、 swapCursorにより、そのデータが最後の Cursor を置き換えます。

CursorAdapterのドキュメントを見ると、この CursorAdapter は一度に 1 つの Cursor しか管理できないことがわかります。

問題を解決するには、必要なすべてのデータを単一のクエリにクエリして、単一のカーソルを作成する必要があります。

于 2013-06-13T08:57:18.813 に答える