Android 添加到文件时,数据未显示在recyclerview中
我正在尝试将json数据存储到文件中。这是创建目录和文件的代码Android 添加到文件时,数据未显示在recyclerview中,android,json,file,android-recyclerview,Android,Json,File,Android Recyclerview,我正在尝试将json数据存储到文件中。这是创建目录和文件的代码 public void filecreation() throws IOException { File mediaStorageDir = new File(getActivity().getFilesDir() + "/" + "Zerribelum"); if (!mediaStorageDir.exists()) { if
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();
}
试试看{
JSONArray mJsonTopicArray=mJsonObject.getJSONArray(AppConstants.APIKeys.TOPICS_列表);
对于(int i=0;i
问题是,如果我没有添加bw.write
和bw.close()
行,数据将显示在recyclerview中。但当我添加这些行时,数据会添加到文件中,但不会显示在recylcer视图中。我想两者都完成。有什么问题吗
注意:公共文件输出流
公共缓冲写入程序bw;草签
检查stacktrace,你的代码一定是崩溃了,你没有注意到它,因为try-catch
初始化适配器后,附加调试器并检查arraylist中的项目。如果项目存在,它将工作已解决,问题是由于Nullpointer异常。因此使用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();
}