1

Hi i need to create a list view to display all the contacts which are stored in a text file. To add a contact the user must enter the details himself and so these will be saved in a text file. Now I need to create a list view to display these contacts in a list and also for every a contact a button would be shown. As for now what my code does is simply show the contact in a Toast.

public void ViewContacts(View v)
{
    //reading contacts from textfile
    try{
        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)
        {
            //char to string conversion
            String readstring=String.copyValueOf(inputBuffer,0,charRead);
            s+=readstring;
        }
        InputRead.close();
        Toast.makeText(getBaseContext(),s,Toast.LENGTH_SHORT).show();
    } catch(Exception e)
    {
        e.printStackTrace();
    }
}
tessie1959
  • 11
  • 4

1 Answers1

1

You can store all contact details in a single text file in a json string type. Then parse that json string and create a list of data whenever you need them back. Populate that list in a listview.

You can follow this link to convert string to json

Parse your json data - example

Community
  • 1
  • 1
Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56
  • This thing is that I need it to accept user input. Will I be able to do that on the example you gave me pls? Sorry but I am a bit new to this :) – tessie1959 May 05 '16 at 11:19