Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何根据模型类在recyclerview适配器中的位置显示列表数组数据?_Android_Json_Arraylist_Android Recyclerview - Fatal编程技术网

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

Android 如何根据模型类在recyclerview适配器中的位置显示列表数组数据?,android,json,arraylist,android-recyclerview,Android,Json,Arraylist,Android Recyclerview,正如下面的json响应一样,我想在recyclerview中显示poolquestions,以及与该poolquestion相关的答案。我已经为recyclerview定义了一个适配器,并为poolquestions和answers定义了一个模型类来保存json值。但我的问题是,当我要从adapter中的模型类提取数据时,所有的poolanswers都显示在所有父poolquestions中 这是我的json: { "data": [ { "pooltitle": "Wh

正如下面的json响应一样,我想在recyclerview中显示
poolquestions
,以及与该
poolquestion
相关的答案。我已经为recyclerview定义了一个适配器,并为
poolquestions
和answers定义了一个模型类来保存json值。但我的问题是,当我要从adapter中的模型类提取数据时,所有的
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
}
主要活动任务代码:

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

        }
私有列表pollDataArrayList;
private List pollQSTOptionsList=new ArrayList();
如果(“1”。相等信号情况(状态)){
试一试{
JSONArray data=js.getJSONArray(“数据”);
对于(int i=0;i
适配器类:

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


}
公共类PollAdapter2扩展了RecyclerView.Adapter{
私有列表数据列表;
语境;
公共类MyViewHolder扩展了RecyclerView.ViewHolder{
TextView txt_poll_问题;
ProgressBar progress1;
线性布局主布局;
公共MyViewHolder(视图){
超级(视图);
txt_poll_question=(TextView)view.findViewById(R.id.txt_poll_question);
mainLayout=(LinearLayout)view.findViewById(R.id.mainLayout);
}
}
公共轮询适配器2(上下文mContext、列表数据列表){
this.dataList=dataList;
this.context=mContext;
}
@凌驾
公共MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图v=LayoutInflater.from(parent.getContext())
.充气(R.layout.card_poll_qst__ans,parent,false);
MyViewHolder viewHolder=新的MyViewHolder(v);
返回视图持有者;
}
@凌驾
公共无效onBindViewHolder(最终MyViewHolder,内部位置){
final PollQstWithOptions poll=dataList.get(位置);
holder.txt_poll_question.setText(poll.getPollQstName());

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

一项建议:


不要将答案视图动态添加到
线性布局
,您应该在回收器视图中使用两个不同的支架,每个问题和答案对应一个支架。请参阅onBindView支架中的,您的循环应该是这样的

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

}
List pollsDataArraylist=dataList.get(position.getPollsDataArrayList();
对于(int i=0;i
刚刚在onBindView中删除了下面的代码,我得到了自己的问题解决方案

 for(int i=0; i<dataList.size();i++){
            PollQstWithOptions itemsData = dataList.get(i);
for(int i=0;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);
            }

    }