使用自定义对象ArrayList从Sqlite获取数据?

时间:2017-06-20 14:35:26

标签: android sqlite android-sqlite

这是Employee类的代码。

public class Employee {
String name;
String email;
byte[] picture;

public Employee(String name, String email, byte[] picture) {
    this.name = name;
    this.email = email;
    this.picture = picture;
}
}

这是我从数据库获取数据的代码,目前无法正常工作,因为我无法弄清楚如何从数据库中获取这三样东西(字符串名称,电子邮件和字节数组图像)。但是我已成功将所有三件事插入到Sqlite数据库中。

**编辑:**这是我的数据库类完整代码。

package com.cplusplusapp.rashidfaheem.hybridsoftwaresolutions.hbss.rashidfaheem.image2database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;



public class Sqlite_database extends SQLiteOpenHelper {


private static final String DATABASE_NAME="myCustomerDatabase";
private static final String DATABASE_TABLE="Customers";
private static final int DATABASE_VERSION=1;

public static final String KEY_ROWID="id";
public static final String KEY_Name="customer_name";
public static final String KEY_Email="customer_email";
public static final String KEY_IMAGE="image";

private SQLiteDatabase ourDatabase;

ArrayList<Employee> elist;

public Sqlite_database(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" +
            KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            KEY_Name + " TEXT, " +
            KEY_Email + " TEXT, " +
            KEY_IMAGE + " BLOB);"
    );

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
    onCreate(sqLiteDatabase);
}



public List<Employee> getImage(){

    List<Employee> employee_list = new ArrayList<>();
    ourDatabase = this.getReadableDatabase();
    String[] field = {KEY_Name,KEY_Email,KEY_IMAGE};
    Cursor c = ourDatabase.query(DATABASE_TABLE, field, null, null, null, null, null);

    int iname = c.getColumnIndex(KEY_Name);
    int iemail = c.getColumnIndex(KEY_Email);
    int image = c.getColumnIndex(KEY_IMAGE);


    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        String name = c.getString(iname);
        String email = c.getString(iemail);
        byte[] pic = c.getBlob(image);
        employee_list.add(new Employee(name, email, pic));

    }

    return employee_list;
}




public long createImageEntry(String name, String email, byte[] image) {
    ourDatabase=this.getWritableDatabase();
    ContentValues cv=new ContentValues();
    cv.put(KEY_Name, name);
    cv.put(KEY_Email, email);
    cv.put(KEY_IMAGE, image);

    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}
}

这是点按钮上的代码,从该返回arraylist获取名称和电子邮件,并通过textviews显示。但是得到错误。                                                                                                                                         java.lang.IllegalStateException:无法从CursorWindow读取第0行col 0。在从中访问数据之前,请确保Cursor已正确初始化。

 btnView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sql = new Sqlite_database(MainActivity.this);
            List<Employee> employees = sql.getImage();

            for (Employee e : employees) {
                r_name = r_name + e.name;
                r_email = r_email + e.email;

            }

            tvName.setText(r_name);
            tvEmail.setText(r_email);
        }
    });

1 个答案:

答案 0 :(得分:0)

每次进行for循环时,您都要为Employee分配一个新的employee_list对象

将您的employee_list更改为此

List<Employee> employee_list = new ArrayList<Employee>();

您可以在for-loop

的末尾将对象添加到该列表中
employee_list.add(new Employee(name, email, pic));

完整的代码看起来像这样

public List<Employee> getImage(){

    List<Employee> employee_list = new ArrayList<Employee>();
    ourDatabase = this.getReadableDatabase();
    String[] field = {KEY_Name,KEY_Email,KEY_IMAGE};
    Cursor c = ourDatabase.query(DATABASE_TABLE, field, null, null, null, null, null);

    int iname = c.getColumnIndex(KEY_Name);
    int iemail = c.getColumnIndex(KEY_Email);
    int image = c.getColumnIndex(KEY_IMAGE);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        String name = c.getString(iname);
        String email = c.getString(iemail);
        byte[] pic = c.getBlob(image);
        employee_list.add(new Employee(name, email, pic));

    }

    return employee_list;
}

然后你可以像这样使用它

List<Employee> employees = getImage();

for (Employee employee : employees) {
    // You can use employee here that represents an Employee entry in the list
}