0

I am trying to change the text in text view cityName on itemclicked from the spinner but when i click on the item it does not does not change the text. I have tried it several times but it still not working what am i doing wrong here? I have tried using just position in arraylist.get[posistion] but it still does not change the text in cityName TextView

public class TimeMain extends AppCompatActivity {

TextView cityName;
ArrayList arrayList=new ArrayList<GetZones>();
Spinner mySpinner;

@TargetApi(Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_time_main);
    cityName=findViewById(R.id.cityName);

    mySpinner=(Spinner)findViewById(R.id.spinner);

    getCountryList();



    Time_Converter_Adapter adapter=new Time_Converter_Adapter(arrayList,this);

    mySpinner.setAdapter(adapter);

    mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
         @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            GetZones getZones;
            getZones= (GetZones) arrayList.get(parent.getSelectedItemPosition());
            cityName.setText(getZones.getCountryName());

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            cityName.setText("Selected cities Name");
        }

}




public void getCountryList()
{

    RequestQueue requestQueue;
    requestQueue = Volley.newRequestQueue(getApplicationContext());
    StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://api.timezonedb.com/v2/list-time-zone?key=Z35J0I51CRWE&format=json", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            try {
                JSONObject res = new JSONObject(response);
                JSONArray jsonArray=res.getJSONArray("zones");
                for(int i=0;i<jsonArray.length();i++)
                {
                    JSONObject jsonObject=jsonArray.getJSONObject(i);
                    String countryName=jsonObject.getString("countryName");
                    int timestamp=jsonObject.getInt("timestamp");
                    String countryCode=jsonObject.getString("countryCode");
                    GetZones getZones=new GetZones(countryName,countryCode,timestamp);
                    arrayList.add(getZones);


                }


            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "There Was A Fatal Error!!!!", Toast.LENGTH_SHORT).show();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Context context = getApplicationContext();

            if (error instanceof TimeoutError || error instanceof NoConnectionError) {
                Toast.makeText(context,
                        "Connection Timed Out",
                        Toast.LENGTH_LONG).show();
            } else if (error instanceof AuthFailureError) {
                //TODO
            } else if (error instanceof ServerError) {
                //TODO
            } else if (error instanceof NetworkError) {
                //TODO
            } else if (error instanceof ParseError) {
                //TODO
            }
        }
    });
    requestQueue.add(stringRequest);
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0
public class TimeMain extends AppCompatActivity {

TextView cityName;
ArrayList arrayList=new ArrayList<GetZones>();
Spinner mySpinner;

@TargetApi(Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_time_main);
    cityName=findViewById(R.id.cityName);

    mySpinner=(Spinner)findViewById(R.id.spinner);

    getCountryList();



    Time_Converter_Adapter adapter=new Time_Converter_Adapter(arrayList,this);

    mySpinner.setAdapter(adapter);

    mySpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
         @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            GetZones getZones;
            getZones= (GetZones) arrayList.get(i);
            cityName.setText(getZones.getCountryName());

        }


}




public void getCountryList()
{

    RequestQueue requestQueue;
    requestQueue = Volley.newRequestQueue(getApplicationContext());
    StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://api.timezonedb.com/v2/list-time-zone?key=Z35J0I51CRWE&format=json", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            try {
                JSONObject res = new JSONObject(response);
                JSONArray jsonArray=res.getJSONArray("zones");
                for(int i=0;i<jsonArray.length();i++)
                {
                    JSONObject jsonObject=jsonArray.getJSONObject(i);
                    String countryName=jsonObject.getString("countryName");
                    int timestamp=jsonObject.getInt("timestamp");
                    String countryCode=jsonObject.getString("countryCode");
                    GetZones getZones=new GetZones(countryName,countryCode,timestamp);
                    arrayList.add(getZones);


                }


            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "There Was A Fatal Error!!!!", Toast.LENGTH_SHORT).show();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Context context = getApplicationContext();

            if (error instanceof TimeoutError || error instanceof NoConnectionError) {
                Toast.makeText(context,
                        "Connection Timed Out",
                        Toast.LENGTH_LONG).show();
            } else if (error instanceof AuthFailureError) {
                //TODO
            } else if (error instanceof ServerError) {
                //TODO
            } else if (error instanceof NetworkError) {
                //TODO
            } else if (error instanceof ParseError) {
                //TODO
            }
        }
    });
    requestQueue.add(stringRequest);
}
  • it doesn't do anything i tried putting in a Toast in public void onItemClick() but nothing happens – Ashiqul Hossain Dec 22 '17 at 15:13
  • Check this answer https://stackoverflow.com/questions/18911607/android-spinner-setonclicklistener –  Dec 22 '17 at 15:37