I'm trying to read a text file into my android app and then print the contents of the file to screen across 3 columns. The text file will be in the format of "column 1 data - column 2 data - column 3 data" on each line. I'm reading the information into an ArrayList> and then trying to print it to screen. I have two xml files that i'm working with.
The one containing my list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
and the one containing my TextViews
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView android:id="@+id/flitem"
android:layout_width="50dip"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/flgrams"
android:layout_width="70dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView android:id="@+id/flpointsLayout"
android:layout_width="60dip"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</RelativeLayout>
I have two lists specified in the first xml because when I had the android:name as
android:id="@android:id/list"
I couldn't like select the list using R.id.list but if I specified it using
android:id="@+id/list"
I was getting a null pointer exception.
My code in my Class extending ListFragment is as follows
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class FoodListFragment extends ListFragment {
ArrayList<HashMap<String, String>> myList;
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
createHashMap(activity);
} catch (Exception e){
System.out.println("Caught Exception: " + e);
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.foodlistmenu, container, false);
ListView list = (ListView) rootView.findViewById(R.id.list);
SimpleAdapter pointsListAdapter = new SimpleAdapter(getActivity(), myList, R.layout.foodlistmenu, new String[] {"Item", "Qty/Weight", "Points"}, new int[] {R.id.flitem, R.id.flgrams, R.id.flpointsLayout});
try {
list.setAdapter(pointsListAdapter);
} catch (Exception e) {
System.out.println("Caught Exception Setting adapter to list. Exception is: " + e);
}
return rootView;
}
public void createHashMap(Activity c) throws IOException{
myList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
InputStream is = c.getAssets().open("pointslist.bc");
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String line = "";
while ((line = in.readLine()) != null) {
String parts[] = line.split("-");
map.put("Item", parts[0]);
map.put("Weight", parts[1]);
map.put("Points", parts[2]);
myList.add(map);
map = new HashMap<String, String>();
}
in.close();
}
}
When I run the application (Via Emulator) I can open the page and it will just display a blank white page. But from putting in System.out.println's here and there I can see that the ArrayList> is being populated and that the List and SimpleAdapter are being successfully created and the Adapter is being set to the list without throwing any errors.
I'm relatively new to android dev and I don't get to code as much as I'd like to so I could be making some foolish mistakes (Hopefully not!). Any help on this would be extremely appreciated as I have been stuck on this for days now!
Thanks!