【问题标题】:How to display list array data from model class in recyclerview adapter according to their position?如何根据位置显示来自recyclerview适配器中模型类的列表数组数据?
【发布时间】:2017-03-27 17:27:12
【问题描述】:

如下 json 响应,我想在 recyclerview 中显示 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
}

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


}

【问题讨论】:

    标签: android json arraylist android-recyclerview


    【解决方案1】:

    RecyclerView 回收视图,因此得名。当您尝试将视图添加到您的mainLayout 时,请确保首先删除所有视图,因为如果它已经创建,那么它包含先前添加的视图。这就是为什么您会看到所有之前添加的布局答案。

    一个建议:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        刚刚在 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);
                    }
        
            }
        

        【讨论】:

          猜你喜欢
          • 2021-12-07
          • 2018-09-21
          • 2016-11-09
          • 1970-01-01
          • 2019-12-10
          • 2018-05-29
          • 1970-01-01
          • 2020-10-06
          • 1970-01-01
          相关资源
          最近更新 更多