Padelis Theodosiou Padelis Theodosiou - 4 months ago 20
Android Question

How to fill a listView in Android with data that they are saved on internal file (my_text_file.txt)

I'm new in Android dev and I want to ask this.

I've create an Android program that saves passwords in a txt file.
I want to retrieve these passwords and display them into a listView.

Below is the code that I wrote to save passwords to my file:

try {
FileOutputStream fileout=openFileOutput("mytextfile.txt", MODE_APPEND);
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(textView3.getText().toString()+"\n");
outputWriter.close();

//display password saved message
Toast.makeText(getBaseContext(), "Password saved successfully!",
Toast.LENGTH_SHORT).show();

} catch (Exception e) {
e.printStackTrace();
}


Now this is my code that reads the file:

FileInputStream fileIn=openFileInput("mytextfile.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);

char[] inputBuffer= new char[READ_BLOCK_SIZE];
String s="";
int charRead;

while ((charRead=InputRead.read(inputBuffer))>0) {

String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
textView4.setText(s);

} catch (Exception e) {
e.printStackTrace();
}


I want to know if there is a way to display these passwords into a listView.

Namely I'm looking for the code. I appreciate any help.


:NOTE: If that question is already answered, I'm sorry.


SOLUTION

Thanks to @mehd-azizi I finally found the solution to my problem.

change the read part to this :

public String[] readFileAsString(String fileName) {
Context context = this;
StringBuilder stringBuilder = new StringBuilder();
String line;
BufferedReader in = null;
ArrayList<String> list=new ArrayList<String>();
try {
in = new BufferedReader(new FileReader(new File(context.getFilesDir(), fileName)));
while ((line = in.readLine()) != null) {
list.add(line);
}

} catch (FileNotFoundException e) {

} catch (IOException e) {

}
String[] stockArr = new String[list.size()];

return list.toArray(stockArr);
}


and use that like this to read the file and fill the ListView:

ListView listView=(ListView)findViewById(R.id.listView);
ArrayAdapter<String> adapter=new ArrayAdapter<String(getApplicationContext(),android.R.layout.simple_list_item_1,readFileAsString("mytextfile.txt"));
listView.setAdapter(adapter);

Answer

change the read part to this :

  public  String[] readFileAsString(String fileName) {
    Context context = this;
    StringBuilder stringBuilder = new StringBuilder();
    String line;
    BufferedReader in = null;
    ArrayList<String> list=new ArrayList<String>();
    try {
        in = new BufferedReader(new FileReader(new File(context.getFilesDir(), fileName)));
        while ((line = in.readLine()) != null) {
            list.add(line);
        }

    } catch (FileNotFoundException e) {

    } catch (IOException e) {

    }
    String[] stockArr = new String[list.size()];

    return list.toArray(stockArr);
}

and use that like this to read the file and fill the ListView:

 ListView listView=(ListView)findViewById(R.id.listView);

 ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_list_item_1,
                    readFileAsString("mytextfile.txt"));

 listView.setAdapter(adapter);
Comments