lppier
lppier

Reputation: 1987

Android - Cannot resolve symbol for getSupportFragmentManager

Here's the code for my ItemAdapter which I use as an adapter for list in this way.

list.setAdapter(new ItemAdapter(thisContext,imageUrl));

I'm trying to implement an onclick listener for the button that will add an item to a ListFragment. When I compile, it says "cannot resolve symbol getSupportFragmentManager". See bold below. But I'm pretty sure I imported android.support.v4.app.FragmentActivity (see below). I used the same getSupportFragmentManager in my main activity and it works. I don't understand why it isn't here. And if it isn't, how should I obtain the HeadlinesFragment so that I can add to the list in that fragment?

Appreciate any help I can get here.

Pier.

package com.suite.android.menu;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
**import android.support.v4.app.FragmentActivity;**


public class ItemAdapter extends BaseAdapter {

private Context itemPageContext;
private String imagesUrl[];
private static final String TAG = "ItemAdapter" ;

public ItemAdapter(Context c, String[] imageUrl)
{
   super();
   itemPageContext = c;
   imagesUrl = imageUrl.clone();  // copy over


}

private LayoutInflater getLayoutInflater()
{
    return (LayoutInflater) itemPageContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public View getView(final int position, View convertView, ViewGroup parent)
{
    // all this is for 1 item in the list

    View theView = convertView;
    final ViewHolder holder;
    if (convertView == null) {
        theView = getLayoutInflater().inflate(R.layout.item_list_image, null);
        holder = new ViewHolder();
        holder.text = (TextView) theView.findViewById(R.id.foodTitle);
        holder.image = (ImageView) theView.findViewById(R.id.foodImage);
        holder.button = (Button) theView.findViewById(R.id.addOrderButton);
        theView.setTag(holder);
    } else
        holder = (ViewHolder) theView.getTag();

    holder.text.setText("Item " + position);
    holder.image.setImageResource(R.drawable.ic_launcher);

    holder.button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            **HeadlinesFragment headFragment = (HeadlinesFragment) getSupportFragmentManager().findFragmentById(R.id.headlines_fragment);**
            headFragment.AddItem("One");
            Log.d(TAG, "Added Item!");
        }
    });

    return theView;
}

private class ViewHolder {
    public TextView text;
    public ImageView image;
    public Button button;
}

@Override
public int getCount() {
    return imagesUrl.length;
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

}

Upvotes: 1

Views: 15085

Answers (3)

Frank Yin
Frank Yin

Reputation: 1990

Surely you've found the answer by now, but this may help others. I was in a situation perhaps similar to yours with no precise answers for how to work around having an Adapter class separate from the FragmentActivity class, while needing to access the getSupportFragmentManager() within the Adapter.

Passing the context didn't work because it doesn't allowing casting of the FragmentActivity (for reasons I still don't understand).

But I gave up and just saved access to the manager itself and call it directly:

public ViewPagerAdapter(Context context, FragmentManager fm, Fragment f) {
    super(fm);  
    _fm = fm; // declared in the class "private FragmentManager _fm;"
    _context=context;       
}

Upvotes: 1

Proxy32
Proxy32

Reputation: 811

It's been a while since I played around with fragments. but I believe you can only access getSupportedFragmentManager from an FragmentActivity class.

try this

public ItemAdapter(Context c,FragmentActivity activity, String[] imageUrl)
{
   super();
   itemPageContext = c;
   this.activity = activity;
   imagesUrl = imageUrl.clone();  // copy over
}

then within the class extending FragmentActivity do this:

list.setAdapter(new ItemAdapter(thisContext,thisActivity, imageUrl));

then within the Adapter do this

activity.getSupportedFragmentManager...

Hope this helps

Upvotes: 6

devunwired
devunwired

Reputation: 63303

You may have imported the class, but you aren't in a FragmentActivity, you are in a BaseAdapter so that method doesn't exist on the class you are in. If you wanted to call that method on your Activity, you would need to pass the Activity in (as a FragmentActivity rather than a Context) or make this adapter a private inner class so it has access to the enclosing Activity methods. That method needs to be called on a FragmentActivity object.

Upvotes: 4

Related Questions