无法从sqlite设置值到微调器?

时间:2016-03-02 13:33:58

标签: android sqlite spinner

这是我的要求我有sqlite db从服务器获取json响应并保存它并将该值设置为spinner其工作正常但是当我编辑listview时我需要编辑微调器的值将该值设置为微调器而不是获取我尝试了一切让我发布到目前为止我尝试过的内容: 这是我尝试设置微调器的适配器:

 final ArrayList<Model_Account> arrayobj = new ArrayList<Model_Account>();
            spinners_state_db = new Spinners_DB_State(getBaseContext());
            final Spinner_DB_AccountManagers acmobj = new Spinner_DB_AccountManagers(getBaseContext());

这是将选择值设置为微调器的地方:

 modelobj.setName(((edit_accnt.getString(edit_accnt.getColumnIndex(Model_Account.State)))));
                    int position=0;
                    position=spinner_state_Adapter.getPosition(modelobj);
                    spinner_state.setSelection(position);

总是得到位置-1不知道在哪里犯错了我使用材料旋转器!!

我的微调器类:

public class Spinners_DB_State extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 4;
    private static final String DATABASE_NAME = "spinners.db";


    public Spinners_DB_State(android.content.Context context) {

        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
                String Create_Spinner= " CREATE TABLE "  + Model_Account.Spinner_Table + "("
                        + Model_Account.spinners_id + " INTEGER PRIMARY KEY AUTOINCREMENT ,"
                        + Model_Account.StateName + " TEXT ,"
                        + Model_Account.PairlinkID + " INTEGER ,"
                        + Model_Account.Spinner_AccountManager + "INTEGER ,"
                        + Model_Account.UserID + " INTEGER "
                        + ");";
                    db.execSQL(Create_Spinner);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + Model_Account.Spinner_Table);
        onCreate(db);
    }
    public void InsertName(Model_Account modlobj){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentobj = new ContentValues();
        contentobj.put(Model_Account.StateName,modlobj.getName());
        contentobj.put(Model_Account.PairlinkID, modlobj.getStateid());
        db.insert(Model_Account.Spinner_Table, null, contentobj);
        db.close();
    }

    public List<Spinner_id> list() {
        String countQuery = "SELECT  * FROM " + Model_Account.Spinner_Table;
      SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        List<Spinner_id> listobj = new ArrayList<Spinner_id>();
        if (cursor.moveToFirst()) {
            do {
                Model_Account modelobj = new Model_Account();
                modelobj.setName(cursor.getString(cursor.getColumnIndex(Model_Account.StateName)));
                modelobj.setStateid(cursor.getInt(cursor.getColumnIndex(Model_Account.PairlinkID)));
                listobj.add(modelobj);

            } while (cursor.moveToNext());
        }db.close();
        return listobj;


    }
    public void update(Model_Account modlobj){
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentobj=new ContentValues();
        contentobj.put(Model_Account.StateName,modlobj.getName());
        contentobj.put(Model_Account.PairlinkID, modlobj.getStateid());
        db.update(Model_Account.Spinner_Table,contentobj,Model_Account.PairlinkID+ "= ?", new String[]{String.valueOf(modlobj.getStateid())});
        db.close();
}
    public Cursor getData(Model_Account id){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res =  db.rawQuery( "select * from " + Model_Account.Spinner_Table + " where " +  Model_Account.PairlinkID  + " = "  +  id.getStateid()  +"", null );
        return res;
    }
    public void StateInsertorUpdate(Model_Account modlobj){
        SQLiteDatabase db=this.getReadableDatabase();
        String query=" SELECT  * FROM " + Model_Account.Spinner_Table+ " WHERE " + Model_Account.PairlinkID +  "  =" +  modlobj.getStateid() +"";
        Cursor c = db.rawQuery(query, null);
        if (c.moveToFirst()) {
            update(modlobj);
        } else {
           InsertName(modlobj);
        }
    }
}

0 个答案:

没有答案