我有资产文件夹中所有文件的列表视图。 问题是,当我打开一个特定的文件,然后返回并从Listview打开另一个文本文件时,它将打开上一个文件。
如何清除旧文件的缓冲区,以便我可以打开另一个文本文件。
public class Listapp extends Activity {
ListView mylistview;
ArrayList<String> array_filename;
ArrayList<String> array_textlocation;
ArrayAdapter<String> listAdapter;
Button btnStartQuiz;
TextView txtTitle, txtAuthor;
private QuizDriver quizDriver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listmain);
mylistview = (ListView)findViewById(R.id.listView1);
//here write folder name which one added assets folder
array_filename = new ArrayList<String>();
array_textlocation = new ArrayList<String>();
listAdapter = new ArrayAdapter<String>(Listapp.this,android.R.layout.simple_list_item_1, array_filename);
mylistview.setAdapter(listAdapter);
quizDriver = (QuizDriver) getApplication();
//For ListItem Click
mylistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String s = array_filename.get(position);
String t = array_textlocation.get(position);
Toast.makeText(Listapp.this, "Selected File is " + s+t, 3000).show();
openFile("Myfiles/"+s, view.getContext());
Intent i = new Intent(view.getContext(), GeneralApp.class);
startActivityForResult(i,0);
}
});
String[] myfilesfolderlist = listAssetFiles("Myfiles");
for (int i = 0; i < myfilesfolderlist.length; i++) {
array_filename.add(myfilesfolderlist[i]);
array_textlocation.add("Myfiles/"+myfilesfolderlist[i]);
}
}
//Method for get files from Assets Folder and sub folder
private String [] listAssetFiles(String path)
{
String [] list;
try
{
list = Listapp.this.getAssets().list(path);
if (list.length > 0)
{
return list;
}
}catch (IOException e)
{
}
return null;
}
protected void openFile(String filename, Context context) {
InputStream is = null;
try {
is = context.getAssets().open(filename);
if ( is != null) {
quizDriver.parseQuestionData(new BufferedReader(new InputStreamReader(is, "UTF-8")));
quizDriver.selectQuestions();
}
} catch (IOException e) {
Log.d("main activity", "IOException thrown");
}
}
}
答案 0 :(得分:0)
我建议覆盖活动编号1中的onResume()方法,并在其中包含刷新数组适配器的代码,这可以通过使用
来完成[yourListViewAdapater].notifyDataSetChanged();
如果您在刷新列表时遇到问题,请阅读此内容:Android List view refresh
如果这不起作用,请尝试使用myListView.invalidateViews()
;