Reputation: 13
I'm trying to read a simple text file that is a list of words and numbers and display it in a scrollable list in an Android application.
I'm not having any issue using arbitrary string arrays that I specify in the code such as the string arrays "myStringArray" or "alphabts." However, when I try to read a text file and use it in one of the lists, my app crashes.
It could be something as simple as the format of the text file is wrong, or that I should have the file in a different location and be referencing it from there.
Thanks for the help!
Java Code:
package com.coderzheaven.pack;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class AlphabetListDemo extends Activity {
//String of alphabets //
String[] myStringArray = {"Calories","Protein","Carbs"};
String[] alphabts = {"1","2","3"};
ListView L1, L2;
myAdapter myadp;
myAdapter2 myadp2;
String prod_arr[] = {};
InputStream is = this.getResources().openRawResource(
R.drawable.filetoread);
DataInputStream myDIS = new DataInputStream(is);
ArrayList<String> list = new ArrayList<String>();
final String[] bob = list.toArray(new String[list.size()]);
//final String[] bob = alphabts; Here if I comment out the four
//lines above and and use the alphabts string it works fine
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
L1 = (ListView)findViewById(R.id.list1);
L2 = (ListView)findViewById(R.id.list2);
myadp = new myAdapter(this,alphabts);
L2.setAdapter(myadp);
L2.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setProducts(arg2);
}
});
}
public void setProducts(int number){
prod_arr = new String[25];
// adding some dummy data //
for(int i = 0; i < 25 ; i++){
prod_arr[i] = bob[number];
}
//setting the adapter in listview //
myadp2 = new myAdapter2(AlphabetListDemo.this,prod_arr);
L1.setAdapter(myadp2);
}
class myAdapter extends ArrayAdapter<String>
{
TextView label;
ImageView image;
View row;
public myAdapter(Context context,String[] arr)
{
super(context, android.R.layout.simple_list_item_1, arr);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
try{
LayoutInflater inflater=getLayoutInflater();
row = inflater.inflate(R.layout.lv_rows, parent, false);
label = (TextView)row.findViewById(R.id.item_title);
label.setText(myStringArray[position]);
label.setTextColor(Color.YELLOW);
}catch(Exception e){
}
return row;
}
}
// adapter for second list.....
class myAdapter2 extends ArrayAdapter<String>
{
TextView label;
ImageView image;
View row;
public myAdapter2(Context context,String[] arr)
{
super(context, android.R.layout.simple_list_item_1, arr);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
try{
LayoutInflater inflater=getLayoutInflater();
row = inflater.inflate(R.layout.lv_rows, parent, false);
label = (TextView)row.findViewById(R.id.item_title);
label.setText(prod_arr[position]);
label.setTextColor(Color.WHITE);
}catch(Exception e){
}
return row;
}
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<!-- this list contains products -->
<ListView
android:id="@+id/list1"
android:layout_width="192dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:cacheColorHint="#00000000"
android:dividerHeight="1px"
android:drawSelectorOnTop="false"
android:fadingEdge="vertical"
android:padding="0dip"
android:scrollbars="none"
android:smoothScrollbar="true"
android:soundEffectsEnabled="true" />
<ListView
android:id="@+id/list2"
android:layout_weight="4"
android:cacheColorHint="#00000000"
android:scrollbars="none"
android:fadingEdge="vertical"
android:soundEffectsEnabled="true"
android:dividerHeight="1px"
android:padding="0dip"
android:smoothScrollbar="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:layout_marginTop="5dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginBottom="5dip" />
</LinearLayout>
lv_rows.xml in the same location as main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="7dp"
>
<TextView
android:id="@+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:text="Main Item"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14dp"
android:textStyle="normal" />
</RelativeLayout>
Upvotes: 1
Views: 2069
Reputation: 1702
Why you want to read a text file from your drawable folder? The correct way is to put this file into the assets folder on your project. If you are using Android Studio right click on your project -> New -> Folder -> Assets folder.
Then move your file into it and simply use the AssetManager to open it like that:
AssetManager assetsManager = getApplicationContext().getAssets(); // or getBaseContext()
try{
InputStream inputStream = assetsManager.open("yourfile.txt");
// read inputStream ...
// and do not forget to close it here when your read is completed!!!
}catch (IOException ex){
ex.printStackTrace();
}
Upvotes: 2
Reputation: 586
Got your problem you are forgetting the + sign.
InputStream is = this.getResources().openRawResource(
+R.drawable.filetoread);
check here Android openRawResource() not working for a drawable
Upvotes: 0