如何将JsonArray从一个活动传递到另一个活动

时间:2018-05-19 06:25:00

标签: java android json arraylist

我会关注这个SO链接,但对我没有帮助:

我正在尝试将JSon Subcategory数据从一个活动发送到另一个活动

见下面的Json格式:

enter image description here

在Json之上data是获取主类别和subcategory获取子数据

现在我将在一项活动中完美地获得这两项数据。

但是我试图点击主要类别的数据,每个主要子类别数据都会传递另一项活动

见下图,以了解击球手:

enter image description here

希望所有使用我的用户

现在为此我尝试了

MainActivity.java 类获取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 类:

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

在上面的代码中,它给我一个myList大小为0

Pojo.java 型号类:

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

更新

当我使用@Birju的答案发送ArrayList时,它对我有用,但是在我收到数据的地方,那就是给我一个错误。

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

并且对于接收数据,我将使用以下代码:

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

        if(b!=null)
        {

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

但是每次都有错误如何解决

3 个答案:

答案 0 :(得分:2)

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

而不是mylist您需要传递从json dataList

获得的 response

答案 1 :(得分:0)

由于您使用Serializable界面传递数据:

  1. 您必须确保所有内部字段属性serializable =&gt;您的Pojo2还必须实施Serializable

  2. 在您收到的活动中,请致电:

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

    取回数据。

答案 2 :(得分:-1)

使所有响应pojo类实现Serializable然后以这种方式传递..

  Intent intent = new Intent(UserInputActivity.this,MapsActivity.class);
                intent.putExtra("Response", response1);
                startActivity(intent);

然后像这样在下一个活动中获取数据..

        response = (Response) getIntent().getSerializableExtra("Response");

尝试像这样发送pojo的arrya ..

    private List<ReviewsItem>reviewsItems=new ArrayList<>();
                Intent intent=new Intent(PlaceDetails.this,ReviewDetailsActivity.class);
            intent.putExtra("Review", (Serializable) reviewsItems);
            startActivity(intent);

下一个活动..调用oncreate方法。

  private List<ReviewsItem>reviewsItems=new ArrayList<>();
  reviewsItems= (List<ReviewsItem>) getIntent().getSerializableExtra("Review");