负荷微调源码使用文本和值 [英] load from spinner sqlite with text and value

查看:132
本文介绍了负荷微调源码使用文本和值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个微调加载数据到sqlite的

i have a spinner load data to sqlite

我有字段id和字段名在数据库中。

i have field id and field name in database.

private void loadSpinnerDataHama() {
        // database handler
        DatabaseSpinner db = new DatabaseSpinner(getApplicationContext()); 
        // Spinner Drop down elements
        List<String> lables = db.getAllLabels();
        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, lables);
        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // attaching data adapter to spinner
        spin2.setAdapter(dataAdapter);
    }

    public List<String> getAllLabels(){
    List<String> labels = new ArrayList<String>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_LABELS;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            labels.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    // returning lables
    return labels;
}

和的结果是

美国 - >值是USA

USA -> value is "USA"

法国 - >值是法国

当我改变code labels.add(cursor.getString(1)); labels.add(cursor.getString (0));

when i change the code labels.add(cursor.getString(1)); to labels.add(cursor.getString(0));

和的结果是

1 - >值为1

2 - >值是2

我尝试用 INT位置2 = spin2.getSelectedItemPosition()+ 1; 但价值ID /飞旋的位置,数据库不是ID

i try with int position2 = spin2.getSelectedItemPosition()+1; but the value is id/position of spinner , not the id of database.

如何在微调显示字段名。但值的名称标识

how to display field name on spinner. but the value is id of name

例:微调显示:

美国 - >值为1

法国 - >值为2

BR

亚历

推荐答案

这是非常简单的,你可以做的是落实/重present你所需要的一类,以下列方式:

It is really simple, what you can do is to implement/ represent what you need by a class, in the following manner :

创建以下类: SpinnerObject

public class SpinnerObject {

    private  int databaseId;
    private String databaseValue;

    public SpinnerObject ( int databaseId , String databaseValue ) {
        this.databaseId = databaseId;
        this.databaseValue = databaseValue;
    }

    public int getId () {
        return databaseId;
    }

    public String getValue () {
        return databaseValue;
    }

    @Override
    public String toString () {
        return databaseValue;
    }

}

使用这个类,你仍然可以使用Android实现一个ArrayAdapter(没有必要实现自己的,因为在的toString 方式提供了您要显示的字符串值,你还是商店数据库ID,你还需要)。

Using this class, you can still use the Android implementation of the ArrayAdapter (no need to implement your own, since the toString method provides the string value that you want to display, and you still store the database id that you also need).

现在,你必须创建你的名单如下:

Now you will have to create your list as follows :

public List < SpinnerObject> getAllLabels(){
    List < SpinnerObject > labels = new ArrayList < SpinnerObject > ();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_LABELS;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if ( cursor.moveToFirst () ) {
        do {
            labels.add ( new SpinnerObject ( cursor.getString(0) , cursor.getString(1) ) );
        } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    // returning labels
    return labels;
}

现在,你有价值观和IDS的对象列表中,您加载微调是这样的:

Now that you have the list of objects with the values and ids, you load the spinner this way :

private void loadSpinnerDataHama() {
    // database handler
    DatabaseSpinner db = new DatabaseSpinner(getApplicationContext()); 
    // Spinner Drop down elements
    List <SpinnerObject> lables = db.getAllLabels();
    // Creating adapter for spinner
    ArrayAdapter<SpinnerObject> dataAdapter = new ArrayAdapter<SpinnerObject>(this,
    android.R.layout.simple_spinner_item, lables);
    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // attaching data adapter to spinner
    spin2.setAdapter(dataAdapter);
}

和微调将显示值虽然具有其ID(从数据库)了。

And the spinner will display the values, while having their ids (from the database) too.

为了获取当前所选项目的ID,你这样做:

In order to retrieve the id of the currently selected item, you do this :

int databaseId = Integer.parseInt ( ( (SpinnerObject) spin2.getSelectedItem () ).getId () );

请尝试,让我知道会发生什么。

Please try it and let me know what happens.

这篇关于负荷微调源码使用文本和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