【问题标题】:Dynamically creating a spinner and setting its Value from SQLite从 SQLite 动态创建微调器并设置其值
【发布时间】:2012-10-09 08:39:35
【问题描述】:

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

好的,我在将 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;
}

很高兴看到这个工作,鼓励任何问题或 cmets

【问题讨论】:

  • 您是否在类型为Spinner 的字符串数组与数据库中的其他名称数组之间建立了映射关系? Spinner 默认为 0 到底是什么意思(您只获取(并设置)第一个适配器,或者无论您选择什么,名称微调器中的选定项目都设置为 0)?
  • 在数据库中我有两个字符串。我用光标项拉动两者,然后使用第一个字符串来设置使用字符串数组资源的第一个微调器的位置。然后根据这个字符串,我将三个适配器中的一个设置为我的下一个微调器,然后使用第二个字符串在我的数组中找到这个元素的位置,并尝试将我的微调器设置到适配器的那个位置。但是第二个微调器一直默认为数组的 0 索引。
  • 这仅用于更新数据库中的现有项目。如果我要添加一个项目,我的微调器工作正常。当我去更新项目时,我的第二个微调器每次都默认为数组的 0 索引,但是我从数据库返回了正确的字符串。出于某种原因,当我调用spinNames.setSelection(n1); 时,它没有设置位置。
  • n1 的值与0 不同?另外,如果您使用此代码设置选择:spin.post(new Runnable() {@Override public void run() {spin.setSelection(n1)}}); 选择是否有效?
  • 不,不幸的是,这只会使问题更加复杂。

标签: android sqlite dynamic spinner


【解决方案1】:

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

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

设置此adapter 后,我使用从cursor 返回的value 在此adapter 中查找字符串,并将spinner 设置为@ 中stringposition 987654339@。

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

【讨论】:

    猜你喜欢
    • 2013-01-12
    • 2013-03-17
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多