0

我有一個資產文件夾中所有文件的列表視圖。 問題是,當我打開一個特定的文件,然後返回並打開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

我會建議重寫在活動編號1的onResume()方法,並在那裏包括代碼刷新陣列適配器,這是通過使用

[yourListViewAdapater].notifyDataSetChanged(); 

做閱讀本,如果您有麻煩刷新列表:Android List view refresh

如果這不工作嘗試使用myListView.invalidateViews();

+0

仍然不能正常工作 – Vishal

+0

它顯示以前打開的文件。 – Vishal

+0

嘗試使用'myListView.invalidateViews()' – prat