在代码的“更新”下找到我的问题检查
好的,我在将 Sqlite 中的正确值加载到我的 Spinner 时遇到问题。这是我的应用程序的构建方式
我的 onCreate() 通过以下方式设置了一个名为 spinTypes 的微调器:
public class MyClass extends Activity{
// local members . . .
.
.
.
//spinner
private Spinner spinTypes, spinNames = null;
// string array decl.
private String [] types, names1, names2, names3 = null;
// array adapters for string arrays
private ArrayAdapter<String> typesAdapter, names1Adapter,
names2Adapter, names3Adapter;
// create a Dbhelper object to access the db
private DbHelper mdbHelper = null;
// on create
@Override
public void onCreate(Bundle savedIstanceState){
// call to super class
// set my content view
// instantiate all members
// Dbhelper object to access the db
mdbHelper = new DbHelper(getBaseContext());
// spinners
spinTypes = (Spinner) findViewById(R.id.spin_types);
spinNames = (Spinner) findViewById(R.id.spin_names);
// get string arrays from resources and create a ArrayAdapter<string>
types = getResources().getStringArray(R.array.types);
typesAdapter = new ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_spinner_item, types);
typesAdapter. setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// want to create a dynamic spinner based on these arrays
names1 = getResources().getStringArray(R.array.names_1);
names2 = getResources().getStringArray(R.array.names_2);
names3 = getResources().getStringArray(R.array.names_3);
// do this for all names++ arrays
names1Adapter = new ArrayAdapter<String>(getBaseContext(),
android.R.layout.simple_spinner_item, names1);
names1Adapter. setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// etc . . . for the other two
// set the adapter for the types spinner
spinTypes.setAdapter(typesAdapter);
}
我有一个创建方法可以将我的所有数据插入到工作正常的数据库中,我的问题是当我想尝试填充微调器以编辑条目时。
该应用程序是这样工作的:您按下添加按钮以弹出一个对话框,该对话框在调用视图中创建一个列表项。我使用两个微调器来创建它,一个用于根据类型动态填充第二个名称微调器的类型。
当我访问数据库对象时,我从微调器返回字符串,但由于某种原因,当我创建 if 语句检查字符串在数组中的位置时,第二个微调器每次都将位置设置为 0。
包括我上面提到的if语句的方法是这样的:
public void populate(){
mdbHelper.open();
// if there is an item create a cursor rowId != null
Cursor mTypes = mdbHelper.fetchItem(rowId);
if(mTypes.moveToFirst()){
String tType = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_TYPE));
String tName = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_NAME));
int t = spinTypes.getPosition(tType);
spinTypes.setSelection(t);
// ** this does not seem to do the job i want it to**
if(t == 0){
spinNames.setAdapter(names1Adapter); // set the adapter based on t value
int n1 = names1Adapter.getPosition(tName); // use name return from db to get pos
spinNames.setSelection(n1); // set the position
}else if(t ==1){
spinNames.setAdapter(names2Adapter);
int n2 = names1Adapter.getPosition(tName);
spinNames.setSelection(n2);
}else{
spinNames.setAdapter(names3Adapter);
int n3 = names3Adapter.getPosition(tName);
spinNames.setSelection(n3);
}
}// end populate
} // end class
微调器都可以工作,但它们并没有以我返回的 tName 值结束。
有人能帮忙吗?
** 我正在记录从数据库返回的名称并使用它在我的错误中查找项目的索引并返回正确的值,但微调器仍默认为 0**
任何人都知道我的错误可能是什么?请
更新(下面发布的最终代码) 我做了很多更改以使其正常工作,为了了解我做了什么,您应该阅读整篇文章,这样您就不会错过任何内容!
之前添加了一个变量onCreate()
private int spinNamePos;
在我的最后一行之后添加了这一行onCreate()
// add a listener to the spinner
spinTypes.setOnItemSelectedListener(TypesListener);
并创建了这个监听器
OnItemSelectedListener TypesListener = new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
// use the position of this spinner to set the second spinner
switch(pos){
case 0 :
spinNames.setAdapter(names1Adapter);
break;
//etc. . for the other two
case 1:
// etc
break;
}
case 2:
// etc
break;
}
// created an int for the position of the second spinner before calling onCreate()
// and use it to set this spinners position
spinNames.setSelection(spinNamePos);
// this value is grabbed in onSavedInstanceState() and used to set the pos when onRestoreInstanceState() is called to handle orientation changes and activity paused
}
};
然后我改变了我onPopulate()
的工作,所以调用一个新方法来设置第二个微调器的位置
public void populate(){
mdbHelper.open();
// if there is an item create a cursor rowId != null
Cursor mTypes = mdbHelper.fetchItem(rowId);
if(mTypes.moveToFirst()){
String tType = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_TYPE));
String tName = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_NAME));
int t = spinTypes.getPosition(tType);
spinTypes.setSelection(t);
// call to the new method
spinNamePos = setSpinNamesPos( t, tName);
// set the position
spinNames.setSelection(spinNamePos)
}// end populate
// new method to set the position when i want to update/change the list item
private int setSpinNamesPos(int m, String name){
int pos = 0;
switch(m){
case 0:
pos = names1Adapter.getPosition(name);
break;
case 1:
pos = names2Adapter.getPosition(name);
break;
case 2:
pos = names3Adapter.getPosition(name);
break;
}
return pos;
}
很高兴看到这个工作鼓励任何问题或评论