【问题标题】:Data is not showing in recyclerview while adding to the file添加到文件时数据未显示在 recyclerview 中
【发布时间】:2018-05-25 10:28:12
【问题描述】:

我正在尝试将 json 数据 存储到文件中。这是创建目录和文件的代码。

      public void filecreation() throws IOException {
       File mediaStorageDir = new File(getActivity().getFilesDir() + "/" + 
      "Zerribelum");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("App", "failed to create directory");
            //  return null;
        } else {
            Log.d("Apppppp", "create directory");
            File textfolder = new File(mediaStorageDir, "Text");
            if (!textfolder.exists()) {
                textfolder.mkdirs();
                File newtext = new File(textfolder, "dummy.txt");
                stream = new FileOutputStream(newtext);
                bw = new BufferedWriter(new OutputStreamWriter(stream));
           
            }
            File imagefolder = new File(mediaStorageDir, "Image");
            if (!imagefolder.exists()) {
                imagefolder.mkdirs();
            }
            File videofolder = new File(mediaStorageDir, "Video");
            if (!videofolder.exists()) {
                videofolder.mkdirs();
            }
        }

    }
}

我想把json数据放到文件里。

      try {
        JSONArray mJsonTopicArray = mJsonObject.getJSONArray(AppConstants.APIKeys.TOPICS_LIST);
        for (int i = 0; i < mJsonTopicArray.length(); i++) {
            Topics mTopics = new Topics();
            mTopics.setId(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.ID));
            mTopics.setTitle(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.TOPICS));
            mTopics.setImage(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.TOPICS_IMAGE));
            mTopics.setDescription(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.DESCRIPTION));

            mTopicsArrayList.add(mTopics);
            bw.write(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.DESCRIPTION));
            bw.newLine();
        }
          bw.close();
         mCategoryListRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mCategoryListRecyclerView.setHasFixedSize(true);
    mCategoryListRecyclerView.setAdapter(new ParallaxRecyclerAdapter(context, HomeFragment.this, mTopicsArrayList));
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

问题是如果我没有添加bw.writebw.close() 行,数据将显示在recyclerview 中。但是当我添加这一行时,数据正在添加到文件中,但没有显示在 recycler 视图中。我希望两者都完成。会有什么问题?

注意:公共 FileOutputStream 流;

公共 BufferedWriter bw;已初始化。

【问题讨论】:

    标签: android json file android-recyclerview


    【解决方案1】:

    检查你的堆栈跟踪,你的代码一定是崩溃了,而你因为 try catch 而没有注意到它

    适配器初始化后,附加一个调试器并检查数组列表中的项目。如果项目存在,它将起作用

    【讨论】:

      【解决方案2】:

      解决了,问题是因为空指针异常。所以使用try catch处理。

                 try {
                      bw.write(mJsonTopicArray.getJSONObject(i).getString(AppConstants.APIKeys.TOPICS));
                      bw.newLine();
                  } catch (IOException e) {
                      e.printStackTrace();
                  } catch (JSONException e) {
                      e.printStackTrace();
                  } catch (NullPointerException e) {
                      e.printStackTrace();
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-21
        • 1970-01-01
        • 2022-01-26
        • 1970-01-01
        • 1970-01-01
        • 2021-01-15
        • 2017-08-06
        • 2018-08-08
        相关资源
        最近更新 更多