在Spinner中选择项目时,在SQlite数据库中显示主键

时间:2013-11-05 07:30:05

标签: android sqlite android-spinner

我正在尝试显示我在Spinner中选择的项目的主键。我想在TextView中显示主键。我会这样做吗?我已经知道如何在数据库中的表中显示字段。< / p>

在我的DatabseHandler.java中 这就是我在表格标准中插入数据的方法

public long insertLabelCriteria(String label, String label2, String label3){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_CRI_NAME, label);
    values.put(KEY_CRI_PER, label2); 
    values.put(KEY_CRI_EVPK, label3);
    // Inserting Row

    long id = db.insert(TABLE_CRITERIA, null, values);
    db.close(); // Closing database connection

    return id;



}

这是获取标签和返回标签列表的方法

public List<Criteria> getAllLabels( String evpk ){
    List<Criteria> labels = new ArrayList<Criteria>();

    SQLiteDatabase db = this.getReadableDatabase();

   String selectQuery = "SELECT  * FROM " + TABLE_CRITERIA + " WHERE "
         + KEY_CRI_EVPK + " = " + evpk ;



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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            labels.add(new Criteria(cursor.getString(1)));


        } while (cursor.moveToNext());
    }

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

    // returning lables
    return labels;

}

在我的MainActivity中

我有一个方法loadSpinnerData我使用这个evrytime我添加一个条件,它将加载Spinner来查看我在数据库中添加的项目

private void loadSpinnerData() {
    // TODO Auto-generated method stub
    // database handler
    DatabaseHandler db = new DatabaseHandler(getApplicationContext());

    // Spinner Drop down elements
    List<Criteria> lables = db.getAllLabels(evpk.getText().toString());

    // Creating adapter for spinner

    ArrayAdapter<Criteria> dataAdapter = new ArrayAdapter<Criteria> (this,
    android.R.layout.simple_spinner_dropdown_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
    criteria_spin.setAdapter(dataAdapter);
    criteria_spin.setOnItemSelectedListener(this);
}

现在,在选择项目时,如何在微调器中显示所选项目的主键? 以下代码仅用于显示和选择项目点击。

  @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(), "You selected: " + label,
            Toast.LENGTH_LONG).show();


}

我也尝试添加此代码,但id显示的是arraylist数字,而不是主键

 long rowId = id;
 String criteriapk = String.valueOf(label);
 cripk.setText(criteriapk);

我发现很难找到解决方案。我该怎么办?帮帮我吧

我也试过这个方法,但是值是cursor.in TextView,它不显示数字。

public Cursor find_id_of_criteria(String label){


SQLiteDatabase db=this.getWritableDatabase(); 
String selectQuery = "SELECT criteria_id FROM Criteria WHERE criteria_name = "+"'" + label +"'";
Cursor id = db.rawQuery(selectQuery, null);


db.close(); 
return  id;

}

并在loadSpinnerdata中我把这个

//display id of criteria
        Cursor id2 =      db.find_id_of_criteria(label);
        String cri =(String.valueOf(id2).toString());
        cripk.setText(cri);     

2 个答案:

答案 0 :(得分:0)

您必须实现扩展BaseAdapter或CursorAdapter类的自己的适配器,并使用它而不是ArrayAdapter。

以下是使用CursorAdapter的示例。我没有对它进行测试,因为它本身就是你自己实现的开始。当然,您也可以使用其他布局,并对此代码进行一些细微更改。

public class CriteriaCursorAdapter extends CursorAdapter {
    private class Holder {
        TextView text;
    }

    private LayoutInflater mInflater;
    public CriteriaCursorAdapter(Context context, Cursor c) {
        super(context, c);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(
                  android.R.layout.simple_spinner_dropdown_item, parent, false);
        }
        Holder holder = (Holder)convertView.getTag();
        if (holder == null) {
            holder = new Holder();
            holder.text = (TextView)convertView.findViewById(android.R.id.text1);
            convertView.setTag(holder);
        }
        Cursor c = (Cursor)getItem(position);
        holder.text.setText(c.getString(1));
        return convertView;
    }
}

重要提示:如果使用CursorAdapter,则光标必须包含名为“_id”的列。如果TABLE不包含此列,则可以通过修改SELECT语句来实现此目的!

SELECT columnPK _id, col1, .... FROM ...

要获取主键(光标列'_id'),您可以使用CriteriaCursorAdapter的long getItemId(int position);

您将找到许多扩展BaseAdapter或CursorAdapter的示例。 One Example

答案 1 :(得分:0)

简单..你快要回答了。按照以下方式更改getAllLabels

public List<Criteria> getAllLabels( String evpk ){
List<Criteria> labels = new ArrayList<Criteria>();
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT  * FROM " + TABLE_CRITERIA + " WHERE "
     + KEY_CRI_EVPK + " = " + evpk ;
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
    do {
        Criteria ct = new Criteria();
        ct.setLabel(cursor.getString(1));
        ct.setKey(Integer.parseInt(c.getString(0)));
        labels.add(ct);
    } while (cursor.moveToNext());
}

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

// returning lables
return labels;
}

这里我只更改了do while循环。通过这种方式为标签和键创建getter和setter。要获取主键,请使用选定的Criteria对象,如ct.getKey;我希望这会对你有所帮助。

<强>更新

List<String> field_key; //accessible in whole class.
private void loadSpinnerData() {
// TODO Auto-generated method stub
// database handler
DatabaseHandler db = new DatabaseHandler(getApplicationContext());

// Spinner Drop down elements
List<Criteria> lables = db.getAllLabels(evpk.getText().toString());
List<String> field_lables = new ArrayList<String>();
field_key = new ArrayList<String>();

// Creating adapter for spinner
for (Criteria ct : lables) {
    field_lables.add(ct.getLabel);
    field_key.add(ct.getkey);
}

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String> (this,
android.R.layout.simple_spinner_dropdown_item, field_lables);

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

// attaching data adapter to spinner
criteria_spin.setAdapter(dataAdapter);
criteria_spin.setOnItemSelectedListener(this);
}

&安培;

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    // On selecting a spinner item
    String label = parent.getItemAtPosition(position).toString();
    String key = field_key.get(position).toString();


    // Showing selected spinner item
    Toast.makeText(parent.getContext(), "You selected: " + label+" Your key: " + key,
        Toast.LENGTH_LONG).show();
}

尝试这种方式让我知道会发生什么