Saturday, 24 August 2013

Can not read file after application resumes in android

Can not read file after application resumes in android

I am facing weird problem in my application. User can read file when
he/she clicks on button and contents of file will be populate through List
view. So, when user first time clicks on button, file content will be
shown in List view. But when user presses back button and again starts the
same activity, and clicks on button to read file content, no data are
appearing in List view. Here is my code...
FileInputStream fstream;
btnAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// read file New
try
{
fstream = openFileInput("New");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
Scanner scanFile = new Scanner(new DataInputStream(fstream));
words = new ArrayList<String>();
words.clear();
while (scanFile.hasNextLine())
{
String nameToadd = scanFile.next();
if(scanFile.hasNext())
{
nameToadd = String.format("%s %s", nameToadd,
scanFile.nextLine());
}
words.add(nameToadd);
}
adapterFriendsToAdd = new ArrayAdapter<String>
getBaseContext(),R.layout.checklist, words);
lvDialog.setAdapter(adapterFriendsToAdd);
lvDialog.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
adapterFriendsToAdd.notifyDataSetChanged();
}
}
});
what needs to be done?

No comments:

Post a Comment