android - 动态创建微调器并从 SQLite 设置其值

标签 android sqlite dynamic spinner

在代码的“更新”下检查我的问题

好的,我在将正确的值从 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);

}

我有一个创建方法可以将我的所有数据插入工作正常的数据库中,我的问题是当我想尝试填充微调器以编辑条目时。

该应用程序的工作方式如下:您按下添加按钮弹出一个对话框,在调用 View 中创建一个列表项。我使用两个微调器来创建它,一个用于根据类型动态填充第二个名称微调器的类型。

当我访问数据库对象时,我从微调器返回字符串,但出于某种原因,当我创建 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;
}

很高兴看到这个工作,欢迎提出任何问题或评论

最佳答案

好吧,几周后我似乎找到了自己的答案!我遇到的问题是,每次为我的第一个 spinner 设置 adapter 时,我都不知道我的 OnItemSelectedListener() 会被调用。

现在意识到这一点,我在 OnItemSelectedListener() 中设置了第一个 spinner 然后使用这个 spinner id 设置我的第二个 spinner adapter。(即基于 id 我将第二个 spinneradapter 设置为3 个选项中的 1 个。)

设置此适配器后,我使用从我的游标返回的在此适配器中查找字符串> 并将 spinner 设置到我的 arraystringposition

现在一切正常,我将把上面的最终代码放在 UPDATE 标题下。感谢所有帮助。

关于android - 动态创建微调器并从 SQLite 设置其值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12962268/

相关文章:

android - 应用程序中的 iOS ADB 工具

安卓优酷 : Process command finished with non-zero exit value 2

android - Google Analytics Easy Tracker 问题

python - Django 动态表单集 UpdateView 不更新

javascript - 以动态形式计算已检查的行数

mysql - DATE 在动态之间

java - Secure.getString(mContext.getContentResolver(), "bluetooth_address") 在 Android O 中返回 null

android - 明确地关闭 SQLite 数据库中的 Cursor,需要还是不需要?

iphone - 如何让 SQLITE 数据库文件在 DropBox 中同步?

ruby-on-rails - 正确显示保存到数据库中的图像作为 blob