0

我想获取从 Sqlite 派生的微调器中选择的值的 id。

我在 Sqlite 中的表是

CITY_ID CITY_NAME

8 孟买
9 新德里
10 马德拉斯
11 加尔各答
12 班加罗尔
13 艾哈迈达巴德
14 斋浦尔
15 昌迪加尔
16 西姆拉
17 勒克瑙
18 巴特那
19 博帕尔
20那普勒

我的数据库连接器代码是

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

}

现在通过在微调器中选择城市。我想从 SQlite 表中获取 id。

有人可以指导我实现这一目标。

提前致谢。

问候, 迪内什

4

2 回答 2

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

设置适配器

    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) {

        }
    });

微调适配器

 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;

    }
}

设置器和获取器

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;
}
}
于 2013-08-06T05:48:24.207 回答
0

您正在设置city namespinner值。您将获得它的位置,因为,您可以为ascity id设置值,然后您可以获得它的位置和值,.. 或者您可以使用e 并且对于您获得的位置,您可以映射它的值。spinnercity_idcity_namcity_id

假设0您将获得bombay的位置,那么位置 0设置city_id8。像那样。

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

        }

同样,该代码未优化。

于 2013-08-06T05:47:58.727 回答