我有一个与Mysql Server交配POST方法的java类。 当我点击NavigationDrawer中的项目时,会发生以下情况:
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");
这是我的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);
}
}
我需要执行以下操作来执行Java类(GetMsg):
GetMsg getMsg=new GetMsg(ctx);
getMsg.execute("msg",username);
通常我所做的是将“this”放在“ctx”所在的位置,但是在不起作用的片段中。 我从我的Java类中的Activity中获取了上下文:
ListView list;
GetMsg (ListView list){
this.list=list;
}
这是我的GetMsg.class onPosExecute(是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()
我的问题? 我需要在片段中向ListView添加项目,但我无法获得片段上下文,所以我无法做到这一点,你能帮助我吗?
谢谢!