-1

As below json response, i want to show poolquestions with answers related to that poolquestion in recyclerview. already i have defined a adapter for recyclerview and model class for poolquestions and answers to save json values. but my question is while i am going to extract data from model class in adapter all poolanswers are showing in all parent poolquestions.

Here is my 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
}

Main Activity AsyncTask code:

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);

        }

Adapter class:

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();
    }


}
Saeed Zhiany
  • 2,051
  • 9
  • 30
  • 41
pb123
  • 489
  • 3
  • 9
  • 25

3 Answers3

0

RecyclerView recycles the views, hence the name. When you try to add view to your mainLayout, make sure to remove all views first, because if it had already been created then it contains the previously added views. That's why you see all previously added answers to the layout.

One suggestion:

Instead of adding answer view dynamically to the linearLayout, you should use 2 different holders in your recycler view, one for each of your question and answer. Refer this

Community
  • 1
  • 1
Binary Baba
  • 1,953
  • 2
  • 16
  • 23
0

In your onBindView holder your for loop should be like this

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()));

}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Rajesh Gosemath
  • 1,812
  • 1
  • 17
  • 31
0

just remvoed the below code in onBindView, i got my own question solution.

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

the final code is,

@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);
            }

    }
pb123
  • 489
  • 3
  • 9
  • 25