java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
I want to get the photo name from SQL database,then get the photo from drawable file and link the photo to a ImageView item which is inside a ListView what is the problem with following code, Please help me fix it, thank you
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);
}
}
I found that the problems appear in
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);
}
}