0

I created an android actionbar tab application, with 4 tabs, one of the tab is a fragment class which is used to contain a list view,to display a list of static information:

However, I have an error at this line of code: SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,

 R.layout.listoflockers, from, to);

These are my 2 xml files used to create a list view:

 listoflockers.xml
<?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" >

   <TextView
      android:id="@+id/textview"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
   />

  <ListView
     android:id="@+id/listview"
     android:layout_width="fill_parent"
      android:layout_height="wrap_content"
   />
</LinearLayout>


lockerinforow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal"
 >

<ImageView
    android:id="@+id/flag"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/email"
    android:paddingTop="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="10dp"
 />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
>
  <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
    />

    <TextView
        android:id="@+id/cur"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
    />
    <TextView
        android:id="@+id/size"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10dp"
    />
       </LinearLayout>
    </LinearLayout>


  public class myLockerFragment.java{
   .....

      public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_mylocker, container,    false);



        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>

             ();        

        for(int i=0;i<10;i++){
              HashMap<String, String> hm = new HashMap<String,String>();

            hm.put("txt", "Locker No : " + i);

            hm.put("cur","Location : " + currency[i]);
            hm.put("size","Size : " + size[i]);

            hm.put("flag", Integer.toString(flags[i]) );            
            aList.add(hm);        
        }

        // Keys used in Hashmap
        String[] from = { "flag","txt","cur","size" };

        // Ids of views in listview_layout
        int[] to = { R.id.flag,R.id.txt,R.id.cur,R.id.size};        

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, 

            R.layout.listoflockers, from, to);

        // Getting a reference to listview of main.xml layout file
        ListView listView = ( ListView ) rootView.findViewById(R.id.listview);
        // Setting the adapter to the listView
        listView.setAdapter(adapter);
            return rootView;
      }

Please help I am confused at this line:

   SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, 
   R.layout.listoflockers, from, to);

giving me the error: The method getBaseContext() is undefined for the type myLockerFragment

4 Answers 4

3

you can use getActivity() inside a Fragment to get the context that holds the Fragment itself

Sign up to request clarification or add additional context in comments.

Comments

0

Do this-

SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList, 
   R.layout.listoflockers, from, to);

Comments

0
SimpleAdapter adapter = new SimpleAdapter(getActivity().getApplicationContext(), aList, R.layout.listoflockeres, from, to);

You need to use the getActivity() method inside Fragments

Comments

0

Besides using getBaseContext() you need to use the getActivity() in Fragments to get the context of the Fragment.

Try out as below:

  SimpleAdapter adapter = new SimpleAdapter(getActivity(), aList,R.layout.listoflockers, from, to);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.