gpt4 book ai didi

java - 从 ArrayList 填充微调器时出现问题

转载 作者:行者123 更新时间:2023-12-02 13:15:28 26 4
gpt4 key购买 nike

我有一个 ArrayList,其中包含从文本文件读取的字符串。我希望这些字符串填充微调器,但我的代码不起作用,当我将鼠标悬停在该行上时,它显示“无法解析构造函数”:

ArrayAdapter adp = new ArrayAdapter(this, android.R.layout.simple_spinner_item, devicesAL);

这是我的其余代码,我已经提供了所有代码,因为我不知道原因在哪里。该特定位位于第二个 try block 中。有人可以帮忙吗?

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();



}
}

最佳答案

请注意,匿名类中的 this 引用该匿名类。您想要的是 MainActivity 的实例,而不是 Runnable 匿名子类的实例。

ArrayAdapter adp = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, devicesAL);
<小时/>

由于您的 Spinner 尚未填充,您可以

  1. 确保已填充 devicesAL
  2. 在将适配器填充并设置为 Spinner 后调用 adp.notifyDataSetChanged()
  3. Activity#runOnUiThread() 中运行与修改 UI 相关的所有内容。

关于java - 从 ArrayList 填充微调器时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43788357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com