0

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;

}

}

Christian Callelero
  • 846
  • 1
  • 8
  • 17

3 Answers3

2

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!

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
  • Wait so you are saying that I'am supposed to extend my Fragment to FragmentActivity? And Replace my ArrayAdapter's default constructor with your first snippet? – Christian Callelero Aug 19 '15 at 09:02
  • See edited answer. your main activity extending ActionBarActivity or AppCompatActivity or something else? – Rajesh Jadav Aug 19 '15 at 09:10
  • I have provided my Full code for my adapter. Can you explain to me further what I need to change? I already removed the super() method however it still returns an error. "There is no default constructor available". – Christian Callelero Aug 19 '15 at 09:12
  • FragmentManager fragmentManager = ((ActionBarActivity) context).getSupportFragmentManager(); is getting an error: incompatible types. required android.app.FragmentManager. Found android.support.v4.app.FragmentManager. Sorry I forgot to mention I'am extending AppCompatActivity in my MainActivity – Christian Callelero Aug 19 '15 at 09:20
  • try getFragmentManager() instead of getSupportFragmentManager() – Rajesh Jadav Aug 19 '15 at 09:24
  • Now I have one last error. fragmentTransaction.replace(R.id.fragment_container, fragment); it says replace (int, android.app.fragment) to (int, detailsfragment); – Christian Callelero Aug 19 '15 at 09:28
  • Sorry I figured it out already. I changed my DetailsFragment import to android.app.fragment. Is there a work around I can do if I want to use the other import. Which is android.support.v4.app.Fragment; ?? – Christian Callelero Aug 19 '15 at 09:33
  • yes you have to import android.support.v4.app.Fragment – Rajesh Jadav Aug 20 '15 at 04:17
0

Is your current activity a "Fragment" activity?

Wonka
  • 1,548
  • 1
  • 13
  • 20
0

Your activity needs to extend and Activity that inherits from FragmentActivity in order to have getSupportFragmentManager() and other support methods available.

At the time of my answer your activity should extend AppCompatActivity. Also ensure that relevant imports use the support classes such as

android.support.v4.app.FragmentManager 
CarefreeCrayon
  • 249
  • 2
  • 7