gpt4 book ai didi

java - 同时阅读 3 个所选项目

转载 作者:IT王子 更新时间:2023-10-29 06:21:51 25 4
gpt4 key购买 nike

如何同时读取 3 个微调器选择的项目?我在 WorkDetails.java 中有 spinner1,2,3,它们的值已插入到 SQLite 中。在我的 UpdatePage.java 中,我想检索出选定的项目,并且选定的项目应该首先显示在微调器 project1、project2 和 project3 中。

更新页面.java

public void RetrievePage(String name,String date, String id) {
final String name2 = name;
final String date2=date;
final String id2 = id;
final EditText name3 = (EditText) findViewById(R.id.editText9);
final EditText date3 = (EditText) findViewById(R.id.editText12);
name3.setText(name2);
date3.setText(date2);

database = dbHelper.getWritableDatabase();
c = database.rawQuery(
"SELECT i.Weather, i.Status,w.Subcontractors, w.NumberOfPerson,"+
"w.NumberOfHours, wd.Project, wd.WorkDescription, wd.Per,"+
"wd.TimeIn, wd.TimeOut FROM Information i LEFT JOIN WorkForce w "+
"ON w.TInfo_id = i._id "+
"LEFT JOIN WorkDetails wd ON wd.Twf_id=w._id "+
"WHERE i.Name = ? AND i._id= ? ",
new String[]{String.valueOf(name2),String.valueOf(id2)}, null);
if (c != null) {
while (c.moveToNext()) {
Details WD = new Details();
String Project11 = c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));
String Project22 =c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));
String Project33 =c.getString(c.getColumnIndexOrThrow(MyDatabaseHelper.Project));

addProject1(Project11);
addProject2(Project22);
addProject3(Project33);
WD.setProject(Project11);
WD.setProject(Project22);
WD.setProject(Project33);
}
}
c.close();
}

public void addProject1(String l) {
project1 = (Spinner) findViewById(R.id.spinner8);
String[] arr = new String[]{"1","2","3"};
List<String> list = new ArrayList<String>();
String project11 = l;
list.add(project11);
for (String s : arr) {
if (!list.contains(s)) {
list.add(s);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
project1.setAdapter(adapter);
}

public void addProject2(String d) {
project2 = (Spinner)findViewById(R.id.spinner9);
String[] arr = new String[]{"1","2","3"};
List<String> list = new ArrayList<String>();
String project22 = d;
list.add(project22);
for (String s:arr) {
if(!list.contains(s)) {
list.add(s);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
project2.setAdapter(adapter);
}

public void addProject3(String e) {
project3 = (Spinner)findViewById(R.id.spinner13);
String[] arr = new String[]{"1","2","3"};
List<String> list = new ArrayList<String>();
String project33 = e;
list.add(project33);
for (String s:arr) {
if (!list.contains(s)) {
list.add(s);
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(UpdatePage.this, android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
project3.setAdapter(adapter);
}

假设spinner1值为1,spinner2值为2,spinner3值为3。当来到updatePage时,微调器 project1 应首先显示 1,project2 应显示 2,依此类推。但是现在的问题是project1,2,3先显示值1...我该如何实现呢?

最佳答案

您可以为此尝试一些逻辑

从数据库中获取三个值。让他们成为

selected_value_of_spinner1,selected_value_of_spinner2,selected_value_of_spinner3

然后定义你的方法getPosition如下

public int getPosition(Spinner your_spinner, String string_value){

int position = 0;

for (int i=0;i<your_spinner.getCount();i++){
if (your_spinner.getItemAtPosition(i).equals(string_value)){
position = i;
}
}
return position;
}

在微调器中显示从数据库中获取的值,如下所示

your_spinner1.setSelection(getPosition(your_spinner1, selected_value_of_spinner1));
your_spinner2.setSelection(getPosition(your_spinner2, selected_value_of_spinner2));
your_spinner3.setSelection(getPosition(your_spinner3, selected_value_of_spinner3));

基本上这里发生的是

  1. 从数据库中获取值

  2. 您通过 getPosition() 方法匹配微调器中从数据库中获取的值以获取其在微调器中的位置。

  3. 根据您通过 setSelection(position) 方法从第 2 步获得的位置设置微调器值。

关于java - 同时阅读 3 个所选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33250968/

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