如何从sqlite数据库中填充spinner中的选定项

时间:2015-06-30 08:11:54

标签: android sqlite

我已经编写了一个程序来从sqlite数据库中获取一些值并在spinner里面填充,这是完美的工作,但我想将所选的项目从微调器显示为一个不同的活动,我得到的只是一个空值或android.database.sqlite.SQLiteCursor@3456046c,那么如何将其转换为字符串??

需要一些帮助

int[] to = new int[] { android.R.id.text1 };
    String[] from = new String[] { DbListHelper.ACCOUNT_TYPE };
    final SimpleCursorAdapter adapter4 = new SimpleCursorAdapter(
            getBaseContext(), android.R.layout.simple_list_item_1, cursor,
            from, to) {
        public View getView(int position, View convertView, ViewGroup  
            parent) {
            View v = super.getView(position, convertView, parent);
            ((TextView) v).setTextSize(18);
            ((TextView) v).setGravity(Gravity.CENTER);
            ((TextView) v).setTextColor(Color.parseColor("#1C689C"));
            return v;
        }

    };

    adapter4.setDropDownViewResource
    (android.R.layout.simple_spinner_dropdown_item);
    spinnerType.setAdapter(adapter4);

在按钮上单击以保存我正在执行此操作的记录,acName是我正在插入的微调器变量,我还尝试将一个虚拟值放在acName中作为acName =" 1" ,这是正确显示。

    btnPrint.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        if(cursor.moveToFirst()){
                            do {
                                acName = 
    cursor.getString(cursor.getColumnIndex(DbListHelper.ACCOUNT_NAME));
                            } while (cursor.moveToNext());

                        }
                        vehicleNo1 = editVehicleNo1.getText().toString();

                        driverName = editDriverName.getText().toString();
                        driverCode = editDriverCode.getText().toString();
                        dieselRate = editDieselRate.getText().toString();
                        dieselQty = editDieselQty.getText().toString();
                        amount = editAmount.getText().toString();
                        referenceNo = editReferenceNo.getText().toString();
                        noOfCopies = editNoOfCopies.getText().toString();
                        remark = editRemark.getText().toString();
                        transactionID = imeiCode + "" + dateTime;

                        if (spinnerData.equals("") || vehicleNo1.equals("")
                                || spinnerAccountName.equals("")
                                || spinnerFuelRate.equals("")
                                || driverName.equals("")
                                || dieselQty.equals("")
                                || dieselRate.equals("")
                                || amount.equals("")
                                || referenceNo.equals("")) {
                            Toast.makeText(getBaseContext(),
                                    "Please fill all the fields",
                                    Toast.LENGTH_LONG).show();
                        }
                        else if (noOfCopies.equals("")) {
                            Toast.makeText(getBaseContext(),
                                    "Please enter any value",
                                    Toast.LENGTH_LONG).show();
                        } else {
                            long id = listHelper.insertPumpData(
                                    acName, vehicleNo1, 
                                    driverName,driverCode,
                                    dieselRate, dieselQty, amount + " Rs",
                                    referenceNo, noOfCopies, date, time,
                                    transactionID);


                        }

                    }
                });

谢谢

1 个答案:

答案 0 :(得分:0)

好的,我自己得到了上述问题的答案

我这样做是为了获取所选微调器项的值

Cursor acNameCur=(Cursor)spinnerAccountName.getSelectedItem();
String accountNamecol=acNameCur.getString(acNameCur.getColumnIndex
(DbListHelper.ACCOUNT_NAME));