ถ้าต้องการให้มันเข้าไป Fragment Layout ในหน้านั้นก่อนแล้วค่อยไป call api อันนี้ผมต้องทำยังไงบ้างคับ
JAVA Fragment Code (Java)
...} else if (id == R.id.nav_help) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.ftMain, new HelpFragment() );
ft.commit();
}
JAVA Activity Code (Java)
public class HelpFragment extends Fragment {
private String base_url = "https://domain.com/api";
private String token_api = "xxxxx";
public HelpFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_help, container, false);
final TextView btnBackToMenu = (TextView)view.findViewById(R.id.btnBackToMenu);
btnBackToMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.ftMain, new AccountFragment() );
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
/* Send API */
String url2 = base_url+"/help.php";
HttpClient client2 = new DefaultHttpClient();
HttpPost httpPost2 = new HttpPost(url2);
List<NameValuePair> params2 = new ArrayList<NameValuePair>();
params2.add(new BasicNameValuePair("action", "getHelp"));
params2.add(new BasicNameValuePair("token_api", token_api));
String resultServer = getHttpPost(url2,params2);
try {
JSONObject dbarr = new JSONObject(resultServer);
final TextView titleHelp = (TextView)view.findViewById(R.id.titleHelp);
titleHelp.setText( dbarr.getString("title"));
String textAccount = dbarr.getString("description");
final TextView textHelp = (TextView)view.findViewById(R.id.textHelp);
textHelp.setText(HtmlCompat.fromHtml(textAccount, 0));
} catch (JSONException e) {
e.printStackTrace();
}
final ProgressBar loader = (ProgressBar)view.findViewById(R.id.loader);
loader.setVisibility(View.INVISIBLE);
return view;
}
public String getHttpPost(String url, List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
}