如何根据它们的位置在recyclerview适配器中显示模型类的列表数组数据?

时间:2016-11-14 05:52:00

标签: android json arraylist android-recyclerview recycler-adapter

如下面的json响应,我想在recyclelerview中向poolquestions显示与poolquestion相关的答案。我已经为poolquestions的recyclerview和模型类定义了一个适配器,并解答了保存json值的问题。但我的问题是,当我要从适配器中的模型类中提取数据时,所有poolanswers所有父poolquestions中都会显示。

这是我的json:

{
  "data": [
    {
      "pooltitle": "Who is the best Actor in India",
      "pooldetails": [
        {
          "poolquestions": "Who is the best actor in Bollywood?",
          "poolanswer": [
            {
              "answer": "Amir Khan",
              "percent": 38
            },
            {
              "answer": "Amitabh Bachhan",
              "percent": 25
            },
            {
              "answer": "Salman Khan",
              "percent": 0
            },
            {
              "answer": "Akshay Kumar",
              "percent": 38
            }
          ]
        },
        {
          "poolquestions": "Who has most female fan following?",
          "poolanswer": [
            {
              "answer": "Amitabh Bachhan",
              "percent": 13
            },
            {
              "answer": "Amir Khan",
              "percent": 38
            },
            {
              "answer": "Salman Khan",
              "percent": 13
            },
            {
              "answer": "Akshay Kumar",
              "percent": 38
            }
          ]
        },
        {
          "poolquestions": "Who is most popular actor in social media?",
          "poolanswer": [
            {
              "answer": "Amir Khan",
              "percent": 27
            },
            {
              "answer": "Amitabh Bachhan",
              "percent": 36
            },
            {
              "answer": "Salman Khan",
              "percent": 18
            },
            {
              "answer": "Akshay Kumar",
              "percent": 18
            }
          ]
        }
      ]
    }
  ],
  "status": 1
}

主要活动AsyncTask代码:

private List<PollsData> pollDataArrayList;
private List<PollQstWithOptions> pollQSTOptionsList = new ArrayList<>();

if ("1".equalsIgnoreCase(status)) {
            try {
                JSONArray data = js.getJSONArray("data");
                for (int i = 0; i < data.length(); i++) {
                    JSONObject detailsData = data.getJSONObject(i);

                    JSONArray pollQstArray = detailsData.getJSONArray("pooldetails");
                    for (int j = 0; j < pollQstArray.length(); j++) {
                        JSONObject poll = pollQstArray.getJSONObject(j);
                        String poolquestion_ = poll.getString("poolquestions");
                        pollDataArrayList = new ArrayList<>();
                        JSONArray poolanswerArray = poll.getJSONArray("poolanswer");
                        for (int k = 0; k < poolanswerArray.length(); k++) {
                            JSONObject pollOptions = poolanswerArray.getJSONObject(k);
                            String options = pollOptions.getString("answer");
                            int percent = pollOptions.getInt("percent");

                            PollsData pollDetails = new PollsData(options, percent);
                            pollDataArrayList.add(pollDetails);
                        }
                        PollQstWithOptions mPollQstWithOptions = new PollQstWithOptions(poolquestion_, pollDataArrayList);
                        pollQSTOptionsList.add(mPollQstWithOptions);
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            mAdapter = new PollAdapter2(ActivityPollDetails.this, pollQSTOptionsList);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(ActivityPollDetails.this);
            recyclerView.setLayoutManager(mLayoutManager);
            recyclerView.setHasFixedSize(true);
            recyclerView.setAdapter(mAdapter);

        }

适配器类:

public class PollAdapter2 extends RecyclerView.Adapter<PollAdapter2.MyViewHolder> {
    private List<PollQstWithOptions> dataList;
    Context context;

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView txt_poll_question;

        ProgressBar progress1;
        LinearLayout mainLayout;
        public MyViewHolder(View view) {
            super(view);

            txt_poll_question=(TextView)view.findViewById(R.id.txt_poll_question);
            mainLayout=(LinearLayout)view.findViewById(R.id.mainLayout);
        }

    }
    public PollAdapter2(Context mContext, List<PollQstWithOptions> dataList) {
        this.dataList = dataList;
        this.context=mContext;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_poll_qst_with_ans, parent, false);

        MyViewHolder viewHolder = new MyViewHolder(v);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final PollQstWithOptions poll = dataList.get(position);

        holder.txt_poll_question.setText(poll.getPollQstName());

        for(int i=0; i<dataList.size();i++){
            PollQstWithOptions itemsData = dataList.get(i);
            for(int j=0;j<itemsData.getOptionList().size();j++){
                System.out.print("child pos:: "+j);
                PollsData mPollsData = itemsData.getOptionList().get(j);

                TextView text = new TextView(context);
                text.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                text.setText(mPollsData.getAns1());
                holder.mainLayout.addView(text);
            }

        }

    }

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


}

3 个答案:

答案 0 :(得分:0)

RecyclerView回收视图,因此得名。当您尝试向mainLayout添加视图时,请务必先删除所有视图,因为如果已经创建了视图,则它包含以前添加的视图。这就是为什么你看到所有以前添加的布局答案的原因。

一个建议:

您应该在回收站视图中使用2个不同的持有者,而不是将答案视图动态添加到linearLayout,而不是每个问题和答案都有一个。请参阅this

答案 1 :(得分:0)

在你的onBindView持有者中,你的for循环应该是这样的

List<PollsData > pollsDataArraylist = dataList.get(position).getPollsDataArrayList();
for(int i = 0;i < pollsdataArraylist.size();i++){

    PollsData pData = pollsdatArraylist.get(i);
    textview1.setText(pData.getAnswer().toString());
    textview2.setText(String.valueOf(pData.getPrecent()));

}

答案 2 :(得分:0)

刚刚在onBindView中重新编写了以下代码,我得到了自己的问题解决方案。

 for(int i=0; i<dataList.size();i++){
            PollQstWithOptions itemsData = dataList.get(i);

最终代码是,

@Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final PollQstWithOptions poll = dataList.get(position);

        holder.txt_poll_question.setText(poll.getPollQstName());


            for(int j=0;j<poll.getOptionList().size();j++){
                PollsData mPollsData = poll.getOptionList().get(j);

                TextView text = new TextView(context);
                text.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                text.setText(mPollsData.getAns1());
                holder.mainLayout.addView(text);
            }

    }