我有一个表设施,其中包含字段FacilityID和字段FacilityName。我有一个spinner加载数据给SQLite。
private void loadSpinnerDataHama() {
// database handler
DatabaseSpinner db = new DatabaseSpinner(getApplicationContext());
// Spinner Drop down elements
List<String> lables = db.getAllLabels();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spin2.setAdapter(dataAdapter);
}
public List<String> getAllLabels(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + Facibilities;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
我有一个公司表格供用户输入公司信息,例如:公司ID,公司名称,地址,电话,设施......以及“创建”和“创建”。按钮。在Facility字段中,我使用微调器显示FacilityName,当用户单击Create按钮时,我想将FacilityID保存到表Company
中答案 0 :(得分:0)
试试这个
1)声明一个公共String变量&#39; id&#39;存储选定的ID;
2)将id保存在单独的arraylist中。
3)在spinner内部.OnitemSelectedListener-&gt;
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
id=ids.get(position);
}
});
3)在创建按钮内部onclick方法传递名为id的变量。
答案 1 :(得分:0)
要管理这样的数据集,您可以做的是:
Faculty
课程。ArrayList<Faculty>
。答案 2 :(得分:0)
您必须创建新的类,例如带有两个类似id和标签的数据,以及它的getter和setter。
class Data {
private String id;
private String label;
//create getter and setter for above properties.
}
然后在getAllLabels方法中进行一些更改,例如:
public List<Data> getAllLabels(){
List<Data> lstData = new ArrayList<Data>();
// Select All Query
String selectQuery = "SELECT * FROM " + Facibilities;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
Data data = new Data();
do {
data .setId(add(cursor.getString(1)))
data .setLabel(add(cursor.getString(2)));
lstData.add(data);
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning data object with id and labels
return lstData;}
然后从返回的列表中创建一个String标签列表和id:
List <String> labels= new ArrayList<String>();
List <String> ids= new ArrayList<String>();
foreach(data in lstData){
ids.add(data.getId());
labels.add(data.getLabel());
}
然后将标签列表传递给适配器和适配器项的onSelect,您将从其位置获取其ID:
spin2.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected( AdapterView<?> parent, View view, int pos, long id){
// you will get your id here of selected item and then you can use it //wherever you want
String singleId= id.get(position);
}
public void onNothingSelected( AdapterView<?> parent){
Log.d(TAG,"Nothing selected ");
selectedCurrency=-1;
}
}
);