Android 如何将微调器的选定项保存(以及以后检索)到SQLite
好吧。。。我受够了 我非常沮丧 所以我宁愿寻求帮助,而不是一个新的显示器 …这些在这里很贵 长话短说。。。我有一个数据库。还有一张桌子Android 如何将微调器的选定项保存(以及以后检索)到SQLite,android,sqlite,spinner,Android,Sqlite,Spinner,好吧。。。我受够了 我非常沮丧 所以我宁愿寻求帮助,而不是一个新的显示器 …这些在这里很贵 长话短说。。。我有一个数据库。还有一张桌子 private String DEFINE_PROP_TYPES = "CREATE TABLE " + TABLE_PROP_TYPES + "(" + TABLE_ID + " INTEGER PRIMARY KEY, " + TABLE_PROP_TYPE_NAME + " TEXT NOT NULL" +
private String DEFINE_PROP_TYPES = "CREATE TABLE " + TABLE_PROP_TYPES + "("
+ TABLE_ID + " INTEGER PRIMARY KEY, "
+ TABLE_PROP_TYPE_NAME + " TEXT NOT NULL"
+ ")";
使用一个“适配器”类来管理它
public abstract class DBAdapter
{
static public final String C_COLUMN_ID = "_id";
protected Context context;
protected DBHelper dbHelper;
protected SQLiteDatabase db;
protected String managedTable;
protected String[] columns;
public String getTableManaged()
{
return managedTable;
}
public void setTableManaged(String managedTable)
{
this.managedTable = managedTable;
}
public void setColumns(String[] columns)
{
this.columns = columns;
}
public DBAdapter(Context context)
{
this.context = context;
}
public void close()
{
dbHelper.close();
}
public DBAdapter open() throws SQLException
{
dbHelper = new DBHelper(context);
db = dbHelper.getWritableDatabase();
return this;
}
public Cursor getList()
{
Cursor c = db.query(true, managedTable, columns, null, null, null, null, null, null);
return c;
}
public long insert(ContentValues reg)
{
return 0;
}
}
public class PropTypesDBAdapter extends DBAdapter
{
static public final String C_TABLE_PROP_TYPES = "PROP_TYPES";
static public final String C_COLUMN_ID = "_id",
C_COLUMN_PROP_TYPES_NAME = "re_prop_type";
public PropTypesDBAdapter(Context context)
{
super(context);
this.setTableManaged(C_TABLE_PROP_TYPES);
this.setColumns(new String[] { C_COLUMN_ID,
C_COLUMN_PROP_TYPES_NAME });
}
public long insert(ContentValues reg)
{
if (db == null)
{
open();
}
return db.insert(C_TABLE_PROP_TYPES, null, reg);
}
}
除了这堆可爱的东西,我还有一个活动课
用纺纱机
public class PropDetailActivity extends Activity implements LocationListener
{
// insert here some blah-blah constants not needed by spinners
private PropDBAdapter mHouses;
private RatingsDBAdapter mRatings;
private PropTypesDBAdapter mPropTypes;
private Cursor mCursorHouses,
mCursorRatings,
mCursorPropTypes;
long mPropType;
private long mPropId;
private Spinner spinnerRating, spinnerType;
AdapterView.OnItemSelectedListener spnLstPropType, spnLstRating;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_house_detail);
Intent intent = getIntent();
Bundle extra = intent.getExtras();
if (extra == null)
{
return;
}
// Figure all view widgets being retrieved here, including...
spinnerRating = (Spinner) findViewById(R.id.spinnerRating);
spinnerType = (Spinner) findViewById(R.id.spinnerType);
// Create adapter and cursor-y things here
mHouses = new PropDBAdapter(this);
mHouses.open();
// And now, for the juicy, deliciously irritating stuff:
String[] from = new String[] { PropTypesDBAdapter.C_COLUMN_PROP_TYPES_NAME };
int[] to = new int[] { android.R.id.text1 };
mPropTypes = new PropTypesDBAdapter(this);
mPropTypes.open();
mCursorPropTypes = mPropTypes.getList();
@SuppressWarnings("deprecation")
SimpleCursorAdapter adapterPropTypes = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
mCursorPropTypes,
from, /*new String[] { RatingsDBAdapter.C_COLUMN_RATING_NAME }, */
to); /*new int[] { android.R.id.text1 } */
adapterPropTypes.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerType.setAdapter(adapterPropTypes);
spinnerRating.setSelection(pos);
spnLstPropType = new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id)
{
mPropType = id;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) { }
};
spinnerType.setOnItemSelectedListener(spnLstPropType);
private int getItemPositionById(Cursor c, long id, DBAdapter adapter)
{
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
if (c.getLong(c.getColumnIndex(DBAdapter.C_COLUMN_ID)) == id)
{
return c.getPosition();
}
}
return 0;
}
private void query(long id)
{
mCursorHouses = mHouses.getRecord(id);
// Figure values being retrieved and set on their widgets instead of this comment... and now...
mPropType = mCursorHouses.getInt(mCursorHouses.getColumnIndex(PropDBAdapter.C_PROP_TYPE_ID));
spinnerType.setSelection(
getItemPositionById(
mCursorRatings,
mCursorHouses.getColumnIndex(PropDBAdapter.C_PROP_TYPE_ID),
mPropTypes
)
);
private void save()
{
ContentValues reg = new ContentValues();
// Read: values being put into 'reg'... eventually it should reach this:
reg.put(PropDBAdapter.C_PROP_TYPE_ID, mPropType);
try
{
if (mFormMode == PropListActivity.C_CREATE)
{
mHouses.insert(reg);
Toast.makeText(PropDetailActivity.this, R.string.house_create_notice, Toast.LENGTH_LONG).show();
}
else if (mFormMode == PropListActivity.C_EDIT)
{
Toast.makeText(PropDetailActivity.this, R.string.house_edit_notice, Toast.LENGTH_LONG).show();
reg.put(PropDBAdapter.C_COLUMN_ID, mPropId);
long resultCode = mHouses.update(reg);
Log.i(this.getClass().toString(), "Database operation result code: " + resultCode);
}
}
catch(SQLException e)
{
Log.i(this.getClass().toString(), e.getMessage());
}
setResult(RESULT_OK);
finish();
}
}
公共类PropDetailActivity扩展活动实现LocationListener
{
//在这里插入一些微调器不需要的无聊常数
私人住房;
私人评级;私人评级;
私有proptypesdabadapter mPropTypes;
私人麦克斯豪斯酒店,
麦库索拉廷,
mCursorPropTypes;
长型;
私人长腿;
私人纺纱机,纺纱机类型;
AdapterView.OnItemSelectedListener SPNLSTropType,spnLstRating;
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(右布局、活动和房屋详图);
Intent=getIntent();
Bundle extra=intent.getExtras();
如果(额外==null)
{
返回;
}
//图:此处检索的所有视图小部件,包括。。。
喷丝头=(喷丝头)findViewById(R.id.喷丝头);
喷丝头类型=(喷丝头)findViewById(R.id.spinnerType);
//在这里创建适配器和游标y
mhouse=新的PropdAdapter(本);
mhouse.open();
//现在,对于那些多汁、美味、刺激的东西:
String[]from=新字符串[]{PropTypesDBAdapter.C_COLUMN_PROP_TYPES_NAME};
int[]to=newint[]{android.R.id.text1};
mPropTypes=新的proptypesdabadapter(此);
mPropTypes.open();
mCursorPropTypes=mPropTypes.getList();
@抑制警告(“弃用”)
SimpleCursorAdapter Adapter PropTypes=新的SimpleCursorAdapter(此,
android.R.layout.simple\u微调器\u项,
mCursorPropTypes,
from,/*新字符串[]{RatingsDBAdapter.C_COLUMN_RATING_NAME}*/
to);/*新int[]{android.R.id.text1}*/
adapterPropTypes.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
喷丝头类型。设置适配器(适配器类型);
喷丝头设置选择(pos);
spnLstPropType=new AdapterView.OnItemSelectedListener()
{
@凌驾
已选择公共视图(AdapterView父视图、视图、,
内部位置,长id)
{
mPropType=id;
}
@凌驾
未选择的公共无效(AdapterView arg0){}
};
喷丝头类型。SetonimSelectedListener(spnLstPropType);
私有int getItemPositionById(游标c、长id、DBAdapter适配器)
{
for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
{
if(c.getLong(c.getColumnIndex(DBAdapter.c_COLUMN_ID))==ID)
{
返回c.getPosition();
}
}
返回0;
}
专用无效查询(长id)
{
mCursorHouses=mHouses.getRecord(id);
//图:在他们的小部件上检索和设置的值,而不是此注释…现在。。。
mPropType=mCursorHouses.getInt(mCursorHouses.getColumnIndex(PropDBAdapter.C_PROP_TYPE_ID));
喷丝头类型(
getItemPositionById(
麦库索拉廷,
mcursorhouse.getColumnIndex(PropDBAdapter.C_PROP_TYPE_ID),
mPropTypes
)
);
私有void save()
{
ContentValues reg=新的ContentValues();
//读:值被放入“reg”…最终它应该达到:
注册put(PropDBAdapter.C_PROP_TYPE_ID,mPropType);
尝试
{
if(mFormMode==PropListActivity.C_CREATE)
{
mhouse.插入(注册号);
Toast.makeText(PropDetailActivity.this,R.string.house\u create\u notice,Toast.LENGTH\u LONG).show();
}
else if(mFormMode==PropListActivity.C_EDIT)
{
Toast.makeText(PropDetailActivity.this,R.string.house_edit_notice,Toast.LENGTH_LONG).show();
注册put(PropDBAdapter.C_COLUMN_ID,mPropId);
long resultCode=mhouse.update(reg);
Log.i(this.getClass().toString(),“数据库操作结果代码:”+resultCode);
}
}
捕获(SQLE异常)
{
Log.i(this.getClass().toString(),e.getMessage());
}
设置结果(结果\正常);
完成();
}
}
纺纱工都是坏孩子,最重要的是懒惰的坏孩子
他们确实加载了要显示的数据——房地产类型列表
就是说,打了几下屁股之后
但是,希望他们将您选择的值保存到SQLite?并在从数据库取回内容时显示确切的值
哦,不,不可能,怎么可能
它们固执地坚持在活动启动时始终显示相同的值
所以…请…我必须利用你们的集体智慧来为一个项目保留我的遗憾借口
请释放?:)
(如果您想深入了解整个未剪切的代码,这里有一个GIT存储库:)一些事情:
(一)我真的看不到在上面的任何地方您实际创建了一个SQLite
数据库,或者使用SQLiteOpenHelper
类来访问该数据。请看一下。它使用一个简单的单表设置来存储数据。一旦创建了数据库,就应该很容易从中读取和写入数据。请验证您是否确实有一个数据库创建埃德
(2) ar在哪里
spinnerRating.setSelection(
getItemPositionById(
mCursorRatings,
mCursorHouses.getInt(mCursorHouses.getColumnIndex(PropDBAdapter.C_PROP_RATING_ID)),
mRatings
)
);
spinnerType.setSelection(
getItemPositionById(
mCursorPropTypes,
mCursorHouses.getInt(mCursorHouses.getColumnIndex(PropDBAdapter.C_PROP_TYPE_ID)),
mPropTypes
)
);