如何通过读取.txt并替换以前存在的.txt来填充TextView



我是android开发的新手,正在做一个项目,用.txt文件填充文本视图。对多个txt文件使用相同的文本视图。但问题是,它在该文本视图中显示了第一、第二、第三个所有文本。它没有清除上一个。

MenuModel menuModel = new MenuModel("Introduction", true, false, "Intro.txt");
headerList.add(menuModel);
menuModel = new MenuModel("Tense", true, true, "");
headerList.add(menuModel);
List<MenuModel> childModelsList = new ArrayList<>();
MenuModel childModel = new MenuModel("About Tense ", false, false, "TENSE.txt");
childModelsList.add(childModel);
if (menuModel.hasChildren) {
childList.put(menuModel, childModelsList);
}

我在这里使用可扩展列表视图private void populateExpandableList(){

expandableListAdapter = new ExpandableListAdapter(this, headerList, childList);
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
if (headerList.get(groupPosition).isGroup) {
if (!headerList.get(groupPosition).hasChildren) {

setFile将选择要打开的文本文档

String setFile = headerList.get(groupPosition).url;
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open(setFile)));
String mLine;
while ((mLine = reader.readLine()) != null) {
text.append(mLine);
text.append('n');
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
TextView txtView = findViewById(R.id.tvContent);
txtView.clearComposingText();
txtView.setMovementMethod(new ScrollingMovementMethod());
txtView.setText(text);

}
onBackPressed();
}
}
return false;
}
});
expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if (childList.get(headerList.get(groupPosition)) != null) {
MenuModel model = childList.get(headerList.get(groupPosition)).get(childPosition);
if (model.url.length() > 0) {

setFile将选择要打开的文本文档

String setFile = model.url;
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open(setFile)));

String mLine;
while ((mLine = reader.readLine()) != null) {
text.append(mLine);
text.append('n');
}
} catch (IOException e) {
Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
//log the exception
}
}
TextView txtView = findViewById(R.id.tvContent);
txtView.clearComposingText();
txtView.setMovementMethod(new ScrollingMovementMethod());
txtView.setText(text);
}
onBackPressed();
}
}
return false;
}
});
}

用于存储文件行的变量text显然是一个StringBuilder对象,不是吗
从文件中读取每一行之后,您可以使用它来附加每一行,并且当读取完成时,您可以将TextView的文本设置为StringBuilder的文本
但在开始读取行并将其附加到StringBuilder之前,您必须使用清除其以前的文本

text = new StringBuilder();

text.setLength(0);

因此,将上述行中的1行放在while循环之前。

最新更新