tessie1959
tessie1959

Reputation: 11

List View to display contacts from txt file-Android Studio

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();
    }
}

Upvotes: 1

Views: 520

Answers (1)

Alex Chengalan
Alex Chengalan

Reputation: 8281

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

Upvotes: 1

Related Questions