Search code examples
javaandroidfileloaddata

Android How can i easily read and add to the List data from txt file?


My code: https://pastebin.com/KudWYD8W

I don't know how write method loadData. I wanna have list with only names of Games and when I press on the element should show the details. My file looks like:

Witcher

1996

free

66h

Please help. I tried in many ways but there was always something wrong.


Solution

  • Try this.

    1.Save the string to be read as different list item separated with a comma(,).

    2.Call this method to get the list by passing the file path.

    private List<String> populateAutoComplete(String path)
        {
            try
            {
                String content = readFile(path);
                Log.d(TAG,
                      "populateAutoComplete: " + content);
                List<String> listToPopulate = Arrays.asList(content.split("\\s*,\\s*"));
                return listToPopulate;
            }
            catch (IOException e)
            {
                e.printStackTrace();
                return Collections.emptyList();
            }
        }
    

    Ask in case of any doubts. Cheers :)