在Android中使用sqlite数据库的Spinner

时间:2011-11-11 05:52:47

标签: android sqlite

我有一个微调器,一个按钮和一个ListView。

我想从微调器中选择项目,单击按钮后,必须在ListView中显示微调器和数据库内容的匹配。数据库值是字符串数据类型。

请有人可以指导我如何做到这一点吗?

3 个答案:

答案 0 :(得分:1)

对于微调器,首先创建一个适配器,如:

ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.months, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

//这里R.array.months是在string.xml文件中创建的字符串数组,然后打开你的数据库:

if(mydb_expenditure!=null)
{
    mydb_expenditure.close();
}

mydb_expenditure=this.openOrCreateDatabase("Database_name", MODE_PRIVATE, null);
if(mydb_expenditure!=null)
{
    Cursor cursor = mydb_expenditure.rawQuery("select column name from "+TableName , null);
    cursor.moveToFirst();
    int column=cursor.getColumnIndex("column name");
    if(cursor!=null)
    {
        do
        {
            column=cursor.getInt(column);
            adapter12.add(column);
        }while(.moveToNext());
    }
    spinner.setAdapter(adapter12);
}

这会将您的数据库列值添加到微调器

答案 1 :(得分:0)

您可以按照以下步骤假设spinner填充了字符串数组:

i)在setOnItemSelectedListener事件中,通过

获取所选位置的位置

int pos=urspinner.getSelectedItemPosition()并通过

获取字符串
 strsel=arrstr[pos]; 

ii)按钮点击监听器使用strsel通过查询数据库填充listadapter

DONE !!!!

答案 2 :(得分:0)

例如:

     setContentView(R.layout.main);
        ListView lv=(ListView)findViewById(R.id.listview);
        Button b=(Button)findViewById(R.id.button);
        Spinner sp=(Spinner)findViewById(R.id.spinner);
        final ArrayList<String> options=new ArrayList<String>();

        Cursor cursor=dbHelper.getOptions();//method to get all options to be displayed in spinner,coming from your database

        if(cursor.getCount()>0)
        {
            cursor.moveToFirst();
            for(int i=0;i<cursor.getCount();i++)
            {
                 options.add(cursor.getString(cursor.getColumnIndex("field_name")));
                cursor.moveToNext();
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item,options);
            sp.setAdapter(adapter);
        }

       b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                  Cursor c=db_helper.getRelatedItems(spinner.getSelectedItem().toString()); //get related data from database according to the item selected in spinner
                  if(c.getCount()>0)
                  {         
                       CustomAdapter ca=new CustomAdapter(context,R.layout.listitem_layout,cursor,new String[]{"field 1","field 2"},new int[]{R.id.textview1,R.id.textview2});
                       //here you would have to pass your custom layout file containing textviews or other component to display your data using CustomAdapter.class which you had prepared 
                       listview.setAdapter(ca);
                       ca.notifyDataSetChanged();

                       listview.setOnItemClickListener(new OnItemClickListener() {    
                              @Override
                              public void onItemClick(AdapterView<?> arg0, View view,
                        int position, long id) {

                                //catch onClick on items of listview here
                              }
                       });
                 }  
           }
      });