我正在尝试更改文本视图中的文本视图cityName上的文本,但是当我单击该项目时,它不会更改文本。 我已经尝试了几次,但它仍然不起作用我在这里做错了什么? 我尝试在arraylist.get [posistion]中使用just position但它仍然不会更改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);
}
答案 0 :(得分: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);
}