Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getFragmentManager from ArrayAdapter

Tags:

android

I have a listView fulled from my custom ArrayAdapter. In each view there is a button. I want to change the current fragment when the button is clicked. This is my code:

public class CheckInSArrayAdapter extends ArrayAdapter<JSONObject> {


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getViewOptimize(position, convertView, parent);
    }

    public View getViewOptimize(int position, View convertView, ViewGroup parent) {

    ......

        viewHolder.commentBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            ??? <<<<<

        });

        return convertView;
    }

    private class ViewHolder {
        ...       
        public Button commentBtn;
    }

}

So now, i can't invoke getFragmentManager from the OnClickListener. How can i do?

like image 503
Antonello Gatto Avatar asked Sep 08 '13 23:09

Antonello Gatto


1 Answers

OK, maybe i'm a bit late but, maybe it will help some people to do this.

In the class where you call the adapter, you need to extends or implements "FragmentActivity". Then your Activity will contain a fragmentManager.

When you call your Adapter call it with the class that extends FragmentActivity

MyAdapterName = new MyAdapterName(MyClassName.this, Objects);

Then in your adapter in getView() - do this.

final Context context = parent.getContext();
FragmentManager fm = ((Activity) context).getFragmentManager();

Be carefull to use the good android FragementManager - android.app.FragmentManager not android.support.v4.app.FragmentManager;

like image 165
1020rpz Avatar answered Oct 23 '22 09:10

1020rpz