从数据库列加载不同的值

时间:2015-12-01 23:37:23

标签: android database sqlite

如何从具有相同名称的列(如照片)中获取不同的值?

DataBase Example

在照片中,“test”有3个不同的值,如何将它们加载到 ListView Spinner ?< / p>

我有这个代码,但是没有得到3个值,只有第一个值:

MainActivity

public void lookupProduct (View view) {
    DatabaseHandler dbHandler = new DatabaseHandler(getApplicationContext());
    Name name = dbHandler.findProduct(spinner.getSelectedItem().toString());
    Toast.makeText(this, spinner.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
    Intent j = new Intent(view.getContext(), SubActivity.class);
    Bundle dados = new Bundle();
    if (name != null) {
        inputLabel.setText(String.valueOf(name.getName()));
        values.setText(String.valueOf(name.getValue()));
        // Passar para SubActivity
        dados.putString("name", String.valueOf(name.getName()));
        dados.putString("value", String.valueOf(name.getValue()));
    } else {
        inputLabel.setText("No Match Found");
        dados.putString("name","No Match Found" );
        dados.putString("value", "No Match Found");
    }
    j.putExtras(dados);
    startActivity(j);
}

DatabaseHelper

public Name findProduct(String name) {
    String query = "Select * FROM " + TABLE_LABELS + " WHERE " + KEY_NAME + " = \"" + name + "\"";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    Name names = new Name();
    if (cursor.moveToFirst()) {
        cursor.moveToFirst();
        names.setID(Integer.parseInt(cursor.getString(0)));
        names.setName(cursor.getString(1));
        names.setValue(cursor.getString(2));
        cursor.close();
    } else {
        names = null;
    }
    db.close();
    return names;
}

NameClass

public class Name {
private int _id;
private String _name;
private String _value;
public Name() {
}
public Name(int id, String name, String value) {
    this._id = id;
    this._name = name;
    this._value = value;
}
public Name(String name, String value) {
    this._name = name;
    this._value = value;
}public String getName() {
    return this._name;
}
public String getValue() {
    return this._value;
}

2 个答案:

答案 0 :(得分:0)

试试这个

 Name names = new Name();
 ArrayList<Name > listaName= new ArrayList<>();//create an arraylist of your               
                                            custom objects

 if (cursor.moveToFirst()) {
                    do {
    names.setID(Integer.parseInt(cursor.getString(0)));
    names.setName(cursor.getString(1));
    names.setValue(cursor.getString(2));

    listaName.add(names);//add your object to arraylist(you were overriding the object.)

   } while (cursor.moveToNext());

                    cursor.close();

 //AND ALSO CLOSE DB:


                     db.close

编辑2:试试这个并更改你的 - &gt;字符串查询=&#34;从&#34; + TABLE_NAME;

中选择*
 for ( int i= 1; i< listaName.size(); i++ ) {
  System.out.println(listaName.get(i).getName());
}

你的问题:

public Name findProduct(String name) {
String query = "Select * FROM " + TABLE_LABELS + " WHERE " + KEY_NAME + " = \"" + name + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
Name names = new Name();
if (cursor.moveToFirst()) {
    cursor.moveToFirst(); // this is not necessary because on the top line,    
                             you put it in that position
    names.setID(Integer.parseInt(cursor.getString(0)));
    names.setName(cursor.getString(1));
    names.setValue(cursor.getString(2));
    cursor.close();             // You should not close until it is 

                                    completely used
} else {
    names = null;
}
db.close();
return names;

}

要读取所有光标,必须使用do-while方法

如果您有任何问题,可以再次问我

答案 1 :(得分:0)

虽然尚未经过测试,但您可以尝试:

// fetch data from DB
public ArrayList<Name> findProduct(String name) {
String query = "Select * FROM " + TABLE_LABELS + " WHERE " + KEY_NAME + " = \"" + name + "\"";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
ArrayList<Name> listOfNames= new ArrayList<>();
if (cursor.moveToFirst()) {
    do {
        listOfNames.add(new Name(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2)));
   } while (cursor.moveToNext());
cursor.close();
db.close
return listOfNames;
}

// add data on spinner
public void addItemsOnSpinner() {
Spinner mSpinner = (Spinner) findViewById(R.id.mSpinner);
ArrayList<String> list = new ArrayList<String>();
for(Name name: findProduct("test")){
    list.add(name.getValue());
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setAdapter(dataAdapter);
}