0

Making a android application where user would an ID, then click submit. Using that id it will search the text file and if found it would display the content in separate textviews.

txt file would look like this, with each content separated by a comma (",")

541,BoB,05-18-2014
483,Phil,01-19-1992
971,Komarov,10-30-1858

The text file is located in the res/raw/players.txt

Have an onClick handler for the button. I'm a little unsure of what type of reader I should use and how to search its contents. So far I have,

try {
        Resources res = getResources();
        InputStream ins = res.openRawResource(R.raw.players);
        byte[] b = new byte[ins.available()];
        ins.read(); 

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.print("error, cant read");
    }
user2077348
  • 7
  • 1
  • 6

1 Answers1

0

The above text format is generally called a CSV format, as they are comma-separated values.

You can do this in several ways, depending on your preference. I did the exact searching in text files but with Java on a computer and not in Android. The basic difference would be how you define the location of file on device. You can go like this StackOverflow post. Just remember that you'll need to use the split method on the String with the delimiter of ",". Example - String[] out = line.split(",");

The aforementioned link uses a double dimension array which you can search through via a loop indexed through the lines for your specific "ID". When found the rest of the data from that row can be accessed in a nested loop and then put to respective TextViews.

Addendum You can avoid useless processing and memory usage in splitting every row into it's components. Instead, just store each line in a String array and compare the first few characters. On successful match, split that row for each data-item.

You might want to look into Searching and other search optimizations, if you're dealing with very large size text files.

Good Luck!

Community
  • 1
  • 1
MD Naseem Ashraf
  • 1,030
  • 2
  • 9
  • 22