Q:

Starting Fragment from ArrayAdapter

I have 4 Fragments that I'm using for my application. One of my fragments(ChatList) has a ListView that uses an adapter which extends ArrayAdapter. Now in my ArrayAdapter class, when a certain row is clicked I want to open up my CurrentChat Fragment. How would I go about this? As far I have looked, it seems as though fragments can only be accessed from activities and other fragments.
ChatList.java
package com.example.jj.fragments;

import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;

/**
 * Created by jj on 11/23/2015.
 */
public class ChatList extends Fragment {
    public static final String ARG_PAGE = "ARG_PAGE";
    ListView chatListLV;
    ChatListAdapter adapter;

    public static ChatList newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        ChatList fragment = new ChatList();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    // Inflate the fragment layout we defined above for this fragment
    // Set the associated text for the title
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.chatlist, container, false);
        chatListLV = (ListView) view.findViewById(R.id.chatLV);
        adapter = new ChatListAdapter(getContext(),R.layout.single_chatlist_row);
        chatListLV.setAdapter(adapter);
        chatListLV.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
        adapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                chatListLV.setSelection(adapter.getCount() - 1);
            }
        });
        fillChatList();
        return view;

    }

    public void fillChatList(){
        DBHelper db = new DBHelper(getActivity());
        db.getChatList(adapter);
        db.close();

    }
}

ChatListAdapter.java
package com.example.jj.fragments;

/**
 * Created by jj on 11/28/2015.
 */

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import io.socket.client.IO;
import io.socket.client.Socket;

public class ChatListAdapter extends ArrayAdapter<ChatListDataProvider>{

    public Socket mSocket;
    {
        try {
            mSocket = IO.socket("https://great-sarodh.c9.io/");
        }
        catch (URISyntaxException e){}
    }

    private static final String TAG = "ChatListAdapter" ;
    public List<ChatListDataProvider> chat_list = new ArrayList<ChatListDataProvider>();

    private TextView dateTV;
    private TextView messageTV;
    private TextView timeTV;
    private TextView sideTV;
    private ImageView PictureIV;
    private String gcmID;
    private String androidID;
    private String roomhash;
    private int side;
    CurrentChat CCFrag;

    int type;

    Context CTX;

    public ChatListAdapter(Context context, int resource) {

        super(context, resource);
        CTX = context;
    }

    @Override
    public void add(ChatListDataProvider object){
        chat_list.add(object);
        super.add(object);

    }

    @Override
    public int getCount() {
        return chat_list.size();
    }

    @Override
    public ChatListDataProvider getItem(int position) {

        return chat_list.get(position);
    }

    @Override public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView == null){

            LayoutInflater inflator = (LayoutInflater) CTX.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflator.inflate(R.layout.single_chatlist_row,parent,false);
        }

        sideTV = (TextView) convertView.findViewById(R.id.sideTV);
        messageTV = (TextView) convertView.findViewById(R.id.lastmsgTV);
        timeTV = (TextView) convertView.findViewById(R.id.timeTV);
        ChatListDataProvider provider = chat_list.get(position);
        gcmID = provider.gcmID;
        messageTV.setText(provider.lastMsg);
        timeTV.setText(provider.time);
        side = provider.side;
        if(side == 0){
            sideTV.setText("Who?:");
        }
        else if(side == 1){
            sideTV.setText("You:");
        }
        roomhash = provider.roomID;

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stuck
                Log.d(TAG, "JOINING ROOMID" + chat_list.get(position).roomID);
                mSocket.emit("CreateRoom", chat_list.get(position).roomID, chat_list.get(position).gcmID);
               //THE CODE TO OPEN CURRENTCHAT FRAGMENTS SHOULD GO HERE
            }
        });

        notifyDataSetChanged();
        return convertView;

    }

}

ChatListDataProvider.java
package com.example.jj.fragments;

import android.support.v4.app.FragmentActivity;

/**
 * Created by jj on 11/28/2015.
 */
public class ChatListDataProvider extends FragmentActivity{
    public String roomID;
    public String gcmID;
    public String lastMsg;
    public String date;
    public String time;
    public int side;

    private ChatAdapter chat;

    public ChatListDataProvider (String gcmID) {
        super();
    }

    public ChatListDataProvider (String roomID, String gcmID, String lastMsg, int side, String time, String date) {
        this.roomID = roomID;
        this.gcmID = gcmID;
        this.lastMsg = lastMsg;
        this.side = side;
        this.date = date;

    }

}

CurrentChat.java
package com.example.jj.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class CurrentChat extends Fragment {

    public static final String ARG_PAGE = "ARG_PAGE";

    public static CurrentChat newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        CurrentChat fragment = new CurrentChat();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.currentchat, container, false);
        return view;
    }

}

A:

In order to do that you can make a function inside your adapter passing your current activity context and container/frame layout like this.
public void changeFragmentFromAdapter(Activity act , int layoutid)
{
    YourFragment fragmentToPopulate =  new YourFragment();
    FragmentManager frgManager = act.getFragmentManager();
    FragmentTransaction fgTransation = frgManager.beginTransaction();
    FgTransation.replace(layoutid, fragmentToPopulate).commit();
}

Paste this function in your ChatListAdapter.
now in your ChatList Class add itemclicklistener to your list
    chatListLV.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
                  //Here Add call change function
           adapter.changeFragmentFromAdapter(getActivity() , R.layout.chatList);
   }
}

i assume R.layout.chatList is your container for 3 fragments in MainActivity.
Now call this function from your activity of fragment.

