繁体   English   中英

Android游标问题“在访问游标之前,请确保游标已正确初始化。”

[英]Android cursor problems “Make sure the Cursor is initialized correctly before accessing data from it.”

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

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

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);



        }




}

我发现问题出现在

      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);
            }
        }

您的问题是由于getColumnIndex未找到指定的列,并因此返回-1(就像在列不是列时那样)。 根本原因是您正在创建一个具有单个列的游标,而在代码中却试图访问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 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);
            }
        }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM