0

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user1152300
  • 23
  • 2
  • 4
  • 1
    possible duplicate of [How to bind ListView Objects in Fragments?](http://stackoverflow.com/questions/10821048/how-to-bind-listview-objects-in-fragments) – Infinite Recursion Jul 15 '15 at 12:58

1 Answers1

1

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, ...
Sam
  • 86,580
  • 20
  • 181
  • 179
  • have tried to replace fragment_my_profile with MainActivity (my fragment activity is called that) but got a different error now"No enclosing instance of the type MainActivity is accessible in scope", what should i do from there? – user1152300 Dec 15 '12 at 06:12
  • Thank you very much !!! just changed the fragment_my_profile.this to getActivity() but left it in the oncreate() and it worked ! thanks again – user1152300 Dec 15 '12 at 06:25