我试图将微调器数据存储到SQLite数据库中

时间:2014-04-10 10:24:42

标签: java android

我正在尝试将微调器数据存储到SQLite database但是在SQLite数据库单元中android.widget.Spinner@411cf168 vale come 即值不从微调器列表中获取。请任何人帮助我,我认为我的问题就在这一行

ad1.insertquery(DatabaseDetail.getPAYMENT(), new String[]{ed_name.getText().toString(),ed_amount.getText().toString(),spin1.toString(),card_number.getText().toString(),ex_date.getText().toString()});

    public class Payment_confirmation extends Activity implements OnItemSelectedListener 
    {
        EditText ed_name,ed_amount,card_number,ex_date;
        Button payment;
        Spinner spin;
        String[] accounttype = { "SBI MASTER CARD","SBI MAESTRO CARD","HDFC DEBIT/CREDIT CARD","BOB CREDIT CARD","INDIAN BANK CREDIT CARD"};
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.payment_confirmation);
            ed_name=(EditText)findViewById(R.id.name);
            ed_amount=(EditText)findViewById(R.id.amount);
            card_number=(EditText)findViewById(R.id.crdnumber);
            ex_date=(EditText)findViewById(R.id.date);

            payment=(Button)findViewById(R.id.button2);
            back = (Button)findViewById(R.id.button1);
            spin=(Spinner)findViewById(R.id.spinner1);
            spin.setOnItemSelectedListener(this);

            ArrayAdapter aa= new ArrayAdapter(this,android.R.layout.simple_spinner_item, accounttype);
            aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spin.setAdapter(aa);

            payment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name=ed_name.getText().toString();
                String amount=ed_amount.getText().toString();
                String card=card_number.getText().toString();
                String date=ex_date.getText().toString();               String spin1= spin.toString();

                AdapterClass ad1 = new AdapterClass(getApplicationContext(), DatabaseDetail.PAYMENT);
                    ad1.Open();
                    ad1.insertquery(DatabaseDetail.getPAYMENT(), new String[]{ed_name.getText().toString(),ed_amount.getText().toString(),spin1.toString(),card_number.getText().toString(),ex_date.getText().toString()});
                    Cursor lCursor1 = ad1.fetchRecords(new String[]{"CUS_NAME","AMOUNT","CARD_NAME","CARD_NUMBER","EX_DATE"},null);
                startManagingCursor(lCursor1);
                lCursor1.moveToFirst();
                if(lCursor1.getCount()!=0)
                {
                    Intent i = new Intent(getApplicationContext(),Thankpayment.class);
                    startActivity(i);
                    }

                ad1.close();
                finish();

                }
          });   
        }
    }

3 个答案:

答案 0 :(得分:2)

尝试这种方式:

Spinner spinner1 = (Spinner) findViewById(R.id.spin1);
 ArrayAdapter aa= new ArrayAdapter(this,android.R.layout.simple_spinner_item, accounttype);

 aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 spinner1.setAdapter(aa);

spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
        Object item = parent.getItemAtPosition(pos);
        String spin_value = item.toString();

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});

答案 1 :(得分:1)

替换

spin1.toString() 

---因为它会将微调器对象引用作为字符串返回,就像你得到android.widget.Spinner@411cf168

一样

spin1.getSelectedItem().toString() -- As it will return the spinner object's content i.e selected item as string..

答案 2 :(得分:0)

执行此操作时:

public class SpinnerActivity extends Activity implements OnItemSelectedListener

你真的不必写

spin.setAdapter(aa);

只需覆盖班级中的以下方法

 public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}