【问题标题】:Spinner with custom cursor adapter and "empty" selection带有自定义光标适配器和“空”选择的微调器
【发布时间】:2012-09-06 02:24:35
【问题描述】:

我有光标从我的 CarProfile 表中选择记录并使用自定义适配器显示 微调器选择中的品牌、型号和线板。我的问题是我想为第一个项目选择“全部”,所以如果选择我可以显示所有汽车的信息。我的代码是:

            // spinner 1
        mDbAdapter.open();
        Cursor cursorCP = mDbAdapter.fetchAllProfiles();
        startManagingCursor(cursorCP);
        mDbAdapter.close();

        MyCustomAdapter ad = new MyCustomAdapter(this, cursorCP);
        spinCP.setAdapter(ad);

    public class MyCustomAdapter extends CursorAdapter {

    public MyCustomAdapter(Context context, Cursor c) {
        super(context, c);

    }
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView nameTextView = (TextView) view
                .findViewById(android.R.id.text1);

        String brand = cursor.getString(cursor
                .getColumnIndex(DefaxedDbAdapter.KEY_BRAND));
        String model = cursor.getString(cursor
                .getColumnIndex(DefaxedDbAdapter.KEY_MODEL));
        String licence = cursor.getString(cursor
                .getColumnIndex(DefaxedDbAdapter.KEY_LICENCEPLATE));
        nameTextView.setText(brand + "-" + model + "-" + licence);
    }
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = View.inflate(context,
                android.R.layout.simple_spinner_dropdown_item, null);
        return view;

    }
}

【问题讨论】:

    标签: cursor spinner adapter


    【解决方案1】:

    好的,我最后的解决方法是在 hashmap 的帮助下使用数组适配器来存储 id。这是我使用的代码

            ArrayList<String> arr = new ArrayList<String>();
        arr.add(DefaxedDbAdapter.ALL_LABEL);
    
        hm = new HashMap<String, Integer>();
    
        while (!cursorProfiles.isAfterLast()) {
            int rowId = cursorProfiles.getInt(cursorProfiles
                    .getColumnIndex(DefaxedDbAdapter.KEY_ROWID));
            String brand = cursorProfiles.getString(cursorProfiles
                    .getColumnIndex(DefaxedDbAdapter.KEY_BRAND));
            String model = cursorProfiles.getString(cursorProfiles
                    .getColumnIndex(DefaxedDbAdapter.KEY_MODEL));
            String licencePlate = cursorProfiles.getString(cursorProfiles
                    .getColumnIndex(DefaxedDbAdapter.KEY_LICENCEPLATE));
            arr.add(brand + "-" + model + "-" + licencePlate);
            hm.put(brand + "-" + model + "-" + licencePlate, rowId);
            cursorProfiles.moveToNext();
        }
    
        ArrayAdapter<String> adapterProfiles = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_dropdown_item, arr);
        profilesSpinner.setAdapter(adapterProfiles);
    

    然后从微调器中选择一个项目后,您可以获得这样的 id

                    if (profilesSpinner.getSelectedItemId() != 0) {
    
                    String tmp = profilesSpinner.getSelectedItem().toString();
                    carId = hm.get(tmp);
    
                    if (alreadySet) {
                        SQLquery += " AND expenses.carId =" + carId;
                    } else {
                        SQLquery += " WHERE expenses.carId =" + carId;
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多