【问题标题】:Can't save spinner selected item to database, it saves android.sqlite.cursor@ not the string无法将微调器选定的项目保存到数据库,它保存 android.sqlite.cursor@ 而不是字符串
【发布时间】:2014-02-24 11:12:59
【问题描述】:

我的问题是sp1.getSelectedItem().toString(); 仅将数据保存为android.database.SQLite.cursor@blablabla ...它没有得到我声明的字符串...我该怎么做?

这是我的光标代码[填充微调器]

 cursor = mySQLiteAdapter.queueSchoolAll();
    cursor.requery();
    String[] from1 = new String[]{Database.KSCHOOLCODE};
    int[] to1 = new int[]{R.id.rt1};       
    cursorAdapter =
        new SimpleCursorAdapter(this, R.layout.a_row_school, cursor, from1, to1);
    sp1.setAdapter(cursorAdapter);

这是保存数据的按钮代码:

 String data1 = (String)sp1.getSelectedItem().toString();
 mySQLiteAdapter.insertGrade(data1);

【问题讨论】:

    标签: android sqlite tostring


    【解决方案1】:

    试试看

      String data1 = ((Cursor) sp1.getSelectedItem()).getString(0);
      mySQLiteAdapter.insertGrade(data1);
    

    【讨论】:

    • 我试过你的代码,它没有保存 android.database .....但它只保存了 id 我该如何更改它
    • 在 getString(0) 中将索引 0 更改为您要保存的其他列索引
    • wooahh,就像魔术一样工作,我根据我的表格列 (2) 的排列对其进行了更改,并且非常感谢它的工作。我真的很感谢你的回答,你保存了我的任务:D
    【解决方案2】:

    您需要从游标中获取数据。 例如:

    public xxxxxx onItemClickFunction(..., int position,....){
    int pos = cursor.getPosition();//saving the position just in case
    
    cursor.moveToPostion(position);// moving to the position where a click is fired
    String val = cursor.getString(column index);
    
    cursor.moveToPosition(pos);// moving back to original position.
    
    }
    

    【讨论】:

    • 哦...我明白了,所以我只需要在直接保存之前提供位置...我会试试谢谢:)
    猜你喜欢
    • 2019-01-05
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2018-09-21
    • 2012-07-04
    • 2018-12-08
    相关资源
    最近更新 更多