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;
}
}
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
then within the class extending FragmentActivity do this:
then within the Adapter do this
Hope this helps