RecyclerView不显示通过捆绑包从列表传递的数据

时间:2019-03-03 18:53:28

标签: android android-recyclerview

我在一个对话框片段中有一个recyclerview,该片段具有一个选项卡布局,其中包含一个可检查项列表和一个按钮。选择一个项目后,单击该项目将其通过捆绑包传递到该父对话框片段,如下所示:

doneBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            retrieveSessions();
        }
    });

private void retrieveSessions() {
    NewProgramDialog dialog = new NewProgramDialog(); // parent dialog fragment is passed the list items here

    Bundle bundle = new Bundle();
    String s = new Gson().toJson(myItemsList);
    bundle.putString(KEY_MY_SESSIONS, s);
    bundle.putBoolean("from_selections", true);

    dialog.setArguments(bundle);
    dialog.show(getChildFragmentManager(), "NewProgramDialog");
    dismiss();
}

recyclerview的数据通过上面显示的捆绑包接收,并且使用Gson检索列表项并将其转换为String类型,对数据库运行查询以获取会话模型类信息。 这些会话将添加到适配器数组列表中,如下所示:

private void populateSessions() {

    addProgramSessionsRV.setHasFixedSize(true);
    addProgramSessionsRV.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));

    // TODO: 2/05/2019 show the added sessions here

    Bundle bundle = getArguments();
    if (bundle != null) {
        String s = bundle.getString(KEY_MY_SESSIONS); / getting the items from bundle
        myItemsList = new Gson().fromJson(s, ArrayList.class); // parsing to List<String> type
        Log.d(TAG, "Items size:\t" + myItemsList.size()); //works
    }

    adapter = new AddProgramSessionsAdapter(getActivity(), sessionsList);
    addProgramSessionsRV.setAdapter(adapter);
    Log.d(TAG, "Items in adapter:\t" + adapter.getItemCount()); // shows total items received from bundle

    for (String s : myItemsList){
        Log.d(TAG, "Ids:\t" + s);

        Sessions session = SQLite.select()
                .from(Sessions.class)
                .where(Sessions_Table.prog_sessionId.eq(s))
                .querySingle();

        sessionsList.add(session);
        Log.d(TAG, "Session List Size:\t" + sessionsList.size());

        adapter = new AddProgramSessionsAdapter(getActivity(), sessionsList);
       addProgramSessionsRV.setAdapter(adapter);
       Log.d(TAG, "Items in adapter:\t" + adapter.getItemCount());

    }

}

仍然ui不会显示此方法中的任何项目。我已使用此代码段手动添加了数据,它在视图中显示了Session 1项目,但在for循环中未显示来自db的数据。 测试案例:

    Sessions sessions = new Sessions();
    sessions.setSession_name("Test one");
    sessions.setProgram_id("234");
    sessions.setProg_sessionId("91");
    sessionsList.add(sessions);

    adapter = new AddProgramSessionsAdapter(getActivity(), sessionsList);
    addProgramSessionsRV.setAdapter(adapter);
    Log.d(TAG, "Items in adapter:\t" + adapter.getItemCount());

在for循环数据库查询中,为什么没有将适配器设置为包含项的列表的任何原因(有效)?请向我解释为什么ui不显示任何内容。谢谢。

这是适配器代码:

public class AddProgramSessionsAdapter extends RecyclerView.Adapter<AddProgramSessionViewHolder> {

private final Context context;
private List<Sessions> itemsList;

public AddProgramSessionsAdapter(Context context, List<Sessions> itemsList) {
    this.context = context;
    this.itemsList = itemsList;
}

@Override
public AddProgramSessionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.add_program_sessions_items_layout, parent, false);
    return new AddProgramSessionViewHolder(view);
}

@Override
public void onBindViewHolder(AddProgramSessionViewHolder viewholder, int position) {
    Sessions sessions = itemsList.get(position);

    viewholder.add_program_sessionNameTV.setText(sessions.session_name);
    Picasso.with(context)
            .load(R.drawable.app_logo_resized)
            .into(viewholder.add_program_sessionIV);

}

@Override
public int getItemCount() {
    if (itemsList == null) {
        return 0;
    }
    return itemsList.size();
}
}

0 个答案:

没有答案