Simple File Reader!
December 4th, 2012
Background
This lab is the beginning of a 2 parter. In this first part, we will simply read from a file and display the text items in a list.
Files at the end
- MainActivity
- activtiy_main.xml
- todos.txt
Layout
- Linear Layout. orientation = vertical
- ListView. id = listview. layout_width = wrap_content. layout_height = fill.
MainActivity
- Start by having private references to the following:
- ListView listView
- ArrayList<String> items
- ArrayAdapter<String> adapter
- get the reference to list view
- instantiate items
- call the parseText() method . We will write this later
- instantiate adapter. Remember! this, android.R.layout.simple_list_item_1, items.
- set adapter to notify data set change
parseText method
Scanner scanner = null
try{
scanner = new Scanner(getAssets().open("todos.txt"));
}catch(Exception e){}
while(scanner.hasNextLine())
items.add(scanner.nextLine();