【问题标题】:How call a class from a fragment如何从片段中调用一个类
【发布时间】:2019-05-23 21:43:29
【问题描述】:

我正在开发一个将 json 文件保存到设备缓存中的小项目。 在我的 Home_fragmen 上,我从网络上的文件加载 json 并保存文件。

JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("photos");

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject hit = jsonArray.getJSONObject(i);
                            String id = hit.getString("id");

                            mlist.add(new items(id));
                        }
                        mAdapter = new main_adapter(getActivity(), mlist);
                        mRecyclerView.setAdapter(mAdapter); 

                        // Cashing json file
                        jsonFile= response.toString();
                        cacheJson(jsonFile);
                        }

 private void cacheJson(String data) {
        try {
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(getActivity().openFileOutput("jsontxt.txt", Context.MODE_PRIVATE));
            outputStreamWriter.write(data);
            outputStreamWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这是我用来从缓存中读取数据的代码

private String readJson() {
        String jsonArray = "";

        try {
            InputStream inputStream = getActivity().openFileInput("jsontxt.txt");
            if ( inputStream != null) {
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                String receivingString = "";
                StringBuilder stringBuilder = new StringBuilder();

                while ((receivingString = bufferedReader.readLine()) != null) {
                    stringBuilder.append(receivingString);
                }
                inputStream.close();
                jsonArray = stringBuilder.toString();
            }
        } catch (FileNotFoundException e) {
            Log.e(TAG, "File not found: " + e.toString());
        } catch (IOException e) {
            Log.e(TAG, "Can not read file: " + e.toString());
        }

        return jsonArray;
    }

所有这些都在我的主要片段上工作,因为那里拥有一切......现在我要做的是从不同片段中读取缓存一个新片段。

我试过做这些,但没有用。

Home_fragment jsonFile = new Home_fragment ();
        String s = jsonFile.cachejson();
        Log.e(TAG, "JSON FILE: " + s);

我的问题是如何从辅助片段读取访问我的主片段上的 readJson()。

【问题讨论】:

    标签: java android caching


    【解决方案1】:

    第 1 步: 在片段 1 中,将您的 json 结果放入 Bundle

    Bundle bundle = new Bundle();
    bundle.putString("key","abc"); // Put anything what you want
    
    Fragment_2 fragment2 = new Fragment_2();
    fragment2.setArguments(bundle);
    
    getFragmentManager()
          .beginTransaction()
          .replace(R.id.content, fragment2)
          .commit();
    

    第 2 步: 在片段 2 中

    Bundle bundle = this.getArguments();
    
    if(bundle != null){
         // handle your code here.
    }
    

    第 3 步 创建一个方法 readjson 作为读取包参数的全局访问权限

    public static String readJson(String jsonStr) {
    
            try {
                // put your code
                }
            } catch (FileNotFoundException e) {
                Log.e(TAG, "File not found: " + e.toString());
            } catch (IOException e) {
                Log.e(TAG, "Can not read file: " + e.toString());
            }
    
            return jsonArray;
        }
    

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      使方法独立于片段的上下文(getActivity)并将该上下文作为参数传递。然后将该方法放在两个片段中的公共可访问对象上,然后您可以从任何地方访问它。

      【讨论】:

        猜你喜欢
        • 2013-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-30
        • 2017-06-09
        • 2021-06-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多