发现我的问题,检查更新下为code
好吧,我有一个问题,从装载的sqlite的正确的价值为我的微调。下面是我的应用程序的构造
Okay, I have a problem loading the right value from Sqlite into my Spinner. Here is how my app is constructed
我的onCreate()建立了一个名为微调由spinTypes:
my onCreate() sets up a spinner called spinTypes by:
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);
}
我插入我的所有数据到工作正常分贝创建方法,我的问题是,当我想尝试和填充微调编辑的条目。
I have a creation method for inserting all my data into the db that works fine, my problem is when i want to try and populate the spinners for editing an entry.
该应用程序的工作原理这样:你preSS的添加按钮,弹出,在调用视图中创建一个列表项的对话框。我用两个微调器来创建它,一个是动态地填充第二名称微调基于该类型的类型。
The app works as so: you press an add button to popup a dialog that creates a list item in the calling view. I use two spinners to create it, one for the type which dynamically populates the second names spinner based on the type.
在我访问数据库对象,我返回字符串的微调,但由于某种原因,当我创造我的if语句检查字符串数组中的位置的第二微调每次设置位置为0。
when i access the database object i return the strings from the spinner but for some reason when i create my if statement checking the position of the string in the array the second spinner sets position 0 every time.
该方法包括if语句我上面提到的是像这样:
the method including the if statement i mentioned above is as so:
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我返回值。
The spinners all work but they do not end up on the value with tName i am returning.
谁能帮助呢?
**我记录从数据库返回的名称,并用它来找到我的错误项的指标,我返回正确的值,但微调仍然默认为0 **
** I am logging the name returned from the db and using it to find the index of the item in my error and i return the correct value, yet the spinner still defaults to 0**
任何人都知道可能是我的错误?请
Anyone know what my error may be? please
更新(最终code下面贴) 我做了相当多的变化来得到这个工作,以了解我所做的,你应该阅读这整个职位,使您不会错过任何东西!
Update(final code posted below) I made quite a bit of changes to get this to work, in order to understand what i did you should read this whole post so you don't miss anything!
加一个变量
私人诠释spinNamePos;
添加此行的的onCreate()
// add a listener to the spinner
spinTypes.setOnItemSelectedListener(TypesListener);
和创造了这个监听器,以便
and created this listener as so
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()
工作作为这样调用一个新的方法来设置第二微调的位置
i then changed my onPopulate()
to work as so calling a new method to set the second spinner's position
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;
}
很高兴看到这样的工作有任何问题或意见,鼓励
推荐答案
好了,所以我也似乎找出我自己的答案,周后!我遇到的问题是,我不知道,我的 OnItemSelectedListener()
被调用每一个我的适配器
设定时间我的第一个微调
。
Okay so I have seemed to figure out my own answer, weeks later! The problem I was having is that i was unaware that my OnItemSelectedListener()
gets called every time my adapter
is set for my first spinner
.
所以,现在意识到这一点,我设置的第一个微调
在 OnItemSelectedListener()
然后用这个微调ID
来设置我的第二个微调
适配器
。(即根据 ID
我设置了适配器 3选择。)
So now realizing this, i set the first spinner
in OnItemSelectedListener()
then use this spinner id
to set my second spinner
adapter
.(i.e based on the id
I set the adapter
of my second spinner
to 1 of 3 choices.)
在此适配器
设置我用的是值
从我的返回光标
查找字符串在此适配器
,并设置微调
到位置
在我的阵列
的字符串
的。
After this adapter
is set I use the value
returned from my cursor
to find the string in this adapter
and set the spinner
to the position
of the string
in my array
.
这一切,现在的伟大工程,我将会把最终的code以上在更新称号。感谢所有帮助。
It all works great now and I will be putting the final code above under the UPDATE title. Thanks for all help.
相关推荐
最新文章