Spinner android 和 okhttp3,当我获取数据时,仅当我单击 Spinner 时它才不会显示在 Spinner 中
Spinner android and okhttp3, When I have fetched data it's no displays in spinner only when I am clicking spinner
我正在使用 okhttp3 获取数据,然后该数据显示在微调器中。但这并没有发生。微调器是空的,但是当我放下微调器时,我可以看到 okhttp3 获取了我的日期。我不太清楚为什么微调器一开始是空的。
private List<String> categories;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra("MESSAGE_LOGIN");
TextView textView = findViewById(R.id.textView);
textView.setText(message);
categories = new ArrayList<String>();
Log.d("LOGIN", "TEST!!!");
final Spinner spinner = findViewById(R.id.spinner);
RequestParams params = new RequestParams();
params.add("type","category");
RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"), params.toString());
categories = postRequest(MainActivity.postURIStage, body);
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public List<String> postRequest(String postUrl, RequestBody postBody) {
final List<String> categories1 = new ArrayList<String>();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(postUrl)
.post(postBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// Cancel the post on failure.
call.cancel();
Log.d("FAIL", e.getMessage());
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseTextLogin = findViewById(R.id.responseTextLogin);
responseTextLogin.setText("Server is unreachable. Please try soon.");
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseTextLogin = findViewById(R.id.textView);
try {
String categoryResponseString = response.body().string();
Log.d("TEST:", categoryResponseString);
JSONObject jsonResponse = new JSONObject(categoryResponseString);
Log.d("Message", "Message form the server : " + jsonResponse.getJSONArray("message"));
try {
// Parse categories from api.php
JSONArray categoriesArray = jsonResponse.getJSONArray("message");
// Lopping through all categories
for (int i = 0; i < categoriesArray.length(); i++) {
JSONObject c = categoriesArray.getJSONObject(i);
String name = c.getString("name");
categories1.add(name);
}
} catch (final JSONException e) {
Log.e("TAG", "Json parsing error: " + e.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
responseTextLogin.setText("No categories as active state in DB!");
}
}
});
}
});
return categories1;
}
}
尽管我将 select 微调器中的数据对此 setOnItemSelectedListener 函数没有反应
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
我附上了可以看到空微调器的照片,当我单击它时会显示数据,但它一开始没有显示。
Empty spinner
When I clicked
按照您编写代码的方式,微调器会在返回结果之前填充空类别 ArrayList。
可以将Spinner population移动到postRequest方法的onResponse中
public List<String> postRequest(String postUrl, RequestBody postBody) {
final List<String> categories1 = new ArrayList<String>();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(postUrl)
.post(postBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// Cancel the post on failure.
call.cancel();
Log.d("FAIL", e.getMessage());
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseTextLogin = findViewById(R.id.responseTextLogin);
responseTextLogin.setText("Server is unreachable. Please try soon.");
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseTextLogin = findViewById(R.id.textView);
try {
String categoryResponseString = response.body().string();
Log.d("TEST:", categoryResponseString);
JSONObject jsonResponse = new JSONObject(categoryResponseString);
Log.d("Message", "Message form the server : " + jsonResponse.getJSONArray("message"));
try {
// Parse categories from api.php
JSONArray categoriesArray = jsonResponse.getJSONArray("message");
// Lopping through all categories
for (int i = 0; i < categoriesArray.length(); i++) {
JSONObject c = categoriesArray.getJSONObject(i);
String name = c.getString("name");
categories1.add(name);
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
} catch (final JSONException e) {
Log.e("TAG", "Json parsing error: " + e.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
responseTextLogin.setText("No categories as active state in DB!");
}
}
});
}
});
return categories1;
}
或者您可以将代码放在一个线程中,并在填充 Spinner
之前在 UI 线程上获得结果
new Thread(new Runnable() {
@Override
public void run() {
categories = postRequest(MainActivity.postURIStage, body);
runOnUiThread(new Runnable() {
@Override
public void run() {
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
}
});
}
}).start();
我正在使用 okhttp3 获取数据,然后该数据显示在微调器中。但这并没有发生。微调器是空的,但是当我放下微调器时,我可以看到 okhttp3 获取了我的日期。我不太清楚为什么微调器一开始是空的。
private List<String> categories;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra("MESSAGE_LOGIN");
TextView textView = findViewById(R.id.textView);
textView.setText(message);
categories = new ArrayList<String>();
Log.d("LOGIN", "TEST!!!");
final Spinner spinner = findViewById(R.id.spinner);
RequestParams params = new RequestParams();
params.add("type","category");
RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded; charset=utf-8"), params.toString());
categories = postRequest(MainActivity.postURIStage, body);
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public List<String> postRequest(String postUrl, RequestBody postBody) {
final List<String> categories1 = new ArrayList<String>();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(postUrl)
.post(postBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// Cancel the post on failure.
call.cancel();
Log.d("FAIL", e.getMessage());
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseTextLogin = findViewById(R.id.responseTextLogin);
responseTextLogin.setText("Server is unreachable. Please try soon.");
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseTextLogin = findViewById(R.id.textView);
try {
String categoryResponseString = response.body().string();
Log.d("TEST:", categoryResponseString);
JSONObject jsonResponse = new JSONObject(categoryResponseString);
Log.d("Message", "Message form the server : " + jsonResponse.getJSONArray("message"));
try {
// Parse categories from api.php
JSONArray categoriesArray = jsonResponse.getJSONArray("message");
// Lopping through all categories
for (int i = 0; i < categoriesArray.length(); i++) {
JSONObject c = categoriesArray.getJSONObject(i);
String name = c.getString("name");
categories1.add(name);
}
} catch (final JSONException e) {
Log.e("TAG", "Json parsing error: " + e.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
responseTextLogin.setText("No categories as active state in DB!");
}
}
});
}
});
return categories1;
}
}
尽管我将 select 微调器中的数据对此 setOnItemSelectedListener 函数没有反应
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
我附上了可以看到空微调器的照片,当我单击它时会显示数据,但它一开始没有显示。
Empty spinner
When I clicked
按照您编写代码的方式,微调器会在返回结果之前填充空类别 ArrayList。
可以将Spinner population移动到postRequest方法的onResponse中
public List<String> postRequest(String postUrl, RequestBody postBody) {
final List<String> categories1 = new ArrayList<String>();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(postUrl)
.post(postBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// Cancel the post on failure.
call.cancel();
Log.d("FAIL", e.getMessage());
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseTextLogin = findViewById(R.id.responseTextLogin);
responseTextLogin.setText("Server is unreachable. Please try soon.");
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseTextLogin = findViewById(R.id.textView);
try {
String categoryResponseString = response.body().string();
Log.d("TEST:", categoryResponseString);
JSONObject jsonResponse = new JSONObject(categoryResponseString);
Log.d("Message", "Message form the server : " + jsonResponse.getJSONArray("message"));
try {
// Parse categories from api.php
JSONArray categoriesArray = jsonResponse.getJSONArray("message");
// Lopping through all categories
for (int i = 0; i < categoriesArray.length(); i++) {
JSONObject c = categoriesArray.getJSONObject(i);
String name = c.getString("name");
categories1.add(name);
}
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
} catch (final JSONException e) {
Log.e("TAG", "Json parsing error: " + e.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
responseTextLogin.setText("No categories as active state in DB!");
}
}
});
}
});
return categories1;
}
或者您可以将代码放在一个线程中,并在填充 Spinner
之前在 UI 线程上获得结果new Thread(new Runnable() {
@Override
public void run() {
categories = postRequest(MainActivity.postURIStage, body);
runOnUiThread(new Runnable() {
@Override
public void run() {
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
}
});
}
}).start();