单击时填充微调器
我正在尝试这个简单的程序,我尝试向服务器发送 POST
请求并获取响应并将它们存储到 spinner
中。这是我的代码;
fetchcolonynames("code"); //sending POST request
if (spin1 != null) {
//spin.setAdapter(adapter) doesn't work here
spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
spin1.setAdapter(adapter); //doesn't work spinner shows nothing on clicked
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
这是我的 fetchcolonynames() 方法:
private void fetchcolonynames(String input){
call=api.fetchcolonynames(input);
if(call!=null){
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.body() != null) {
JSONArray array = null;
try {
array = new JSONArray(response.body());
for(int i=0;i<array.length();i++){
arr.add(array.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
Collections.sort(arr); //data is correct here
adapter = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_spinner_item,arr);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
}
我的所有变量都是全局声明的。我想在用户单击时填充spinner
。我该如何实现这个目标?
I am trying out this simple program where I try to send a POST
request to the server and fetch the response and store them into the spinner
. This is my code;
fetchcolonynames("code"); //sending POST request
if (spin1 != null) {
//spin.setAdapter(adapter) doesn't work here
spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
spin1.setAdapter(adapter); //doesn't work spinner shows nothing on clicked
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
This is my fetchcolonynames()
method:
private void fetchcolonynames(String input){
call=api.fetchcolonynames(input);
if(call!=null){
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.body() != null) {
JSONArray array = null;
try {
array = new JSONArray(response.body());
for(int i=0;i<array.length();i++){
arr.add(array.getString(i));
}
} catch (JSONException e) {
e.printStackTrace();
}
Collections.sort(arr); //data is correct here
adapter = new ArrayAdapter<String>(view.getContext(), android.R.layout.simple_spinner_item,arr);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
}
}
All my variables are globally declared. I want to populate the spinner
, when the user clicks it. How do I accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论