如何在 Android Studio 中使用微调器实现 SQLite 加入,以便从表中获取 id

我在SQLite数据库中创建了2个表,并且stoksales


at DatabaseHelper.java


创建表stok


String tbStok = "CREATE TABLE stok(id_stok INTEGER PRIMARY KEY AUTOINCREMENT, waktu_stok DATETIME, id_sales INTEGER, stok INTEGER, FOREIGN KEY id_sales REFERENCES sales(id_sales)";

创建表sales


String tbSales = "CREATE TABLE sales(id_sales INTEGER PRIMARY KEY AUTOINCREMENT, nama VARCHAR, kodesales VARCHAR, username VARCHAR, password VARCHAR, level INTEGER)";

从 SQLite 创建列表数据


public List<String> getSpinnerSales(){

    List<String> labels = new ArrayList<String>();


    // Select All Query

    String selectQuery = "SELECT  * FROM " + "sales";


    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.rawQuery(selectQuery, null);


    // looping through all rows and adding to list

    if (cursor.moveToFirst()) {

        do {

            labels.add(cursor.getString(0));

            labels.add(cursor.getString(1));

        } while (cursor.moveToNext());

    }


    // closing connection

    cursor.close();

    db.close();


    // returning lables

    return labels;

}

在微调器上显示


private void loadSpinnerSales() {

    // database handler

    DatabaseHelper db = new DatabaseHelper(getApplicationContext());


    // Spinner Drop down elements

    List<String> lables = db.getSpinnerSales();


    // Creating adapter for spinner

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,

            android.R.layout.simple_spinner_item, lables);


    // Drop down layout style - list view with radio button

    dataAdapter

            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);


    // attaching data adapter to spinner

    spsales.setAdapter(dataAdapter);

}

http://img.mukewang.com/62fca9ea0001a93005770202.jpg

我需要将值保存到其他表 SQLite,但微调器必须显示销售名称。id_sales



慕桂英546537
浏览 189回答 3
3回答

手掌心

最好的方法是使用自定义适配器,然后您可以将对象列表(在您的情况下是“销售ID和名称列表”)传递给适配器而不仅仅是字符串列表,然后在选择的项目上,您将获得所选对象,然后您可以获得所需的任何id或名称,您可以按照此链接使自定义适配器&nbsp;https://abhiandroid.com/ui/custom-spinner-examples.html

明月笑刀无情

我通过编辑代码来解决这个问题// looping through all rows and adding to list&nbsp; &nbsp; &nbsp; &nbsp; if (cursor.moveToFirst()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; labels.add(cursor.getString(0)+cursor.getString(1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } while (cursor.moveToNext());&nbsp; &nbsp; &nbsp; &nbsp; }并在项目上的微调器上选择使用子字符串进行操作spsupir.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String label = parent.getItemAtPosition(position).toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String ids = String.valueOf(label).substring(0,1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("label:", ids);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onNothingSelected(AdapterView<?> adapterView) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });已编辑的代码String label = parent.getItemAtPosition(position).toString();String ids = String.valueOf(label).substring(0,1);Log.d("label:", ids);

翻阅古今

另一个解决问题步骤 1 创建模型类public class Sales {&nbsp; &nbsp; String id_sales,nama;&nbsp; &nbsp; public Sales(String id_sales, String nama) {&nbsp; &nbsp; &nbsp; &nbsp; this.id_sales = id_sales;&nbsp; &nbsp; &nbsp; &nbsp; this.nama = nama;&nbsp; &nbsp; }&nbsp; &nbsp; public String getId_sales() {&nbsp; &nbsp; &nbsp; &nbsp; return id_sales;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId_sales(String id_sales) {&nbsp; &nbsp; &nbsp; &nbsp; this.id_sales = id_sales;&nbsp; &nbsp; }&nbsp; &nbsp; public String getNama() {&nbsp; &nbsp; &nbsp; &nbsp; return nama;&nbsp; &nbsp; }&nbsp; &nbsp; public void setNama(String nama) {&nbsp; &nbsp; &nbsp; &nbsp; this.nama = nama;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return nama;&nbsp; &nbsp; }}步骤 2 从 SQLite 表销售中放入所有数据//Data spinner supir&nbsp; &nbsp; public ArrayList<Sales> getSpinnerSales(){&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Sales> salesList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; // Select All Query&nbsp; &nbsp; &nbsp; &nbsp; String selectQuery = "SELECT&nbsp; * FROM " + "sales";&nbsp; &nbsp; &nbsp; &nbsp; SQLiteDatabase db = this.getReadableDatabase();&nbsp; &nbsp; &nbsp; &nbsp; Cursor cursor = db.rawQuery(selectQuery, null);&nbsp; &nbsp; &nbsp; &nbsp; // looping through all rows and adding to list&nbsp; &nbsp; &nbsp; &nbsp; if (cursor.moveToFirst()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; salesList.add(new Sales(cursor.getString(0), cursor.getString(1)));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } while (cursor.moveToNext());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // closing connection&nbsp; &nbsp; &nbsp; &nbsp; cursor.close();&nbsp; &nbsp; &nbsp; &nbsp; db.close();&nbsp; &nbsp; &nbsp; &nbsp; // returning lables&nbsp; &nbsp; &nbsp; &nbsp; return salesList;&nbsp; &nbsp; }步骤 3 生成 void 以使用 sqlite aa 活动中的数据private void loadSpinnerSales() {&nbsp; &nbsp; &nbsp; &nbsp; // database handler&nbsp; &nbsp; &nbsp; &nbsp; DatabaseHelper db = new DatabaseHelper(getApplicationContext());&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Sales> salesList = db.getSpinnerSales();&nbsp; &nbsp; &nbsp; &nbsp; ArrayAdapter<Sales> adapter = new ArrayAdapter<Sales>(this, android.R.layout.simple_spinner_dropdown_item, salesList);&nbsp; &nbsp; &nbsp; &nbsp; spsales.setAdapter(adapter);&nbsp; &nbsp; }步骤 4 在 OnCreate 上加载方法&nbsp; &nbsp; spsales = (Spinner) findViewById(R.id.spSales);&nbsp; &nbsp; loadSpinnerSales();&nbsp; &nbsp; spsales.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sales sales = (Sales) parent.getSelectedItem();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getApplicationContext(),sales.getId_sales(),Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onNothingSelected(AdapterView<?> parent) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java