类型不匹配:无法从List <datas>转换为Cursor </datas>

时间:2014-05-22 16:56:21

标签: java android

我是android的新手并且学习将sqllite数据库连接到应用程序。这是主要活动并在Cursor中获得错误=(Cursor)db.getAllData();线。错误说类型不匹配:无法从List转换为Cursor。请有人帮助我克服这一点。

public Cursor getAllData(){

    List<Datas> dataList = new ArrayList<Datas>();

    String buildSQL = "SELECT * FROM " + TABLE_NAME;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(buildSQL, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Datas r = new Datas();
            r.setkonsumen(cursor.getString(1));
            r.setbarang(cursor.getString(2));
            r.setharga(cursor.getString(3));
            r.setqty(cursor.getString(4));
            // Adding contact to list
            dataList.add(r);
        } while (cursor.moveToNext());
    }

    // return transaction list
    return dataList; // error here

}

这是Datas类:

public class Datas {

//private variables
int _id;
String _konsumen;
String _barang;
String _qty;
String _harga;

// Empty constructor
public Datas(){

}
// constructor
public Datas(int id, String konsumen, String barang, String qty, String harga){
    this._id = id;
    this._konsumen = konsumen;
    this._barang = barang;
    this._qty = qty;
    this._harga = harga;
}

// constructor
public Datas(String konsumen, String barang, String qty, String harga){
    this._konsumen = konsumen;
    this._barang = barang;
    this._qty = qty;
    this._harga = harga;
}
// getting ID
public int getID(){
    return this._id;
}

// setting id
public void setID(int id){
    this._id = id;
}

// getting name
public String getkonsumen(){
    return this._konsumen;
}

// setting name
public void setkonsumen(String konsumen){
    this._konsumen = konsumen;
}

// getting phone number
public String getbarang(){
    return this._barang;
}

// setting phone number
public void setbarang(String barang){
    this._barang = barang;
}

// getting phone number
public String getqty(){
    return this._qty;
}

// setting phone number
public void setqty(String qty){
    this._qty = qty;
}

// getting phone number
public String getharga(){
    return this._harga;
}

// setting phone number
public void setharga(String harga){
    this._harga = harga;
}

}

1 个答案:

答案 0 :(得分:2)

你有

 public Cursor getAllData () { // return type is cursor

但你回来了

 return dataList; // which is List<Datas> dataList

更改为

 public List<Datas> getAllData () {