开发者社区> 问答> 正文

如何把spinner中的dropdown item存储到sqlite数据库?

a123456678 2016-07-18 13:54:09 719

我想存储 spinner中的dropdown值到数据库。我可以按照android开发网站的说明中获得dropdown,但是当用户点击保存按钮时,不能把dropdown的值存储到数据库中。请问如何把spinner中的dropdown item存储到sqlite数据库?
我用的是下面的代码:

public class Akshay extends Activity
{
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //Spinner For Selecting Room
    Spinner spinner_room = (Spinner) findViewById(R.id.spinner_for_Room_type_screen_2);
    ArrayAdapter adapter_room = ArrayAdapter.createFromResource(this,
        R.array.room_array, android.R.layout.simple_spinner_item);
    adapter_room.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_room.setAdapter(adapter_room);
    spinner_room.setOnItemSelectedListener(new MyOnItemSelectedListener_room());
  }
}

// Listener Implementation of Spinner For Selecting Room 
public class MyOnItemSelectedListener_room implements OnItemSelectedListener
{
  public void onItemSelected(AdapterView parent, View view, int pos, long id)
  {
  }

  public void onNothingSelected(AdapterView parent)
  { // Do nothing.}
  };
}
存储 数据库 Android开发 数据库管理
分享到
取消 提交回答
全部回答(1)
  • a123456678
    2019-07-17 19:57:46
    public class Akshay extends Activity {
    
       public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.configuration);
        // Spinner For Selecting Room
    
        Spinner spinner_room = (Spinner) findViewById(R.id.spinner_for_Room_type_screen_2);
    
        ArrayAdapter<CharSequence> adapter_room = ArrayAdapter.createFromResource(this,
                            R.array.room_array,android.R.layout.simple_spinner_item);
    
        adapter_room.setDropDownViewResourc(android.R.layout.simple_spinner_dropdown_item);
        spinner_room.setAdapter(adapter_room);
    
        spinner_room.setOnItemSelectedListener(new Listener_Of_Selecting_Room_Spinner());
    }
    
    // Listener Implementation of Spinner For Selecting Room
    
    public static class Listener_Of_Selecting_Room_Spinner implements OnItemSelectedListener 
    {
        static String RoomType;
    
        public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) 
        {   
            // By using this you can get the position of item which you
            // have selected from the dropdown 
    
            RoomType = (parent.getItemAtPosition(pos)).toString();
        }
    
        public void onNothingSelected(AdapterView<?> parent) 
        {           
            // Do nothing.
        }
    };
    
    // Listener Implementation For Saving Number Of Board
    private OnClickListener btnListener_Btn_Save_Room_Board = new OnClickListener() 
    {
        public void onClick(View view) 
        {
            DBAdapter dbAdapter1 = new DBAdapter(view.getContext());
    
            String room;
            try {
                dbAdapter1.createDataBase();
                dbAdapter1.openDataBase();
    
                // Here i am using the object RoomType which i have got from 
                // the Listener of spinner
    
                room = Listener_Of_Selecting_Room_Spinner.RoomType;
    
                ContentValues initialValues1 = new ContentValues();
    
                initialValues1.put("RoomType", room);
    
                //Here i am storing it(RoomType) to the database
    
                dbAdapter1.InsertNumberOfBoardInDB("Configuration", null,initialValues1);   
            }
            catch (Exception e) {
            }
            finally {
                dbAdapter1.close();
            }
        }
    };
    }
    
    0 0
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

推荐文章
相似问题
推荐课程