android – How to parse this JSON and retrieve data in the mainactivity and set it to recyclerview-ThrowExceptions

Exception or error:
{
"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": ""
    }

]
}

This is my JSON data ,how could I parse it and set this data to the adapter so that it works fine and run on recycler view. The problem mainly occurs in the adapter part is not able to set two different data list, I am making it using two model class one parent model and second child model

This is my Adapter code

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);
    }
}
}
How to solve:

I would suggest you to create a model class for this json and then using gson library to parse it into model, then you can use that model in recyclerview easily.

you can add gson library by downloading the jar file from the link shown below:
https://mvnrepository.com/artifact/com.google.code.gson/gson/2.3.1

Leave a Reply

Your email address will not be published. Required fields are marked *