-3

I'll Follow this SO links but can't helpful for me:

I am trying to send JSon Subcategory data from one activity to another activity

see below Json format:

enter image description here

In Above Json data is a get Main category's and subcategory get sub data

now i'll get both data perfectly in one activity.

But i am trying to Click on Main category's data and each main subcategory data pass on another activity

see below image to batter understanding:

enter image description here

HOPE all users getting me

Now for this what i tried

MainActivity.java class For getting JsonData:

final JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                url, jsonObject, new Response.Listener<JSONObject>() {


            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());

                try {

                    JSONArray jArray = response.getJSONArray("data");

                    for (int i = 0; i < jArray.length(); i++) {
                        JSONObject json_data = jArray.getJSONObject(i);

                        Pojo dataPojo = new Pojo();
                        dataPojo.setCatagory_id(json_data.getString("category_id"));
                        dataPojo.setCatagory_name(json_data.getString("category_name"));
                        dataPojo.setCatagory_thumbnailUrl(json_data.getString("category_image"));
                        dataList.add(dataPojo);

                        JSONArray jArray1 = json_data.getJSONArray("subcategory");

                    }

                    dataAdapter = new DataAdapter(dataList);
                    dataAdapter.setClickListener(jsonDataClickListener);
                    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
                    recyclerView.setLayoutManager(mLayoutManager);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                    //recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
                    recyclerView.setAdapter(dataAdapter);
                    dataAdapter.notifyDataSetChanged();

                }

MainActivity.java class for Passing SubCategory :

OnItemClickListener jsonDataClickListener = new OnItemClickListener() {
    @Override
    public void onItemClick(Pojo item) {
        Intent intent = new Intent(MainActivity.this, SubCategoryData.class);
        intent.putExtra("Key", (Serializable) dataList);
        startActivity(intent);
    }
};

In Above code it's give me a myList size is 0

Pojo.java Model class:

public class Pojo extends Base implements Serializable{

    private String catagory_id;
    private String catagory_name;
    private String catagory_thumbnailUrl;
    private String childListSize;
    private ArrayList<Pojo2> subCatagoryArrayList;

    public ArrayList<Pojo2> getSubCatagoryArrayList() {
        return subCatagoryArrayList;
    }

    public void setSubCatagoryArrayList(ArrayList<Pojo2> subCatagoryArrayList) {
        this.subCatagoryArrayList = subCatagoryArrayList;
    }

    public String getCatagory_id() {
        return catagory_id;
    }

    public void setCatagory_id(String catagory_id) {
        this.catagory_id = catagory_id;
    }

    public String getCatagory_name() {
        return catagory_name;
    }

    public void setCatagory_name(String catagory_name) {
        this.catagory_name = catagory_name;
    }

    public String getCatagory_thumbnailUrl() {
        return catagory_thumbnailUrl;
    }

    public void setCatagory_thumbnailUrl(String catagory_thumbnailUrl) {
        this.catagory_thumbnailUrl = catagory_thumbnailUrl;
    }

}

Pojo2.java Model class for Subcategory :

public class Pojo2  {

    private String subcatagory_id;
    private String subcatagory_name;

    /*public Pojo2(String SubId, String SubName){
        super(SubId, SubName);
    }*/

    public String getSubcatagory_id() {
        return subcatagory_id;
    }

    public void setSubcatagory_id(String subcatagory_id) {
        this.subcatagory_id = subcatagory_id;
    }

    public String getSubcatagory_name() {
        return subcatagory_name;
    }

    public void setSubcatagory_name(String subcatagory_name) {
        this.subcatagory_name = subcatagory_name;
    }
}

UPDATE

When i used @Birju answer to send ArrayList it's work for me but where i receive data then it's give me a error.

java.util.ArrayList cannot be cast to java.lang.String

and for Receiving a data i'll use Below Code:

Intent intent = getIntent();
        Bundle b = intent.getExtras();

        if(b!=null)
        {

            String j =(String) b.getSerializable("Key");
            subId.setText(j);
        }

BUT ABOVE ERROR COME EVERY TIME How to i solve

Ali
  • 3,346
  • 4
  • 21
  • 56

2 Answers2

2

In Onclicklistner

ArrayList<Pojo> myList = new ArrayList<Pojo>(); // do not create new list....
                                                // every time creation its by default empty
intent.putExtra("SubCategory",myList);// use "dataList" instade of mylist

Instead of mylist you need to pass the dataList that you got from the json response

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
BiRjU
  • 733
  • 6
  • 23
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171529/discussion-between-birju-and-mohammad-ali). – BiRjU May 22 '18 at 10:01
  • so why your responce @Override public void onItemClick(Base item) ....take Base item??? – BiRjU May 22 '18 at 10:02
0

Since you're using Serializable interface to pass your data:

  1. You must ensure that all inner field properties serializable => Your Pojo2 must also implement Serializable

  2. In your received activity, call:

    List<Pojo> story = (List<Pojo>) intent.getSerializableExtra("SubCategory");
    

    to get back the data.

Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51