【问题标题】:Take a list in list according to json in recyclerview根据recyclerview中的json取list中的list
【发布时间】:2018-09-18 12:54:20
【问题描述】:

我正在构建一个使用动态json 并从服务器获取数据的应用程序。 json 数据还包含嵌套的json array

此数据需要在RecyclerView 中呈现。我设法在RecyclerView 中显示了部分数据。

我展示了父数组数据,但是如何显示对象数组的数据以查看其中也有数组。 我的json喜欢

[

    {"_id":"5a8fa892f399c80f95ac0c81",

    object: "1", 

    type:"type-1",

    question : "Question lorem ipsum ?"

    answer : [  
{  
    "id":"A",
    "is_correct":"false",
    "_id":"5a8fa892f399c80f95ac0c85",
    "matching_id":[  
        ""
    ],
    "text":"text option 1"
},
{  
    "id":"B",
    "is_correct":"true",
    "_id":"5a8fa892f399c80f95ac0c84",
    "matching_id":[  
        ""
    ],
    "text":"text option 2"
},
{  
    "id":"C",
    "is_correct":"false",
    "_id":"5a8fa892f399c80f95ac0c83",
    "matching_id":[  
        ""
    ],
    "text":"text option 3"
  }
 ]
 },

   {"_id":"5a8fa892f399c8081",

    object: "2", 

    type:"type-2",

    question : "Question lorem ipsum 2?"

    answer : [  
{  
    "id":"A",
    "is_correct":"false",
    "_id":"5a8fa892f399c80f95ac0c85",
    "matching_id":[  
        ""
    ],
    "text":"text option 1a"
},
{  
    "id":"B",
    "is_correct":"true",
    "_id":"5a8fa892f399c80f95ac0c84",
    "matching_id":[  
        ""
    ],
    "text":"text option 2a"
},
{  
    "id":"C",
    "is_correct":"false",
    "_id":"5a8fa892f399c80f95ac0c83",
    "matching_id":[  
        ""
    ],
    "text":"text option 3a"
  }
 ]
 }    
]

我已经成功地展示了问题部分,但如何在Recyclerview's view 中展示答案部分。每个问题都有答案选项,所以在每个视图中我都必须显示答案选项,取自这个 json 进入recycelrview

