0

I have a listview of all the files in assets folder. The problem is that when I open a particular file and then go back and open another text file from Listview then it would open the previous file.

How to clear buffer of old file so I can open and another text file.

   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");


    }
  }
 }
Vishal
  • 1
  • 2

1 Answers1

0

I would recommend overriding the onResume() method in activity number 1, and in there include code to refresh your array adapter, this is done by using

[yourListViewAdapater].notifyDataSetChanged();

Read this if you are having trouble refreshing the list: Android List view refresh

If this is not working try using myListView.invalidateViews();

Community
  • 1
  • 1
prat
  • 795
  • 5
  • 14