Android游标问题"确保在从中访问数据之前正确初始化Cursor。"

时间:2017-12-08 11:42:30

标签: java android sqlite listview

java.lang.IllegalStateException:无法从CursorWindow读取第0行col -1。在从中访问数据之前,请确保Cursor已正确初始化。

我想从SQL数据库中获取照片名称,然后从可绘制文件中获取照片并将照片链接到ListView内部的ImageView项目,以下代码有什么问题,请帮我修复一下,谢谢

class CustomerDbContext : DbContext   
{
    public CustomerDbContext(DbContextOptions<CustomerDbContext> options) : base(options) { }
    public DbSet<Customer> Customers { get; set; }
}

我发现问题出现在

import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class HotProduct extends ListActivity {

    private ProductSQLiteHelper sqlHelper;
    private SQLiteDatabase infodb;
    private String[] columnsToSelect;
    private String[] columnsToSelect2;
    private Cursor infoCursor;
    private SimpleCursorAdapter dbAdapter;
    private SimpleCursorAdapter dbAdapter2;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hotproduct_middle_page);
        columnsToSelect = new String[] {
                ProductSQLiteHelper.PRODUCT_ID,
                ProductSQLiteHelper.PRODUCT_NAME,
                ProductSQLiteHelper.PRODUCT_BRAND,
                ProductSQLiteHelper.PRODUCT_PRICE


        };




        Resources res = getResources();
        Log.v("Res:",String.valueOf(res));


//        setTitle("Hot Product");



        sqlHelper = new ProductSQLiteHelper(this);

        infodb = sqlHelper.getReadableDatabase();


        Cursor infoCursor= infodb.rawQuery("SELECT photo FROM phone", null);
        ImageView moviePoster = (ImageView) findViewById(R.id.productPhoto);
        infoCursor.moveToFirst();
        if (!infoCursor.isAfterLast()) {
            if (infoCursor.getInt(infoCursor.getColumnIndex(String.valueOf(ProductSQLiteHelper.PRODUCT_HOT))) == 1) {
                res = this.getResources();
                String posterResourceName = infoCursor.getString(infoCursor.getColumnIndex(ProductSQLiteHelper.PRODUCT_PHOTO));
                Log.v("resource name",posterResourceName);
                int resId = res.getIdentifier(posterResourceName, "drawable", getPackageName());
                moviePoster.setImageResource(resId);
            }
        }

        String columnsToDisplay[] = {
                ProductSQLiteHelper.PRODUCT_NAME,
                ProductSQLiteHelper.PRODUCT_BRAND,
                ProductSQLiteHelper.PRODUCT_PRICE
//                ProductSQLiteHelper.PRODUCT_PHOTO
        };


        int mappingToView[] = {
                R.id.productName,
                R.id.productBrand,
                R.id.productPrice
//                R.id.productPhoto
        };





        String args[] = {"1"};
        infoCursor = infodb.query(ProductSQLiteHelper.TABLE_NAME,columnsToSelect," hot = ? ",args,null,null,null);

        String posterResourceName = infoCursor.getString(infoCursor.getColumnIndex("photo"));
        Log.v("resource name",posterResourceName);
        dbAdapter = new SimpleCursorAdapter(this,R.layout.hotproduct_middle_page_row,infoCursor,columnsToDisplay,mappingToView,0);


        setListAdapter(dbAdapter);



        }




}

1 个答案:

答案 0 :(得分:0)

您的问题是由于getColumnIndex未找到指定的列并因此返回-1(就像列不是列时那样)。根本原因是你在代码中创建了一个带有单个列的Cursor,你试图访问2列,即

  • infoCursor.getInt(infoCursor.getColumnIndex(String.valueOf(ProductSQLiteHelper.PRODUCT_HOT)))
  • infoCursor.getString(infoCursor.getColumnIndex(ProductSQLiteHelper.PRODUCT_PHOTO))

光标只包含1列,即照片列。

我建议将SELECT子句更改为 "SELECT * FROM phone" ,然后这将创建一个包含表中所有列的Cursor。

但是,代码中还有一个缺陷,即您没有检查moveToFirst的结果。

如果Cursor为空,则moveToFirst将返回false。由于Cursor在最后一次之后不会,因为没有数据,任何从Cursor检索数据的尝试都会失败。

我建议修改您的代码: -

        Cursor infoCursor= infodb.rawQuery("SELECT * FROM phone", null);
        ImageView moviePoster = (ImageView) findViewById(R.id.productPhoto);
        while (infoCursor.moveToNext()) {
            if (infoCursor.getInt(infoCursor.getColumnIndex(String.valueOf(ProductSQLiteHelper.PRODUCT_HOT))) == 1) {
                res = this.getResources();
                String posterResourceName = infoCursor.getString(infoCursor.getColumnIndex(ProductSQLiteHelper.PRODUCT_PHOTO));
                Log.v("resource name",posterResourceName);
                int resId = res.getIdentifier(posterResourceName, "drawable", getPackageName());
                moviePoster.setImageResource(resId);
            }
        }

您可能必须编写代码来处理空光标。