我有一個ArrayList包含從文本文件讀入的字符串。我想這些字符串來填充微調,但我的代碼不工作,它說「無法化解的構造」當我將鼠標懸停在該行:麻煩從ArrayList填充微調器
ArrayAdapter adp = new ArrayAdapter(this, android.R.layout.simple_spinner_item, devicesAL);
這裏是我的代碼的其餘部分,我提供了所有的這是因爲我不知道原因在哪裏。這個特定的位在第二個try塊中。任何人都可以幫忙嗎?
package com.example.iotdevicestatus;
public class MainActivity extends AppCompatActivity {
String statusLink = "http://redacted.uk/pmt/status.txt";
String deviceLink = "http://redacted.uk/pmt/devices.txt";
String status = "status testttest";
ArrayList<String> devicesAL = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
// Set up connection to device.txt on web server
URL deviceUrl = new URL (deviceLink);
URLConnection deviceConn = deviceUrl.openConnection();
deviceConn.setDoOutput(true);
deviceConn.connect();
InputStream dis = deviceConn.getInputStream();
InputStreamReader disr = new InputStreamReader(dis, "UTF-8");
BufferedReader dbr = new BufferedReader(disr);
String deviceLine;
// Set up connection to status.txt on web server
URL statusUrl = new URL(statusLink);
URLConnection statusConn = statusUrl.openConnection();
statusConn.setDoOutput(true);
statusConn.connect();
InputStream sis = statusConn.getInputStream();
InputStreamReader sisr = new InputStreamReader(sis, "UTF-8");
BufferedReader sbr = new BufferedReader(sisr);
String statusLine;
try {
while ((deviceLine = dbr.readLine()) != null) {
System.out.println(deviceLine);
devicesAL.add(deviceLine);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adp = new ArrayAdapter(this, android.R.layout.simple_spinner_item, devicesAL);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
}
while ((statusLine = sbr.readLine()) != null) {
System.out.println(statusLine);
status = statusLine;
System.out.println("Status = " + status);
//al.add(line); - use in case of multiple statuses
TextView output = (TextView) findViewById(R.id.textView);
System.out.println(status);
output.setText(status);
}
for (String str : devicesAL) {
System.out.println(str);
}
} finally {
sbr.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
看來你沒有'ArrayAdapter'進口,或者你沒有一些要求在'Gradle' – DarkCygnus
無法解析構造「ArrayAdapter(匿名了java.lang.Runnable,INT,java.util.ArrayList中)' –
JB2000
看@ m0skit0回答 – DarkCygnus