gpt4 book ai didi

java - 如何检索微调器中所选项目的 int 值?

转载 作者:搜寻专家 更新时间:2023-11-01 09:50:21 25 4
gpt4 key购买 nike

我正在为 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;
}

最佳答案

你试过了吗spinner.getSelectedItem().toString();

然后将其转换为您填充它的任何对象。

关于java - 如何检索微调器中所选项目的 int 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36453532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com