0

i have a java class that communicates with a Mysql Server trought POST method. When i click on item in the NavigationDrawer this is what happens:

 chat_fragment fragment= new chat_fragment();
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container,fragment);
        fragmentTransaction.commit();
        setTitle("Chat");

This is my fragment.class(chat_fragment.class)

public class chat_fragment extends Fragment {
String username;
private ListView listView;

public chat_fragment() {
    // Required empty public constructor
}



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    // don't know whats your rootlayout. Assuming you use a LinearLayout
    LinearLayout layout = inflater.inflate(R.layout.chat_fragment, container, false);
    listView = layout.findViewById(R.id.lv_chat); 

   return view;
}

@Overwrite
public void onActivityCreated()
{

SharedPreferences userDetails = getActivity().getBaseContext().getSharedPreferences("Login", Context.MODE_PRIVATE);
    username=userDetails.getString("Unm","");


    GetMsg getMsg=new GetMsg(listView);
    getMsg.execute("method",username);
}

}

I need to execute the Java Class(GetMsg) by doing the follow:

 GetMsg getMsg=new GetMsg(ctx);
 getMsg.execute("msg",username);

Usually what i do is put "this" where it says "ctx", but in the fragment that is not working.. I get the context from the Activity doing this in my Java Class:

ListView list;

GetMsg (ListView list){

    this.list=list;


}

This is my GetMsg.class onPosExecute(is AsyncTask):

                   String[] msg = new String[] {
                           response,

               };
               final List<String> msg_list = new ArrayList<String>(Arrays.asList(msg));

               ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
                       (list.getContext(), android.R.layout.simple_list_item_1, msg_list);



               list.setAdapter(arrayAdapter);
               list.getAdapter().notifyDatasetChanged()

My problem? I need to add items to a ListView in the fragment, but i can't get the Fragment Context, so i not being capable of doing that, can you please help me?

Thanks!!

Vall0n
  • 1,601
  • 2
  • 14
  • 19
Ko0kiE
  • 33
  • 6
  • add getActivity() in place of ctx .. – Hobbit Nov 27 '16 at 12:50
  • getActivity() is getting the context of the MainActivty, not of the fragment, any suggestion, what should i post to better expose my problem? – Ko0kiE Nov 27 '16 at 13:02
  • Just pass getActivity().getApplicationContext() to GetMsg. – Hobbit Nov 27 '16 at 13:05
  • i am doing that in the onCreateView of the Fragment, am i doing it right? – Ko0kiE Nov 27 '16 at 13:07
  • Please post more code. – Vall0n Nov 27 '16 at 13:15
  • Please check post , thank you! – Ko0kiE Nov 27 '16 at 13:19
  • Overwrite the onActivityCreated() method in the fragment and put the getMsg stuff in it. To make sure that getActivity() call is working. More infos here: https://developer.android.com/guide/components/fragments.html – Vall0n Nov 27 '16 at 13:36
  • In my javaclass when i get the ListView to a variable i need to find it, how do i do that, because this is a fragment not a AppCompactActivity – Ko0kiE Nov 27 '16 at 13:48
  • @Ko0kiE Can you post more code. What do you want to achieve? Fill the list view with data form the getMsg stuff? – Vall0n Nov 27 '16 at 13:56
  • please check my post again, thank you! – Ko0kiE Nov 27 '16 at 14:05
  • you should pass only the listview to the getMsg constructor. And make the listview a member of the fragment and set it in the onCreateView() method of the Fragment. – Vall0n Nov 27 '16 at 14:17
  • could you please tell me how to do it? i am new at android studio development.. Thanks for your help – Ko0kiE Nov 27 '16 at 14:19
  • As this question is marked as duplicate, i can't answer anymore. So, I've edited the code of your question with the modifications to hopefully resolve your problem. – Vall0n Nov 27 '16 at 14:35
  • Hi, Vall0n39, i try what you gave me , but is still getting the MainActivity context, but thank you so much for your help, i will try to loof for another way. Have a nice day! – Ko0kiE Nov 27 '16 at 15:16
  • No problem! Even better is to just pass the array adapter of the listivew to the AsyncTask GetMsg. And instanciate and set the arrayadapter in the onCreateView Method of the fragment. – Vall0n Nov 27 '16 at 15:17

0 Answers0