Search code examples
javaandroidgridviewfragmentandroid-arrayadapter

Starting a Fragment Inside ArrayAdapter


I have a gridview that gets populated with ArrayAdapter. Within the GridView I have a Button and ImageView. I made a Listener for the Button and Image however I would like to start a Fragment once I tap on the ImageView. And I got the Error in using getSupportFragmentManager();.

holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            DetailsFragment fragment = new DetailsFragment();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
        }
    });

UPDATE

This is my code for my ArrayAdapter Class

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;


public class MyAdapter extends ArrayAdapter<RowItem> {
    Context context;
    int imgId;



public MyAdapter(Context context, int resourceId, List<RowItem> items){
    super(context, resourceId, items);
    this.context = context;
}


private class ViewHolder{
    ImageView imageView;
    Button btn;
    TextView textView;
}

public View getView(int position, View convertView, ViewGroup parent){
    ViewHolder holder = null;
    RowItem rowItem = getItem(position);

    LayoutInflater inflater = (LayoutInflater)        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    if (convertView == null){
        convertView = inflater.inflate(R.layout.grid_item, null);
        holder = new ViewHolder();
        holder.imageView = (ImageView) convertView.findViewById(R.id.img);
        holder.btn = (Button) convertView.findViewById(R.id.btnPocket);
        holder.textView = (TextView) convertView.findViewById(R.id.tv);
        convertView.setTag(holder);
    }
    else
        holder = (ViewHolder) convertView.getTag();

    Picasso.with(context)
            //.load("http://www.balay-indang.com/megamobile/pics/"+   String.valueOf(position+1) +".png")
            .load(rowItem.getImgUrl())
            .resize(200,150)
            .centerCrop()
            .into(holder.imageView);
    holder.textView.setText(rowItem.getText());
    holder.btn.setBackgroundResource(rowItem.getPocketId());

    final String pos = String.valueOf(position);

    holder.btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getContext(), "Pocket: "+ pos,   Toast.LENGTH_LONG).show();
        }
    });
    holder.imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction =   fragmentManager.beginTransaction();
            DetailsFragment fragment = new DetailsFragment();
            fragmentTransaction.replace(R.id.fragment_container, fragment);
            fragmentTransaction.commit();
        }
    });

    return convertView;

}

}


Solution

  • you can only access getSupportedFragmentManager from an FragmentActivity class. If you have to access getSupportedFragmentManager in your adapter then you have pass activity's instance through constructor.

    getSupportFragmentManager() is only defined for the class FragmentActivity.

    EDIT:

    If your MainActivity extends ActionBarActivity then try this.

    holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = ((ActionBarActivity) context).getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                DetailsFragment fragment = new DetailsFragment();
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.commit();
            }
        });
    

    Hope this helps!