【问题标题】:passing arraylist item of tabfragment recyclerview to other activity recyclerview将 tabfragment recyclerview 的 arraylist 项传递给其他活动 recyclerview
【发布时间】:2017-12-26 11:06:09
【问题描述】:

我已将 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 示例
  • @Ziat JSONArray 中有对象吗?
  • 在这里发布你的 json
  • ec2-54-147-238-136.compute-1.amazonaws.com/hmc/api/… 这是 json 的链接.. 请先生看看@Alexey #Shivam #nihal
  • @ShivamOberoi ec2-54-147-238-136.compute-1.amazonaws.com/hmc/api/... 这是 json 的链接 .. 请先生看看

标签: json arraylist android-recyclerview


【解决方案1】:

您的响应已经在 J​​SONArray 中,无需存储在另一个 JSON Array 中。

看看下面的代码。

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

【讨论】:

    【解决方案2】:

    new Response.Listener<String>
    

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

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

    希望对您有所帮助!

    【讨论】:

    • 你有没有浏览过给url上的json数据..没有jsonArray的名称
    • 那么就这样做:JSONArray responseArray=new JSONArray(response);
    • 那么我如何使用密钥获取数据并将它们存储在相应的字符串变量中?就像我想在“标题”和“描述”数组中获取数据并将它们存储在各自的字符串变量@BhoomikaPatel
    • @xamMTS 你可以这样做。 for(int index= 0;index link了解更多信息
    • 遇到同样的错误!! @BhoomikaPatel 我按照您提供的链接进行了此操作 JSONArray jArr = null;尝试 { jArr = new JSONArray(response); for (int i = 0; i
    【解决方案3】:

    根据您的 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();
    

    【讨论】:

    • 如果我想把获取的数据放到一个arrayList而不是相应的字符串中。
    • @ZiafatRaheemKhokhar 创建一个模型类。并使用此模型类进行存储。像 ArrayList list = new ArrayList();模型类模型 = 新模型类(); model.setId(jObj.optString("id")); ---------------------------------------- ---------- ------ list.add(model);
    • @ZiafatRaheemKhokhar 模型类表示 Setter 和 Getter 类。
    • 我做的和你上面提到的一样,但它对我不起作用.. @nihal_softl
    • @ZiafatRaheemKhokhar 等我更新我的答案
    猜你喜欢
    • 2020-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多