如何将微调器的下拉项存储到android的sqlite数据库
我想将微调器的下拉值存储到数据库中。我可以根据android开发者网站上的教程获得dopdown,但当用户单击“保存”按钮时,我无法将该下拉值存储到数据库中。我不知道是否可能。如果是,请告诉我如何使用一些示例。 提前谢谢 这是我的密码如何将微调器的下拉项存储到android的sqlite数据库,android,sqlite,spinner,Android,Sqlite,Spinner,我想将微调器的下拉值存储到数据库中。我可以根据android开发者网站上的教程获得dopdown,但当用户单击“保存”按钮时,我无法将该下拉值存储到数据库中。我不知道是否可能。如果是,请告诉我如何使用一些示例。 提前谢谢 这是我的密码 public class Akshay extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedIns
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应用程序中使用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();
public静态最终字符串PREFS\u NAME=“MyPrefsFile”;
公共静态最终字符串微调器\u VALUE=“微调器值”;
SharedReferences设置=GetSharedReferences(首选项名称,0);
SharedReferences.Editor=settings.edit();
putString(微调器_值,);
commit();
当然,这取决于您的需求,ymmv。公共类Akshay扩展活动{
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();
}
}
};
}
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.configuration);
//选择房间的微调器
微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调器微调;
ArrayAdapter_room=ArrayAdapter.createFromResource(此,
R.array.room\u array,android.R.layout.simple\u微调器\u项);
adapter\u room.SetDropDownViewResource(android.R.layout.simple\u微调器\u下拉项);
微调器室。设置适配器(适配器室);
spinner_room.setOnItemSelectedListener(选择_room_spinner()的新侦听器);
}
//用于选择房间的微调器的侦听器实现
公共静态类侦听器\u的\u选择\u房间\u微调器实现OnItemSelectedListener
{
静态字符串类型;
已选择公共位置(AdapterView父项、视图、整数位置、长id)
{
//通过使用此选项,您可以获得所需项目的位置
//已从下拉列表中选择
RoomType=(parent.getItemAtPosition(pos)).toString();
}
未选择公共无效(AdapterView父级)
{
//什么也不做。
}
};
//保存板数的侦听器实现
私有OnClickListener btnListener\u Btn\u Save\u Room\u Board=new OnClickListener()
{
公共void onClick(视图)
{
DBAdapter dbAdapter1=新的DBAdapter(view.getContext());
弦乐室;
试一试{
dbAdapter1.createDataBase();
dbAdapter1.openDataBase();
//这里我使用的是我从
//斯宾纳的听众
room=侦听器,用于选择room\u微调器.RoomType;
ContentValues initialValues1=新的ContentValues();
初始值1.放置(“房间类型”,房间);
//在这里,我将它(RoomType)存储到数据库中
dbAdapter1.InsertNumberOfBoardInDB(“配置”,null,初始值1);
}
捕获(例外e){
}
最后{
dbAdapter1.close();
}
}
};
}
感谢您的重播,但基本上我已经完成了记事本教程,但我想在单击按钮时将值存储在微调器中到数据库中。到目前为止,您得到了什么?你到底被困在哪里?你试过什么没用的?你期望它做什么?它看起来做了什么呢?嗨,程序员布鲁斯,我已经粘贴了我的代码。现在我想做的是,我必须将spinner的值存储到数据库中。(想想你博客上的例子,我必须将“Earth”存储到数据库中的tabel中。)我想知道该怎么做。Plz ReplayTank用于replay。我想知道当用户从spinner中选择Perticular值时,当用户单击save按钮时,该值应保存到数据库中。您的一些代码似乎丢失了(如Akshay类中?onCreate?函数定义的函数头。。。