0

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!

microbenny
  • 33
  • 7
  • this probelem occurs with custom ListFragment. Refer this [link][1] [1]: http://stackoverflow.com/questions/11770773/listfragment-layout-from-xml – Rajesh Batth Nov 21 '13 at 17:09
  • @RajeshBatth, Thanks for the quick reponse. I've added the additional class but when I try to set the id to the ListFragment.INTERNAL_... `view.findViewById(R.id.empty_id).setId(ListFragment.INTERNAL_FLITEM);` I'm getting INTERNAL_FLITEM cannot be resolved or is not a field. Is there something I need to set to configure these `INTERNAL_` fields? – microbenny Nov 22 '13 at 11:27
  • @RajeshBatth, Sorry, I had a typo in my code that wasn't allowing the `INTERNAL_` paramaters be prompted. I have created the class `ListFragmentLayout` with the following code and for each of my TextView's that are updated via the `SimpleAdapter`, I've added `view.findViewById(R.id.flitem).setId(ListFragment.INTERNAL_EMPTY_ID);` I then call this method in my onCreateView of my class extending ListFragment. Along with this, I've moved my TextView's into the xml file containing my list. With these changes, My App is still just displaying a white screen? The list isin't showing? – microbenny Nov 22 '13 at 11:45
  • @RajeshBatth I have got it working! My problem was that I hadn't put an android:id on my layout in my xml. I added the ID. Then I put the code as above in to setId and the list is now appearing. Thank you so much for your help – microbenny Nov 22 '13 at 13:52

0 Answers0