将tabfragment recyclerview的arraylist项目传递给其他活动recyclelerview

问题描述 投票:-1回答:3

我已经将JSonarray数据作为字符串获取并将其添加到arraylist,即这是在选项卡片段回收器视图上完成的。

      JSONObject jsonObject = response.getJSONObject(index);
//                        Log.d("TAG", jsonObject.getString("title"));
                        int id =jsonObject.getInt("id");
                        String title=jsonObject.getString("title");
//                        Log.d("TAG", jsonObject.getString("description"));
                        String shortDec=jsonObject.getString("description");
                        String longDec=jsonObject.getString("short_description");
                        String imguUrl=jsonObject.getString("image");
                        String createdAt=jsonObject.getString("created_at");

//                        Toast.makeText(NewsDetailsActivity.this, "\nTitle: "+title+"\n Description:", Toast.LENGTH_SHORT).show();
                        NewsListGetter newsListGetter=new NewsListGetter(id, title, shortDec,longDec,imguUrl,createdAt);
                        arrayList.add(newsListGetter);
                        adapter.notifyDataSetChanged();

我有另一个叫做NewsDetailsAcivity的活动,我在其中实现了一个回收站视图。我想只将被点击的tab1fragment的recylerview中的数据传递给newsDetailActivity recylerview。

json arraylist recycler-adapter
3个回答
1
投票

您的响应已经在JSONArray中,无需存储在另一个JSON数组中。

看下面的代码。

RequestQueue requestQueue = Volley.newRequestQueue(this);

String url = "http://ec2-54-147-238-136.compute-1.amazonaws.com/hmc/api/getnewsfeeds?order=asc";
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
    @Override
    public void onResponse(JSONArray response) {
        for (int index = 0; index < response.length(); index++) {
            try {
                JSONObject jsonObject = response.getJSONObject(index);
                Log.d("TAG", jsonObject.getString("title"));
                Log.d("TAG", jsonObject.getString("description"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});
requestQueue.add(request);

0
投票

首先在String中使用你的Response.Listener

new Response.Listener<String>

然后将您的响应存储在JSONObject中,然后从存储的JSONObject中获取JSONArray。像这样:

  JSONObject data = new JSONObject(response);
  JSONArray responseArray = data.getJSONArray("data"); // here data is Key of your JsonArray.

希望这对你有所帮助!


0
投票

根据您的JSON响应您可以粘贴此代码。我在我的机器上尝试了这个并且正在工作,如果有任何问题,请告诉我

String json_url = "http://ec2-54-147-238-136.compute-.amazonaws.com/hmc/api/getnewsfeeds?order=asc";

JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
    for (int i = 0; i < response.length(); i++) {
        try {
            JSONObject jObj = response.getJSONObject(i);
            Log.d("TAG", jObj.getString("title"));
            Log.d("TAG", jObj.getString("description"));
        } catch (JSONException exc) {
            exc.printStackTrace();
        }
    }
}

}

如何在ArrayList中保存数据

创建SampleModelClass

public class SampleModelClass {

private String id;
private String description;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

}

现在在您的活动中解析JSON的位置,执行相同操作

 JSONArray root_array = root_obj.getJSONArray("YOUR_ARRAY_NODE_NAME");
        for (int i = 0; i < root_array.length(); i++) {

            SampleModelClass commonModels = new SampleModelClass();
            JSONObject array_object = root_array.getJSONObject(i);
            commonModels.setId(array_object.optString("id"));
            commonModels.setDescription("description");
            common_stop_list.add(commonModels);
        }

    } catch (JSONException e) {

        e.printStackTrace();
    }

    adapter.notifyDataSetChanged();
© www.soinside.com 2019 - 2024. All rights reserved.