如何将微调器的下拉项存储到安卓的sqlite数据库中



我想将微调器的下拉值存储到数据库中。我能够按照 android 开发人员站点中的教程进行 dopdown,但是当用户单击保存按钮时,我无法将该下拉值存储到数据库中。我不知道是否有可能,如果是,请告诉我如何通过一些示例做到这一点。提前致谢

这是我的代码

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.}
  };
}
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();
        }
    }
};
}

如果您正在寻找有关如何在Android应用程序中使用SQLite数据库的信息,我强烈建议您阅读记事本教程。

还是您被困在处理按钮点击上?

这并不能直接回答这个问题,但从设计角度来看,为了简单起见,将 UI 值存储为首选项可能是有意义的。

例如:

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();

当然,这取决于您的要求,ymmv。

相关内容

  • 没有找到相关文章

最新更新