How to read .txt file into ListView?
11,810
First move your file in assets folder in your project and use this code for reading file from assets folder :
try{
InputStream inputreader = getAssets().open("myaddress.txt");
BufferedReader buffreader = new BufferedReader(new InputStreamReader(inputreader));
// your code here
Related videos on Youtube
Author by
Chris Braswell
I am very new at Objective C so please forgive me if i asked bad questions. Im so used to java that sometimes i cant wrap my head around this different syntax
Updated on September 14, 2022Comments
-
Chris Braswell over 1 year
I am trying to read lines stored in a separate text file into my android application and have each line show up in the list view.
The problem is that I cannot get anything to appear despite the fact my code says their are not errors in it. As you read this code, I will clarify that my ListView is named "addresslist" and the file i'm reading from is named "myaddress.txt". I appreciate any help.
ListView listView= (ListView) findViewById(R.id.addresslist); try{ InputStream instream = openFileInput("myaddress.txt"); InputStreamReader inputreader = new InputStreamReader(instream); BufferedReader buffreader = new BufferedReader(inputreader); ArrayList<String> lines = new ArrayList<String>(); boolean hasNextLine =true; while (hasNextLine){ String line = buffreader.readLine(); lines.add(line); hasNextLine = line != null; } ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.id.addresslist,lines); listView.setAdapter(adapter); instream.close(); } catch(java.io.FileNotFoundException e){ }catch(java.io.IOException e){ } }
With the following errors in the log:
12-08 01:21:30.394: E/Trace(1500): [ 12-08 01:21:30.864 1500: 1500 V/Home to School 4828 N. Crescent Norridge IL 60706, 5500 N St Louis Avenue Chicago IL 60625; Home to School 4828 N. Crescent Norridge IL 60706, 5500 N St Louis Avenue Chicago IL 60625; 12-08 01:21:30.864: D/AndroidRuntime(1500): Shutting down VM 12-08 01:21:30.924: W/dalvikvm(1500): threadid=1: thread exiting with uncaught exception (group=0xb3e92288) 12-08 01:21:30.944: E/AndroidRuntime(1500): FATAL EXCEPTION: main 12-08 01:21:30.944: E/AndroidRuntime(1500): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tripapp/com.tripapp.Frontpage}: java.lang.NullPointerException: println needs a message 12-08 01:21:30.944: E/AndroidRuntime(1500): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 12-08 01:21:30.944: E/AndroidRuntime(1500): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 12-08 01:21:30.944: E/AndroidRuntime(1500): at android.app.ActivityThread.access$600(ActivityThread.java:130) 12-08 01:21:30.944: E/AndroidRuntime(1500): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 12-08 01:21:30.944: E/AndroidRuntime(1500): at android.os.Handler.dispatchMessage(Handler.java:99) 12-08 01:21:30.944: E/AndroidRuntime(1500): at android.os.Looper.loop(Looper.java:137) 12-08 01:21:30.944: E/AndroidRuntime(1500): at android.app.ActivityThread.main(ActivityThread.java:4745) 12-08 01:21:30.944: E/AndroidRuntime(1500): at java.lang.reflect.Method.invokeNative(Native Method) 12-08 01:21:30.944: E/AndroidRuntime(1500): at java.lang.reflect.Method.invoke(Method.java:511) 12-08 01:21:30.944: E/AndroidRuntime(1500): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 12-08 01:21:30.944: E/AndroidRuntime(1500): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 12-08 01:21:30.944: E/AndroidRuntime(1500): at dalvik.system.NativeStart.main(Native Method) 12-08 01:21:30.944: E/AndroidRuntime(1500): Caused by: java.lang.NullPointerException: println needs a message 12-08 01:21:30.944: E/AndroidRuntime(1500): at android.util.Log.println_native(Native Method) 12-08 01:21:30.944: E/AndroidRuntime(1500): at android.util.Log.v(Log.java:117) 12-08 01:21:30.944: E/AndroidRuntime(1500): at com.tripapp.Frontpage.onCreate(Frontpage.java:61) 12-08 01:21:30.944: E/AndroidRuntime(1500): at android.app.Activity.performCreate(Activity.java:5008) 12-08 01:21:30.944: E/AndroidRuntime(1500): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 12-08 01:21:30.944: E/AndroidRuntime(1500): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 12-08 01:21:30.944: E/AndroidRuntime(1500): ... 11 more
-
ρяσѕρєя K over 11 yearssee this answer and move your file in assets folder in your appliction
-
ρяσѕρєя K over 11 yearshave u got me what i'm saying ?
-
Chris Braswell over 11 yearsIt has no erroes but it says "Unfortanatly trippapp has stopped working.
-
ρяσѕρєя K over 11 years@ChrisBraswell : we are near to solve it post your logcat when application is crashing
-
Chris Braswell over 11 yearsThe good news is i see my text in the log, the bad news is there is ALOT of red errors. Things such as Fatal exception main, Unable to start activity, printlin needs a message. The most noticable the one Frontpage.java.Oncreate(java 61) as that method is actually in my code but it doesnt haven any errors
-
ρяσѕρєя K over 11 years@ChrisBraswell : this is not bad news dear if you get more error then it means you are going to learn something new . without getting lots of error anyone never become a good coder. ok just past your error log with your question then i will try to find what is issue
-
Chris Braswell over 11 yearsok ive added the erros to my original question. I must thank you for how much you have helped me so far. It is truly appreciated.
-
ρяσѕρєя K over 11 yearsif you are using Log.v in your code then just remove it or comment out and then run your appliction
-
ρяσѕρєя K over 11 yearsand for checking ArrayList size use
System.out.println("ArrayList Size :: " +lines.size());
after while loop -
Chris Braswell over 11 yearscommented the line out, its back to "unfortanatly, the app has stopped working"
-
Chris Braswell over 11 yearsI have no problem with that. Ill send you the whole thing in a zip or .rar file. Theres more to the code (like other classes), but i just want the get the part we discussed from you.