如何解决Android Studio:为什么微调框无法正确显示?
这是关于微调器的。他从ArrayList获取数据,而ArrayList从http请求获取数据。 http请求位于json中,并进行相应的转换。
package de.it18b.saufomat;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
public TextView mTextViewResult;
ArrayList<String> tester;
public MainActivity() throws IOException {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tester=new ArrayList<>();
try {
run("http://111.111.111.1/helloWorld");
} catch (IOException e) {
e.printStackTrace();
}
}
public void btnc1(View view) throws IOException {
run("http://111.111.111.1/helloWorld");
}
public void btnc2(View view) throws IOException {
run("http://111.111.111.1/helloWorld");
}
public void run(String url) throws IOException {
mTextViewResult = findViewById(R.id.text_view_result);
Spinner spinner = findViewById(R.id.spinner1);
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call,IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call,Response response) throws IOException {
if (response.isSuccessful()) {
final String myResponse = response.body().string();
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
try {
JSONObject jsonObject=new JSONObject(myResponse);
String test0 = jsonObject.toString();
tester.clear();
tester.add(0,"Auswählen");
JSONArray jsonArray=jsonObject.getJSONArray("data");
for(int i=0; i < jsonArray.length(); i++) {
tester.add(jsonArray.getString(i));
}
mTextViewResult.setText(test0);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
});
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,tester);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,View view,int position,long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(),text,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
问题在于,尽管启动应用程序时内容已填满,但微调器无法直接显示内容。如果单击小箭头,则条目在其中,但无法选择。如果再次单击button2(Bestellen)并执行请求(包括在微调器中插入),则第一个条目将正确显示,您可以选择其他数据。
有人可以解决吗? 非常感谢
随附的视频可帮助您了解问题
https://puu.sh/GrWt0/b9cfca5f97.mp4
解决方法
不是创建一个空的ArrayList,而是将一个项目添加到arrayList,以便在微调器运行时,它具有默认选择。然后,当从远程获取数据并且在微调器下拉列表中可用数据时,如果选择微调器项目,它将选择该项目而不会延迟。
示例:
List tester = new ArrayList<>();
tester.add("Nothing selected");// or use empty String
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。