需要在Spinner控件中选择Sqlite保存的数据

时间:2011-11-08 14:20:11

标签: android

我在页面加载期间有一个Spinner Control绑定来自Sqllite的数据,如下所示。

public void BindSpinner() {
        ArrayList<HashMap<String, String>> alist = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map = new HashMap<String, String>();
        Cursor cursor;
        delivary_helper deliveryHelper;
        SimpleAdapter simpleAdapter;
        deliveryHelper = new delivary_helper(this);
        cursor = deliveryHelper.GetProblemCodes();
        cursor.moveToFirst();

        for (int i = 0; i < cursor.getCount(); i++) {
            map = new HashMap<String, String>();

            map.put("Deliv_Issue2_PKProblemCode",
                    cursor.getString(cursor.getColumnIndex("PKProblemCode")));
            map.put("Deliv_Issue2_ProblemCode",
                    cursor.getString(cursor.getColumnIndex("ProblemCode")));
            map.put("Deliv_Issue2_ProblemCodeDescription", cursor
                    .getString(cursor.getColumnIndex("ProblemCodeDescription")));
            map.put("Deliv_Issue2_VWReturn",
                    cursor.getString(cursor.getColumnIndex("VWReturn")));
            alist.add(map);
            cursor.moveToNext();
        }

        simpleAdapter = new SimpleAdapter(this, alist,
                R.layout.delivery_issue_2_spinner_row, new String[] {
                        "Deliv_Issue2_PKProblemCode",
                        "Deliv_Issue2_ProblemCode",
                        "Deliv_Issue2_ProblemCodeDescription",
                        "Deliv_Issue2_VWReturn" }, new int[] {
                        R.id.tvDelivIssue2PKProblemCode,
                        R.id.tvDelivIssue2ProblemCode,
                        R.id.tvDelivIssue2ProblemCodeDescription,
                        R.id.tvDelivIssue2VWReturn });
        delivery_line_problem.setAdapter(simpleAdapter);
    }

我已在Sqllite数据库表中保存了Selected项的ID。

问题是当我在保存Spinner控制数据并返回其他页面后返回到同一页面时,我无法在Spinner控件中选择保存的数据。如何选择该项目我已经保存并且还需要完全绑定微调器后面的数据。

任何人都可以为我提供解决方案。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你应该可以在你的微调器上设置OnItemSelectedListener。每当选择一个项目时,将其保存到活动中的实例变量中。这样你就会知道最后选择的项目。所以像这样:

yourSpinner.setOnItemSelectedSpinner(new AdapterView.OnItemSelectedListener(){
   public void onItemSelected (AdapterView<?> parent, View view, int position, long id){
     lastItemSelected = id;
   }
   public  void onNothingSelected (AdapterView<?> parent){
     //do nothing here
   }
 });