尝试遍历recyclerViewAdapter中的嵌套列表

时间:2019-05-11 11:30:27

标签: java android recycler-adapter nested-lists

我正在尝试遍历android recyclerviewAdapter中的嵌套列表。

public class ParentObject{
String mTitle;
List<ChildObject> mChildsList;
}
    public ParentObject(String title, List<ChildObject> childsList) {
    this.mTitle = title;
    this.mChildsList = childsList;
}

public String getmTitle() {
    return mTitle;
}

public List<ChildObject> getmChildsList() {
    return mChildsList;
}

这是我的ParentObject和另一个ChildObject。现在,我试图像这样在此objectModel中存储数据。

//JSON PARSING

List<ParentObject> parentObjectList = new ArrayList<>();

if (data.length() > 0) {
    for (int i = 0; i < data.length(); i++) {

        JSONObject pObject = data.getJSONObject(i);

        String title = pObject.getString("Title");

        JSONArray childData = new 
            JSONArray(pObject.getString("childData"));

                List<ChildObject> childObjectList = null;

                if (childData.length() > 0) {
                    childObjectList = new ArrayList<>();

                    JSONObject child = childData.getJSONObject(i);

                    ChildObject childObject = new 
                       ChildObject(child.getString("child_title"));
                       // and other keyvalues

                    childObjectList.add(childObject);
                }

                    ParentObject parentObject = new ParentObject(
                            title,
                            childObjectList
                    );
                    parentObjectList.add(parentObject);
    }
}

现在所有JSON数据都正确存储在列表中,并且我已经在列表中列出了(Parent_LIST(title,Child_LIST))

这是我的适配器

public class ParentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<ParentObject> parentList;
private Context activity;

public ParentAdapter(List<parentList> pList, Context activity) {
    this.parentList = pList;
    this.activity = activity;
}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    if (viewType == 0) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.parent_layout, parent, false);
        return new ViewHolder2(v);
    } else {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.child_layout, parent, false);

        return new ViewHolder(v);
    }
}


@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    final ParentObject pObject = parentList.get(position);

    switch (holder.getItemViewType()) {
        case 0:
            ViewHolder2 viewHolder2 = (ViewHolder2) holder;
            viewHolder2.Parent_titleTV.setText(pObject.get());
            break;
        default:
            ViewHolder viewHolder = (ViewHolder) holder;
            final ChildObject postO = pObject.getmChildsList().get(position);
            viewHolder.Child_Title.setText(postO.getP_title());

}

@Override
public int getItemViewType(int position) {

    return position;
}

@Override
public int getItemCount() {
    return parentList.size();
}


class ViewHolder extends RecyclerView.ViewHolder {

    TextView chilld_Title;
    CardView _cardView;

    ViewHolder(View itemView) {

        super(itemView);
        chilld_Title = itemView.findViewById(R.id.postTitle);
        _cardView = itemView.findViewById(R.id.postCard);
    }
}

class ViewHolder2 extends RecyclerView.ViewHolder {

    TextView Parent_titleTV;

    ViewHolder2(View itemView) {

        super(itemView);
        Parent_titleTV = itemView.findViewById(R.id.title);
    }
}
}

我在应用程序中只能看到标题为0的标题,并且没有其他子数据。

预期输出 [PARENT_TITLE_AT_0] - [CHILD_TITLE_AT_0] - [CHILD_TITLE_AT_1] - [CHILD_TITLE_AT_2] - [CHILD_TITLE_AT_3] [PARENT_TITLE_AT_1] - [CHILD_TITLE_AT_0] - [CHILD_TITLE_AT_1] - [CHILD_TITLE_AT_2] - [CHILD_TITLE_AT_3] . . . [PARENT_TITLE_AT_n] - [CHILD_TITLE_AT_0] - [CHILD_TITLE_AT_1] - [CHILD_TITLE_AT_2] - [CHILD_TITLE_AT_3]

实际输出

[PARENT_TITLE_AT_0] (parent title at 0 Index)

0 个答案:

没有答案