Android:如何让Spinner在EditText中显示其他sqlite值

时间:2017-06-09 08:48:04

标签: android android-sqlite

你好,我是编程android的新手,我对android studio有疑问..

我有一个包含3列的数据库是“no”,“”namamakanan“和”kalori“。我在表格中输入数据: (不,namamakanan,kalori)价值观('1','nasi uduk','343'),('2','nasi goreng','1046')

我的问题是,当我从微调器中选择“nasi goreng”时,如何从数据库向EditText etpagilaukkcal1显示“kalori”数据?

例如,当我选择“nasi goreng”时,etpagilauk1kcal是“1046”或“nasi uduk”,etpagilauk1kcal是“343”。

谢谢..

import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class BuatJadwalDiet extends Activity implements
        OnItemSelectedListener {

    // Spinner element
    Spinner spinnerpagilauk1, spinnerpagilauk2;
    EditText etpagilauk1kcal;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buat_jadwal_diet);

        // Spinner element
        spinnerpagilauk1 = (Spinner) findViewById(R.id.spinnerpagilauk1);
        spinnerpagilauk2 = (Spinner) findViewById(R.id.spinnerpagilauk2);
        etpagilauk1kcal = (EditText) findViewById(R.id.etpagilauk1kcal);

        // Spinner click listener
        spinnerpagilauk1.setOnItemSelectedListener(this);
        spinnerpagilauk2.setOnItemSelectedListener(this);

        // Loading spinner data from database
        loadSpinnerData();

    }

    private void loadSpinnerData() {

        // database handler
        DataHelperMakanan db = new DataHelperMakanan(getApplicationContext());

        // Spinner Drop down element
        List<String> label = db.getAllNames();

        // Creating adapter for spinner
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, label);

        // Drop down layout style - list view with radio button
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // attaching data adapter to spinner
        spinnerpagilauk1.setAdapter(dataAdapter);
        spinnerpagilauk2.setAdapter(dataAdapter);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {

        // On selecting a spinner item
        String label = parent.getItemAtPosition(position).toString();

        // Showing selected spinner item
        Toast.makeText(parent.getContext(), "Kamu Memilih: " + label,
                Toast.LENGTH_LONG).show()
    }

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

这是我的DataHelper

public class DataHelperMakanan extends SQLiteOpenHelper {

  private static final String DATABASE_NAME = "nutrizimakanan.db";

  private static final int DATABASE_VERSION = 1;
  public DataHelperMakanan(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String makanan = "create table makanan(no integer primary key, namamakanan text null, kalori integer);";
    Log.d("Data", "onCreate: " + makanan);
    db.execSQL(makanan);
    makanan = "INSERT INTO makanan (no, namamakanan, kalori) VALUES ('1', 'Nasi Uduk', '494');";

    db.execSQL(makanan);



  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

  }

  public List<String> getAllNames(){
    List<String> names = new ArrayList<String>();

    // Select All Query
    String selectQuery = "SELECT  * FROM makanan";

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            names.add(cursor.getString(1));
        } while (cursor.moveToNext());
    }

    // closing connection
    cursor.close();
    db.close();

    // returning names
    return names;
}

1 个答案:

答案 0 :(得分:0)

您可以向DatabaseHelper添加一个查询,该查询根据namamakanan列的值返回kalon列。

e.g。

    public String getKalonFromNamamakanan(String namamakanan) {

        String rv = "Unknown";
        String[] columns = { "kalon" };
        String whereclause = "namamakanan=?";
        String[] whereargs = { namamakanan };
        Cursor csr = db.query("yourtable",columns,whereclause,whereargs,null,null,null);
        if(csr.getCount() > 0) {
            csr.moveToFirst();
            rv = csr.getString(csr.getColumnIndex("kalon"));
        }
        csr.close();
        return rv;
    }

然后你可以使用类似的东西来获取基于namamakanan列的kalon。

    etpagilauk1kcal.setText(db.getKalonFromNamamakanan(label)); 

在您的onItemSelected方法中。请注意,这假设namamakanan将是唯一的并且 db 将是SQLiteDatabase(,即您可能必须添加SQLiteDatabase db = this.getWritableDatabase();SQLiteDatabase db = this.getReadableDatabase();,具体取决于{{{ 1}}已编码

代码如何使用单独/单独的监听器(根据评论添加)

而不是

DataHelperMakanan

你可以: -

    spinnerpagilauk1.setOnItemSelectedListener(this);
    spinnerpagilauk2.setOnItemSelectedListener(this);