Guest User

Untitled

a guest
Jun 20th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. public class EActivity extends Fragment {
  2. ArrayList<Countries> countries;
  3. JSONArray countryArray;
  4. Spinner e_country;
  5. @Nullable
  6. @Override
  7. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  8. MainActivity.fragmentName="eR";
  9. final View v= inflater.inflate(R.layout.activity_e,null);
  10. e_country=(Spinner)v.findViewById(R.id.txtECountry);
  11. progressDialog=new ProgressDialog(v.getContext());
  12.  
  13. getCountry(v);
  14. return v;
  15.  
  16. }
  17. void getCountry(final View view){
  18.  
  19. progressDialog.setMessage("Loading, Please wait...");
  20. progressDialog.setIndeterminate(true);
  21. progressDialog.show();
  22. JsonObjectRequest objectRequest=new JsonObjectRequest(Request.Method.GET, ApiLinks.countryPoint, null,
  23. new Response.Listener<JSONObject>() {
  24. @Override
  25. public void onResponse(JSONObject response) {
  26. try {
  27. String status = response.getString("status");
  28. if(status.equals("success")){
  29. countryArray=response.getJSONArray("country");
  30. countries=new ArrayList<Countries>();
  31. for(int i=0;i<countryArray.length();i++){
  32. JSONObject object=countryArray.getJSONObject(i);
  33. Countries country=new Countries();
  34. country.setId(object.getInt("id"));
  35. country.setName(object.getString("country"));
  36. countries.add(country);
  37. }
  38. ArrayAdapter<Countries> adapter=new ArrayAdapter<Countries>(view.getContext(),android.R.layout.simple_spinner_dropdown_item
  39. ,countries);
  40. e_country.setAdapter(adapter);
  41. e_country.setPrompt("-- Select Country --");
  42. }
  43.  
  44. }catch(Exception e){
  45. Toast.makeText(view.getContext(),"Country not loaded.Please go back and try again.",Toast.LENGTH_SHORT).show();
  46. }
  47. progressDialog.cancel();
  48. }
  49.  
  50. },
  51. new Response.ErrorListener() {
  52. @Override
  53. public void onErrorResponse(VolleyError error) {
  54. Toast.makeText(view.getContext(),"Country not loaded.Please go back and try again.",Toast.LENGTH_SHORT).show();
  55. progressDialog.cancel();
  56. }
  57. });
  58. AppSingleton.getInstance(view.getContext().getApplicationContext()).addToRequestQueue(objectRequest,ApiLinks.REQUEST_TAG);
  59. }
  60.  
  61. <Spinner
  62. android:layout_width="match_parent"
  63. android:layout_height="wrap_content"
  64. android:id="@+id/txtECountry"
  65. android:layout_below="@+id/textView18"
  66. android:layout_alignParentLeft="true"
  67. android:layout_alignParentStart="true"
  68. android:paddingTop="10dp"
  69. android:paddingBottom="10dp"
  70. android:layout_marginTop="8dp"
  71. android:layout_marginBottom="8dp"
  72. android:background="@drawable/textbox_style"/>
Add Comment
Please, Sign In to add comment