java - 如何从 fragment 中调用类

标签 java android caching

我正在开发一个将 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;
    }

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

我试过这些但没有用。

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

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

最佳答案

第 1 步: 在 fragment 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 步: 在 fragment 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;
    }

希望对你有帮助

关于java - 如何从 fragment 中调用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53918314/

相关文章:

java - 在抽象方法中比instanceof/cast更好的选择?

android - 如何检查我的应用程序是否是默认启动器

android - 如何禁用 fragment 中的抽屉并返回正确的 fragment

java - 如何使用 FileUtils 对将文件保存到磁盘进行单元测试?

java - Spring 事务原子性到底意味着什么?

caching - 将 MemoryCacheHandle 与 RedisCacheBackplane 一起使用,但不使用 RedisCacheHandle

redirect - 带有缓存响应 header 的 IIS 重定向

mysql - MySQL JDBC Driver中的cachePrepStmts和useServerPrepStmts有什么区别

java - 从网络读取数据

java - Android AsyncTask onPostExecute 访问 GUI 元素给出空指针