Android - 如何在选择Spinner时更新ListView

时间:2012-10-22 06:47:51

标签: android database listview spinner

我正在尝试在选择微调器中的项目时更新listview,但它不能正常工作。

这是我的微调器:

Spinner spnTypeSchool = (Spinner)findViewById(R.id.spnType);
spnTypeSchool.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,strarray));
spnTypeSchool.setOnItemSelectedListener(new OnItemSelectedListener() 
    {
     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
     {
          spnItemClick = arg0.getItemIdAtPosition(arg2)+1;
          myDbHelper.open(); // my databaseHelper
         Cursor spnSelectSchoolType = myDbHelper.getSchoolTypeList(spnItemClick);
        if(spnSelectSchoolType != null && spnSelectSchoolType.moveToFirst())
        {
          do
          {        
              schoolTypeSelectedForSpinner.add(spnSelectSchoolType.getString(0).toString());
          }
          while(spnSelectSchoolType.moveToNext());
        }
        String[] strSchoolByTypeSelected = new String[schoolTypeSelectedForSpinner.size()];
        schoolTypeSelectedForSpinner.toArray(strSchoolByTypeSelected);
        myDbHelper.close();
    }
                        public void onNothingSelected(AdapterView<?> arg0) {/* TODO Auto-generated method stub*/}
   });

这是我的ListView:

final ListView result = (ListView)findViewById(R.id.resultListView);
result.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,schoolTypeSelectedForSpinner));

这是我的数据库查询

public Cursor getSchoolTypeList(long sType)
    {
       return db.rawQuery("select school_name from tbl_school where s_type =\""+sType+"\"", null);
    }

我面临的问题是,当我选择项目时,列表视图无法正确更新。第一个选择和第二个选择在微调器上组合并返回一个不正确的值。

提前感谢您的帮助......

0 个答案:

没有答案