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!!