gpt4 book ai didi

android - 检索从 sqlite 数据库加载的微调器中选定元素的 id

转载 作者:行者123 更新时间:2023-11-30 03:19:00 25 4
gpt4 key购买 nike

我要检索的 ID 应与 sqlite 数据库中的相同,而不是微调器项目行的 ID。

我使用了以下链接中的代码并得到了一些异常。

load from spinner sqlite with text and value

我认为问题出在这里:

anId = Integer.valueOf(( (AnSco) spnAnsco.getSelectedItem () ).getIdan());

我尝试过:

anId = ( (AnSco) spnAnsco.getSelectedItem () ).getIdan();

总是同样的问题。

加载微调器

private void loadSpinnerAnsco() {
List<String> ansco = new ArrayList<String>();
db = new DatabaseHelper(this);
boolean ok = true;
try {
SQLiteDatabase dbs = db.getWritableDatabase();

cursor = dbs.rawQuery(
"Select idan as _id, ansco from TAnSco order by ansco",
null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
ansco.add(cursor.getString(1));
} while (cursor.moveToNext());
}

// closing connection
cursor.close();
dbs.close();

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ansco);

// Drop down layout style - list view with radio button
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
spnAnsco.setAdapter(dataAdapter);

} catch (Exception ex) {
ok = false;
AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setMessage(ex.toString());
b.show();
} finally {
if (ok) {
db.close();
}
}

}

获取选中的项目

public class anscoSpinner implements OnItemSelectedListener {

@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String label = parent.getItemAtPosition(position).toString();
AnSco an=new AnSco(label);
anId = Integer.valueOf(( (AnSco) spnAnsco.getSelectedItem () ).getIdan());
loadSpinnerPeriode(anId);
// Showing selected spinner item
Toast.makeText(parent.getContext(), "You selected: " + label+ " AnId: "+anId,
Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub

}
// TODO Auto-generated method stub

}

项目等级 Ansco

public class AnSco {

int idan;
String ansco;

public AnSco(int idan, String ansco) {
this.idan = idan;
this.ansco = ansco;
}

public AnSco(String ansco) {
this.ansco = ansco;
}

public AnSco() {
// TODO Auto-generated constructor stub
}

public int getIdan() {
return idan;
}
public void setIdan(int idan) {
this.idan = idan;
}

public String getAnsco() {
return ansco;
}
public void setAnsco(String ansco) {
this.ansco = ansco;
}

}

堆栈跟踪:

10-21 13:34:59.319: E/AndroidRuntime(438): FATAL EXCEPTION: main
10-21 13:34:59.319: E/AndroidRuntime(438): java.lang.ClassCastException: java.lang.String
10-21 13:34:59.319: E/AndroidRuntime(438): at com.android.moyenne.activity.MoyenneMain$anscoSpinner.onItemSelected(MoyenneMain.java:306)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.widget.AdapterView.fireOnSelected(AdapterView.java:864)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.widget.AdapterView.access$200(AdapterView.java:42)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.os.Handler.handleCallback(Handler.java:587)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.os.Handler.dispatchMessage(Handler.java:92)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.os.Looper.loop(Looper.java:123)
10-21 13:34:59.319: E/AndroidRuntime(438): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-21 13:34:59.319: E/AndroidRuntime(438): at java.lang.reflect.Method.invokeNative(Native Method)
10-21 13:34:59.319: E/AndroidRuntime(438): at java.lang.reflect.Method.invoke(Method.java:521)
10-21 13:34:59.319: E/AndroidRuntime(438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-21 13:34:59.319: E/AndroidRuntime(438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-21 13:34:59.319: E/AndroidRuntime(438): at dalvik.system.NativeStart.main(Native Method)
10-21 13:35:02.119: I/Process(438): Sending signal. PID: 438 SIG: 9

最佳答案

I used this tutorial load from spinner sqlite with text and value and get some exceptions during running.

我使用了您提供的链接中的解决方案。我建议进行以下更改。

在您的数据库类中创建以下方法

public List < Ansco> getAllLabels(){
List < Ansco > lansco = new ArrayList < Ansco > ();
// Select All Query
String selectQuery = "SELECT * FROM " TAnSco;// tAnsco is your table name?

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if ( cursor.moveToFirst () ) {
do {
lansco.add ( new Ansco ( cursor.getString(0) , cursor.getString(1) ) );
} while (cursor.moveToNext());
}

// closing connection
cursor.close();
db.close();

// returning labels
return lansco;
}

然后在你的 load spinner 方法中

 List <Ansco> ansco = db.getAllLabels();
ArrayAdapter<Ansco> dataAdapter = new ArrayAdapter<Ansco>(this,
android.R.layout.simple_spinner_item, ansco);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnAnsco.setAdapter(dataAdapter);

然后在您的微调器选择中

  int databaseId = Integer.parseInt ( ( (SpinnerObject) spin2.getSelectedItem () ).getId () );

并更改您的 Ansco 类

public class AnSco {

String idan; // change to string
String ansco;

public AnSco(String idan, String ansco) {
this.idan = idan;
this.ansco = ansco;
}

public AnSco(String ansco) {
this.ansco = ansco;
}

public AnSco() {
// TODO Auto-generated constructor stub
}

public String getIdan() {
return idan;
}
public void setIdan(String idan) {
this.idan = idan;
}

public String getAnsco() {
return ansco;
}
public void setAnsco(String ansco) {
this.ansco = ansco;
}

}

关于android - 检索从 sqlite 数据库加载的微调器中选定元素的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19496580/

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