【问题讨论】:

    标签: android json arraylist android-recyclerview


    【解决方案1】:

    我看过你的 json 并且看到我可以说你应该在这里使用 Expandable recycler view 而不是 recyclerview。

    看这里 https://github.com/thoughtbot/expandable-recycler-view

    【讨论】:

    • 但我想在一个视图中显示数据(不想添加可扩展视图)
    【解决方案2】:

    看看这段代码不是你想做的……所以复制粘贴效果不好。 看逻辑

    /**
     * @param args
     */
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //ypu should do this:
        //you adapter constructor has a JSONArray in paramater
    
    
    
    
    
        //List<QuestionObjects> questionObjects = new ArrayList<>();
    
        //List<AnswerObjects> answersObjects = new ArrayList<>();
    
    
    
        try {
            String jsonquest = response.body().string();
            Log.d(TAG, "onResponse: jsonQuest: " + jsonquest);
            JSONArray questionwithresponses = new JSONArray(jsonquest);
    
    
    
    
            /*for (int i = 0; i < question.length(); i++) {
    
                JSONObject e = question.getJSONObject(i);
    
                newQuestionObject._id = e.getString("_id");
                newQuestionObject.question = e.getString("question");
                newQuestionObject.type = e.getString("type");
    
                JSONArray answers =e.getJSONArray("answers");
    
                for (int j = 0 ; j <answers.length(); j++) {
    
                    JSONObject a = answers.getJSONObject(j);
    
                    Answers newAnswers = new Answers();
    
                    newAnswers.id = a.getString("id");
    
                    newAnswers.isCorrect = a.getString("is_correct");
    
                    newAnswers.idAnswer = a.getString("_id");
    
                    newAnswers.text = a.getString("text");
    
                    answersObjects.add(newAnswers);
    
                }
    
                newQuestionObject.answers = answersObjects;*/
            mExamView = (RecyclerView) findViewById(R.id.recyclerView);
    
      mAdapter = new QuestionAdapter(ExamActivity.this,questionwithresponses);
    
            mExamView.setHasFixedSize(true);
            mExamView.setAdapter(mAdapter);
        mExamView.setLayoutManager(newLinearLayoutManager(ExamActivity.this));
            submitBtn = (Button) findViewById(R.id.test_submit_btn);
            mExamView.setDuplicateParentStateEnabled(true);
    
    
            }
    
    
    
        }
        catch (IOException e) { e.printStackTrace();
       Toast.makeText(ExamActivity.this, "error in data",   
    
       Toast.LENGTH_SHORT).show();
    
            Log.e(TAG, "onResponse: IOException: " + e.getMessage());
        }
    
    
    }
    

    对于适配器

    公共类适配器{

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    
    
    
    
      public class QuestionAdapter extends
    
     RecyclerView.Adapter<RecyclerView.ViewHolder> {
    
     public static final String TAG = QuestionAdapter.class.getSimpleName();
    
     private Context context;
    
     private LayoutInflater inflater;
     JSONArray questionwithresponses;
     QuestionObjects current;
     private int currentPos=0;
     int total_types;
    
     //ArrayList<Answers>AnswerData;
    Answers answer;
    int getTotal_types;
    
    
     // Create constructor to initialize context and data sent from 
    
     ExamActivity
    
     //don't use this constructor...
    
     public QuestionAdapter(Context context, List<QuestionObjects> data, 
    
     ArrayList<Answers> AnswerData){
    
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.data = data;
        this.AnswerData= AnswerData;
    
       }
    //use this one
    public QuestionAdapter(Context context, JSONArray quesRep){
        this.context = context;
    
        this.questionwithresponses=quesRep;
    
    }
    public static class TextTypeViewHolder extends RecyclerView.ViewHolder{
    
        TextView txtTpe;
        CardView cardView;
    
        public TextTypeViewHolder(View itemView){
            super(itemView);
    
        }
    }
    
    
     //    Inflate the layout when viewholder created
    
    @Override
    
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int 
    
    viewType){
    //        Context context =parent.getContext();
     //
     //        LayoutInflater inflater = LayoutInflater.from(context);
    
        View view = inflater.inflate(R.layout.match_the_following_questions, 
      parent, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    
    }
    
     //    Bind data
    
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int  
    
    currentPos ) {
    
    //        Get current position of item i recycler view to bind data and  
    
     assign values from list
    
        //if some raisons you have to use object create by  
    
     //yourself(QuestionObjects,Answers) you can rebuild them here
        //otherwise you can use JSONObject directly
        JSONObjet current=questionwithresponses.get(currentPos);
    
    
        MyHolder myHolder = (MyHolder) holder;
    
    
        myHolder.questionU.setText(current.getString("question"));
        myHolder.qNumber.setText(current.getString(""));
    
        JSONArray answer=object.getJSONOArray("answer")
                //to liste answers
                for(int i=0;i<answers.lenght();i++)
                {
                JSONObject answer=answers.getJSONObject(i);
                 //you can get all you need about anwers here
                //exemple
    
                 myHolder.op1.setText(answer.getString("text"));//or whatever    
    
      you want
                }
    
    
    
    
    
        Log.d(TAG, "onBindViewHolder: "+current +"    myHolder"  
    
    
       +current.question_number);
     }
    
    
    
    @Override
    public long getItemId(int currentPos){
        return currentPos;
    
    }
    
    @Override
    public int getItemViewType(int position) {
    
     return (position ==questionwithresponses .lenght() ?          
    
     R.layout.activity_exam : R.id.test_submit_btn) ;
    
    }
    
    //    return total item from List
    @Override
    
    public int getItemCount() {
        return questionwithresponses .lenght();
    }
    
    class MyHolder extends RecyclerView.ViewHolder{
    
        TextView questionU;
        TextView qNumber;
        TextView op1, op2, op3, op4;
    
      //        create constructor to get widget reference
    
        public MyHolder(View itemView) {
    
            super(itemView);
    
            questionU = itemView.findViewById(R.id.question);
            qNumber = itemView.findViewById(R.id.questionnumber);
    
            op1= itemView.findViewById(R.id.option1);
            op2 =itemView.findViewById(R.id.option2);
            op3 =itemView.findViewById(R.id.option3);
            op4 =itemView.findViewById(R.id.option4);
        }
    }
    
    }
    
    
    }
    
    }
    

    【讨论】:

    • 我已经完成了这一步,在这之前我想把这个for循环的数据显示到recyclerview的每个视图中
    • 这里是code的链接
    • 我无法获取您的代码,因为没有授权。只需复制适配器内的代码,然后通过编辑您的问题将其作为代码传递。
    • 检查我已添加权限 [Link] (drive.google.com/drive/folders/…)
    猜你喜欢
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 2016-05-29
    相关资源
    最近更新 更多