androidandroid-layoutandroid-listviewandroid-fragmentsandroid-listfragment

Android Multi-Line Listview in fragment


I have an app that uses a tab layout using fragments, in one of the fragments I would like to have a two/multi-line List View, I have been following this tutorial which shows it for a ListActivity. I have copied the code into my fragment and cannot seem to get it to work. all of my code for the layout of the fragment and the two lines is the same as code in the link above, with the exception of the Java class for the fragment I want to show the list in.

The code for the fragment is as follows:

package com.example.shopsellswap;

import java.util.ArrayList;
import java.util.HashMap;
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.SimpleAdapter;

public class Fragment_My_Profile extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View myProfileView = inflater.inflate(R.layout.fragment_my_profile, container, false);


        return myProfileView;
    }

    //ArrayList holds the data (as HashMaps) to load into the ListView
        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
        //SimpleAdapter does the work to load the data in to the ListView
        private SimpleAdapter sa;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            //HashMap links each line of data to the correct TextView
            HashMap<String,String> item;
            for(int i=0;i<StatesAndCapitals.length;i++){
              item = new HashMap<String,String>();
              item.put( "line1", StatesAndCapitals[i][0]);
              item.put( "line2", StatesAndCapitals[i][3]);
              list.add( item );
            }

            sa = new SimpleAdapter(Fragment_My_Profile.this, list,
                    R.layout.my_two_lines,
                    new String[] { "line1","line2" },
                    new int[] {R.id.line_a, R.id.line_b});

            setListAdapter(sa);
        }

        private String[][] StatesAndCapitals =
            {{"Alabama","Montgomery"},
            {"Alaska","Juneau"},
            {"Arizona","Phoenix"},
            {"Arkansas","Little Rock"},
            {"California","Sacramento"}};

The part that is giving me errors is

        sa = new SimpleAdapter(Fragment_My_Profile.this, list,
                R.layout.my_two_lines,
                new String[] { "line1","line2" },
                new int[] {R.id.line_a, R.id.line_b});

        setListAdapter(sa);

the specific error is:

The constructor SimpleAdapter(Fragment_My_Profile, ArrayList<HashMap<String,String>>, int, String[], int[]) is undefined

what's weird is when I change ListFragment to ListActivity the error is no longer there

Why it isn't working and how I can fix it?


Solution

  • ListFragment is not a subclass of Context, while ListActivity is. You must pass some type of Context to this constructor. For example, let's assume that your Activity (or FragmentActivity) class is named MainActivity:

    sa = new SimpleAdapter(MainActivity.this, list, ...
    

    Depending on when you create this Fragment that might not work, so you can move all of your code in onCreate() to the onActivityCreated() method and use:

    sa = new SimpleAdapter(getActivity(), list, ...