如何解析此JSON并检索mainactivity中的数据并将其设置为recyerview

时间:2017-09-13 10:32:56

标签: android arrays json parsing android-recyclerview

{
"data": [
    {
        "0": [
            [
                {
                    "fans_id": "2",
                    "ans": "ans",
                    "qst_id": "15",
                    "ans_uid": "20",
                    "ans_date": "2017-09-05 22:13:58"
                },
                {
                    "fans_id": "3",
                    "ans": "8770577495",
                    "qst_id": "15",
                    "ans_uid": "20",
                    "ans_date": "2017-09-09 03:23:51"
                }
            ]
        ],
        "forum_id": "15",
        "qst": "cxvcxv",
        "qst_description": "xcvcx\n",
        "qst_uid": "20",
        "q_status": "Enable",
        "user_id": "20",
        "univer_id": "1",
        "fullname": "Deepak Verma",
        "branchid": "10",
        "semid": "1",
        "collegeid": "1",
        "mobile": "9685318196",
        "bio": "",
        "enrollno": "",
        "username": "",
        "password": "e10adc3949ba59abbe56e057f20f883e",
        "u_status": "Enable",
        "user_image": "user.png",
        "loginwith": "",
        "otp": ""
    }

]
}

这是我的JSON数据,我如何解析它并将此数据设置到适配器,以便它可以正常工作并在回收器视图上运行。问题主要发生在适配器部分无法设置两个不同的数据列表,我使用两个模型类一个父模型和第二个子模型

这是我的适配器代码

public class Adapter_theory extends RecyclerView.Adapter<Adapter_theory.Viewholder> {
private Context context;
private LayoutInflater inflater;
List<Model_theory> listtheorydata= Collections.emptyList();
Model_theory current;
int currentPos=0;
Intent intent;

public Adapter_theory(Context context, List<Model_theory> listtheorydata) {
    this.context = context;
    this.listtheorydata = listtheorydata;
    inflater= LayoutInflater.from(context);

}

@Override
public Adapter_theory.Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view=inflater.inflate(R.layout.view_theory, parent,false);
    Viewholder holder=new Viewholder(view);
    return holder;
}

@Override
public void onBindViewHolder(Adapter_theory.Viewholder holder, final int position) {
    final Viewholder theoryholder = (Viewholder) holder;
    Model_theory  current = listtheorydata.get(position);
    theoryholder.theorytittle.setText(current.mt_tittle);
    final String title = Html.fromHtml(current.mt_tittle).toString();
    final String theory = Html.fromHtml(current.myheory).toString();
    final String image = Html.fromHtml(current.myheory + "<img src=\"http://consoledude.in/ckfinder/userfiles/images/").toString();
    final String secondimage = current.getMyimages();




    theoryholder.btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            theoryholder.btn.toggle();

        }
    });

    theoryholder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Model_theory current = listtheorydata.get(position);
            intent = new Intent(context,TheoryDetailActivity.class);
            intent.putExtra("t_title",title);
            intent.putExtra("theory",theory);
            intent.putExtra("second",secondimage);
            intent.putExtra("abc",image);
            context.startActivity(intent);
            ((Activity)context).finish();
        }
    });




}

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

public class Viewholder extends RecyclerView.ViewHolder {
    TextView theorytittle;
    PlayPauseView btn;
    public Viewholder(View itemView) {
        super(itemView);
        theorytittle = (TextView)itemView.findViewById(R.id.theorytittle);
        btn = (PlayPauseView)itemView.findViewById(R.id.play_pause_view);
    }
}
}

1 个答案:

答案 0 :(得分:0)

我建议您为此json创建一个模型类,然后使用gson库将其解析为模型,然后您可以轻松地在recyclerview中使用该模型。

您可以通过从下面显示的链接下载jar文件来添加gson库: https://mvnrepository.com/artifact/com.google.code.gson/gson/2.3.1