如何从微调器中获取DB的值

时间:2012-03-12 12:41:09

标签: android sqlite

如何从数据库中获取值到微调器中。我想在DB上显示UI上的值或数据。是否需要使用游标将值填充到UI上?

3 个答案:

答案 0 :(得分:2)

How to populate a Spinner widget from a database

跟着它,它非常简单。

答案 1 :(得分:2)

是的,需要游标来获取结果集对象或数据作为结果集。您可以从

找到解决方案

Android Developers Blog

答案 2 :(得分:2)

当spinner值由postion改变时,我只需按照事物从数据库中获取值 -

Java类

Spinner spinner = (Spinner) findViewById(R.id.spinner1);
//set some adapter here
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

public class MyOnItemSelectedListener implements OnItemSelectedListener 
{
@SuppressWarnings("unused")
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
try
    {
    switch(parent.getId())
    {
    case R.id.spinner2:

        int selected_spinner_item_position=pos;

        String expired_spinner_item =parent.getItemAtPosition(pos).toString();
        if(selected_spinner_item_position==0)
        {
            //am just using simple cursor adapter here and getting the values from database using dh.fetchAll() when the position is 0
            sc_adapter(dh.fetchAll());
            spinnertemp = selected_spinner_item_position;
        }
        }
    }
}
}

数据库

public Cursor fetchAll()
{
    SQLiteDatabase db = this.getReadableDatabase();
    cursor = db.query(t1, new String[] {"_id",task, pname, prior, time, dateformat, duptitle,dupname}, "stat=0", null, null, null, prior);
    return cursor;
}

按照这些步骤做你需要的。