我的应用程序允许用户命名和添加项目,然后生成文本文件(尚未编码)。例如,如果他们添加名为"冰箱"然后它生成fridge.txt。添加项目时,它也会被添加到ArrayList中,并传递给我的微调器。因此现在也可以在旋转器中看到冰箱。我需要实现的是,当用户从微调器中选择一个项目时,它还会根据生成的文本文件在textview中显示数据。我的问题是,如果我不知道用户将该项命名为什么,我该怎么做呢?
在我的下面的代码中,在onItemSelected()中我有一个条件来检查微调器的值是否等于一个字符串,但是由于用户将命名自己的项目如何让我的微调器做一些事情项目被选中了?
public class MainActivity extends AppCompatActivity {
String statusLink = "http://redacted.uk/pmt/status.txt";
String deviceLink = "http://redacted.uk/pmt/devices.txt";
String status;
final String degree = "\u00b0";
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);
for (String str : devicesAL) {
System.out.println(str);
}
}
while ((statusLine = sbr.readLine()) != null) {
System.out.println(statusLine);
status = statusLine;
System.out.println("Status = " + status);
TextView output = (TextView) findViewById(R.id.textView);
System.out.println(status);
}
for (String str : devicesAL) {
System.out.println(str);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//LOAD SPINNER
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter adp = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, devicesAL);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
adp.notifyDataSetChanged();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView output = (TextView) findViewById(R.id.textView);
if (parent.getItemAtPosition(position).equals("Water Cooler")) {
System.out.println("Water cooler selected");
output.setText(status);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
});
} finally {
sbr.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
}
答案 0 :(得分:0)
看起来您正在尝试使用if语句预测用户将输入的内容 - 即水冷却器。然后你只是写出“选择水冷却器”。如果只是打印出用户在文本框中选择的内容,只需删除if语句。
System.out.println(parent.getItemAtPosition(position) + " selected");
output.setText(status);
答案 1 :(得分:0)
您可以使用ArrayList的方法contains来查看用户在微调器中选择的项目。像这样的东西:
if (devicesAL.contains(parent.getItemAtPosition(position))) {
System.out.println(parent.getItemAtPosition(position) + " selected");
output.setText(status);
}