我是android开发的新手,正在做一个项目,在其中用.txt文件填充textview。对多个txt文件使用相同的textview,但是问题是该textview中显示所有文本的第一,第二,第三。它不会清除上一个。
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);
}
我在这里使用可扩展列表视图 私人无效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;
}
});
}
答案 0 :(得分:0)
用于存储文件行的变量text
显然是StringBuilder
对象,不是吗?
您可以使用它在从文件中读取每一行之后追加每行,读取完成后,将TextView
的文本设置为StringBuilder
的文本。
但是在开始阅读这些行并将其附加到StringBuilder
之前,您必须先使用
text = new StringBuilder();
或
text.setLength(0);
因此,将上述行中的1行放在while
循环之前。