我想获得在微调器中选择的值的id,其值是从Sqlite派生的。
我在Sqlite中的表是
CITY_ID CITY_NAME
8 BOMBAY
9新德里
10 MADRAS
11 CALCUTTA
12 BANGLORE
13 AHMEDABAD
14 JAIPUR
15 CHANDIGARH
16 SIMLA
17 LUCKNOW
18 PATNA
19 BHOPAL
20 NAGPUR
我的数据库连接器代码是
public List<String> getCity(){
List<String> labels = new ArrayList<String>();
String selectQuery = "SELECT CITY_ID, CITY_NAME FROM city_list";
Log.d("Destination Country Query", selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return labels;
}
我的实施是
city = (Spinner) findViewById(R.id.city);
loadCityData();
private void loadCityData() {
DatabaseConnector db = new DatabaseConnector(getApplicationContext());
List<String> lables = db.getCity();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(dataAdapter);
}
现在通过在微调器中选择City。我想从SQlite表中获取id。
有人可以指导我实现这一目标。
提前致谢。
问候,Dinesh
答案 0 :(得分:1)
为Arraylist增加价值
final ArrayList<Devstatus> statusList = new ArrayList<Devstatus>();
Devstatus tempstatus = new Devstatus();
tempstatus.setDevStatusName("BOMBAY ");
tempstatus.setDevStatusValue("8");
Devstatus tempstatus1 = new Devstatus();
tempstatus1.setDevStatusName(" NEW DELHI ");
tempstatus1.setDevStatusValue("9");
statusList.add(tempstatus);
statusList.add(tempstatus1);
设置Addapter
SpinnerAdapter stateAdaptor = new SpinnerAdapter(SettingActivity.this,statusList);
status.setAdapter(stateAdaptor);
status.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int position, long arg3) {
changeStatus(statusList.get(position));
selected = statusList.get(position).getDevStatusName();
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Spinner Adapter
class SpinnerAdapter extends BaseAdapter {
LayoutInflater mInflater;
ArrayList<Devstatus> statusList;
public SpinnerAdapter(Context context, ArrayList<Devstatus> countryList) {
mInflater = LayoutInflater.from(context);
this.statusList = countryList;
}
public int getCount() {
return this.statusList.size();
}
public Devstatus getItem(int position) {
return (Devstatus) this.statusList.get(position);
}
public long getItemId(int position) {
return (position);
}
public View getView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder;
Devstatus tempState = (Devstatus) this.statusList.get(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.state, null);
holder = new ViewHolder();
holder.adtitle = (TextView) convertView.findViewById(R.id.tvstate);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.adtitle.setText(tempState.getDevStatusName());
return convertView;
}
class ViewHolder {
TextView adtitle;
}
}
Setter和getter
public class Devstatus
{
private String devStatusName;
private String devStatusValue;
public String getDevStatusName()
{
return devStatusName;
}
public void setDevStatusName(String devStatusName)
{
this.devStatusName = devStatusName;
}
public String getDevStatusValue()
{
return devStatusValue;
}
public void setDevStatusValue(String devStatusValue)
{
this.devStatusValue = devStatusValue;
}
}
答案 1 :(得分:0)
您将city name
设置为spinner
值。您将获得其位置,对于city id
,您可以将spinner
的值设置为city_id
,然后您可以获取其位置和值,..
或者您可以使用city_nam
e,并且您获得的位置可以映射其city_id
值。
让位置0
说明您将获得bombay
,然后位置0
设置city_id
为8
。
就像那样。
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.i("cyberwalker", "item selected" + arg2);
if (arg2 == 0) {
city_id=8;
} else if (arg2 == 1) {
city_id=9;
} else if (arg2 == 2) {
city_id=10;
}
......
}
同样,该代码未经过优化。