我在android中创建了一个关系数据库应用程序,它根据我用来填充微调器的选定ID显示发票。微调器正确填充,但我需要获取包含SQL的viewInvoice
方法的参数的id的整数值(不是微调器的索引)。我尝试过使用getSelectedItem
方法,但这似乎并不能满足我的需求。
以下是微调器的代码:
package com.example.dh0020348.inventory;
import android.app.AlertDialog;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class ViewAllCustomers extends AppCompatActivity {
DbHelper db;
TextView id, name, email, address, city, state, zip, phone, invoiceID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_all_customers);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
db = new DbHelper(this);
final Spinner spinner = (Spinner) findViewById(R.id.spinnerInvoiceID);
DbBackend dbBackend = new DbBackend(ViewAllCustomers.this);
String[] spinnerLists = dbBackend.getAllSpinnerContent();
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(ViewAllCustomers.this,android.R.layout.simple_spinner_item, spinnerLists);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
double total;
Cursor res = db.viewInvoice(Integer.parseInt(String.valueOf(spinner.getSelectedItem())));
if (res.getCount() == 0) {
// show message
showMessage("Error", "Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
buffer.append("Invoice ID: " + res.getString(0) + "\n");
buffer.append("Date: " + res.getString(1) + "\n");
buffer.append("Name: " + res.getString(2) + "\n");
buffer.append("Street Address: " + res.getString(8) + "\n");
buffer.append("City: " + res.getString(9) + "\n");
buffer.append("State: " + res.getString(10) + "\n");
buffer.append("Zip Code: " + res.getString(11) + "\n");
buffer.append("Phone: " + res.getString(12) + "\n");
buffer.append("Item: " + res.getString(4) + "\n");
buffer.append("Size: " + res.getString(5) + "\n");
buffer.append(String.format("Price Per Item: %.2f", Double.parseDouble(res.getString(7))) + "\n");
buffer.append("Quantity: " + res.getString(6) + "\n");
total = Double.parseDouble(res.getString(7)) * Double.parseDouble(res.getString(6));
buffer.append(String.format("Total Price: %.2f", total));
// show all data
showMessage("Data", buffer.toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
id = (TextView) findViewById(R.id.lblCustomerID);
name = (TextView) findViewById(R.id.lblCustomerName);
email = (TextView) findViewById(R.id.lblCustomerEmail);
address = (TextView) findViewById(R.id.lblCustomerAddress);
city = (TextView) findViewById(R.id.lblCity);
state = (TextView) findViewById(R.id.lblState);
zip = (TextView) findViewById(R.id.lblZip);
phone = (TextView) findViewById(R.id.lblPhone);
invoiceID = (TextView) findViewById(R.id.lblInvoice);
ViewAllCustomers();
// ViewCustomerInvoices();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
//display the StringBuffer message
public void showMessage(String title, String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
public void ViewAllCustomers(){
Cursor res = db.viewAllCustomers();
while (res.moveToNext()) {
id.setText("ID: " + res.getString(0));
name.setText("Name: " + res.getString(1));
email.setText("Email: " + res.getString(2));
address.setText("Address: " + res.getString(3));
city.setText("City: " + res.getString(4));
state.setText("State: " + res.getString(5));
zip.setText("Zip: " + res.getString(6));
phone.setText("Phone: " + res.getString(7));
invoiceID.setText("Invoice ID:");
}
}
}
以下是DbHelper
类中查询的方法:
//view a single saved invoice
public Cursor viewInvoice(int id){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT ord." + INVOICE_ID + ", " + INVOICE_DATE + ", " + C_CUSTOMER_NAME + ", cus."
+ C_CUSTOMER_ID + ", inventory." + INVENTORY_ITEM + ", " + ORDERED_SIZE + ", " + ORDERED_QUANTITY + ", "
+ INVENTORY_PRICE + ", " + C_STREET_ADDRESS + ", " + C_CITY + ", " + C_STATE +
", " + C_ZIP + ", " + C_PHONE +
" FROM " + TABLE_INVOICE + " as inv INNER JOIN " + TABLE_CUSTOMER + " as cus " +
"ON inv." + C_CUSTOMER_ID + " = " + "cus." + C_CUSTOMER_ID +
" INNER JOIN " + TABLE_ORDERED + " as ord ON inv." + INVOICE_ID + " = " + "ord." + ORDERED_ID +
" INNER JOIN " + TABLE_INVENTORY + " as inventory " +
"ON ord." + INVENTORY_ID + " = " + " inventory." + INVENTORY_ID, null);
if(res != null)
res.moveToFirst();
return res;
}
答案 0 :(得分:2)
然后将其投射到您填充它的任何对象。
答案 1 :(得分:0)
使用spinnerAdapter.getSelectedItemId()
,如果未选择任何内容,则会返回当前所选的商品ID或INVALID_ROW_ID
( - 9223372036854775808)。
答案 2 :(得分:0)
你可以用两种方式做到这一点,
1]使用微调项目位置,
int myNum = 0;
try {
myNum = Integer.parseInt(Spinner_NAME.get(position).toString());
} catch(NumberFormatException nfe) {
Log.e("","Exception :: "+nfe);
}
2]如果您正在使用任何模型类或任何arraylist,那么
int myNum = Integer.parseInt(spinner1.getModel().getValue().toString());