2 dystroy dystroy 于 2013.02.22 15:11 提问

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

我想存储 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.}
  };
}

2个回答

curie_87
curie_87   2013.02.22 17:36
已采纳
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();
        }
    }
};
}
CSDN_Ask
CSDN_Ask   2013.02.27 11:42
public static final String PREFS_NAME = "MyPrefsFile";
public static final String SPINNER_VALUE = "SPINNER VALUE";

SharedPreferences settings = getSharedPreferences(PREFS_NAME , 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(SPINNER_VALUE, <the spinner value>);
editor.commit();
Csdn user default icon
上传中...
上传图片
插入图片