lundi 7 mars 2016

java.lang.IllegalArgumentException: column '_id' does not exist Error

Following on from this question I attempted a different approach which is better suited to what I am trying to do however I have run into a couple more problems firstly the error

java.lang.IllegalArgumentException: column '_id' does not exist

I do have a _id column defined in my database create statement however I still get the error.

Secondary the CursorAdapter is expecting a int to be defined within the argument which I believe is defined as flags in the method entryCursorAdapter however on the example they only had two arguments defined.

Database helper class

    public Cursor getAllData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from " +TABLE_NAME, null);
    return res;
}

Within OnCreate method

     myDb = new database_helper(this);
    Cursor res = myDb.getAllData();

    // Find ListView to populate
    ListView entryItems = (ListView) findViewById(R.id.displayview);
    // Setup cursor adapter using cursor from last step
    entryCursorAdapter entryAdapter = new entryCursorAdapter(this, res);
    // Attach cursor adapter to the ListView
    entryItems.setAdapter(entryAdapter);

Within (defined after OnCreate method)

    public class entryCursorAdapter extends CursorAdapter {
    public   entryCursorAdapter(Context context, Cursor res, int flags) {
        super(context, res, 0);
    }

    // The newView method is used to inflate a new view and return it,
    // you don't bind any data to the view at this point.
    @Override
    public View newView(Context context, Cursor res, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.display_data_layout, parent, false);
    }

    // The bindView method is used to bind all data to a given view
    // such as setting the text on a TextView.
    @Override
    public void bindView(View view, Context context, Cursor res) {
        // Find fields to populate in inflated template
        TextView entryid = (TextView) view.findViewById(R.id.id);
        TextView entrymeal = (TextView) view.findViewById(R.id.foodname);
        TextView entryfoodname = (TextView) view.findViewById(R.id.meal_type);
        TextView entrysyns = (TextView) view.findViewById(R.id.syns);
        TextView entryhexa = (TextView) view.findViewById(R.id.hexa);
        TextView entryhexb = (TextView) view.findViewById(R.id.hexb);
        TextView entryfreefood = (TextView) view.findViewById(R.id.freefood);
        TextView entryfibre = (TextView) view.findViewById(R.id.fibre);
        TextView entryprotein = (TextView) view.findViewById(R.id.protein);
        TextView entryspeed = (TextView) view.findViewById(R.id.speedfood);

        // Extract properties from cursor
        int id = res.getInt(0);
        String foodname = res.getString(1);
        String meal = res.getString(2);
        int syns = res.getInt(3);
        float hexa = res.getFloat(4);
        float hexb = res.getFloat(5);
        int freefood = res.getInt(6);
        int fibre = res.getInt(7);
        int protein = res.getInt(8);
        int speed = res.getInt(9);


        // Populate fields with extracted properties

        entryid.setText(String.valueOf(id));
        entrymeal.setText(meal);
        entryfoodname.setText(foodname);
        entrysyns.setText(String.valueOf(syns));
        entryhexa.setText(String.valueOf(hexa));
        entryhexb.setText(String.valueOf(hexb));
        entryfreefood.setText(String.valueOf(freefood));
        entryfibre.setText(String.valueOf(fibre));
        entryprotein.setText(String.valueOf(protein));
        entryspeed.setText(String.valueOf(speed));


    }


}

Aucun commentaire:

Enregistrer un commentaire