0

I am building a list box manipulated by cursor adapter. The call to create cursor adapter crashes. I have written the code below. RoomShare is the activity which holds the list. MessDatabase is the class that extends from SQLiteOpenHelper. TempDataFromDB is the temporary class that holds the ecord derived from DB.DB2CursorAdapter is the cursor adapter. logcat- java.lang.IllegalArgumentException: column '_id' does not exist. Available columns: [id, Participants, Mess_NameTEXT]

    public class RoomShare extends AppCompatActivity {

            DB2CursorAdapter DBAdapter;
            MessDatabase mdb;
            Cursor cursor;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_room_share);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);

                FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
                fab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Snackbar.make(view, "text u", Snackbar.LENGTH_LONG)
                                .setAction("Action", null).show();
                    }
                });

                String selectQuery = "SELECT  * FROM " + TempDataFromDB.TABLE_NAME /*+ " ORDER BY " +
                        TempDataFromDB.COLUMN_MESS_NAME + " DESC"*/;
                mdb = new MessDatabase(this);
                SQLiteDatabase db = mdb.getWritableDatabase();
                cursor = db.rawQuery(selectQuery, null);

                DBAdapter = new DB2CursorAdapter(this, cursor, false);//---------crash here
                //final ListView listview = (ListView) findViewById(R.id.listview);
                //listview.setAdapter(DBAdapter);

            }

  ---------------------------------------------------------------------------  
    public class DB2CursorAdapter extends CursorAdapter {
        private LayoutInflater cursorInflater;

        public DB2CursorAdapter(Context context, Cursor cursor, boolean flags) {
            super(context,cursor);
            cursorInflater = (LayoutInflater) context.getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);

        }

        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            // R.layout.list_row is your xml layout for each row
            return cursorInflater.inflate(R.layout.db_cursor_room_share, parent, false);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView tvBody = (TextView) view.findViewById(R.id.tvBody);
            TextView tvPriority = (TextView) view.findViewById(R.id.tvPriority);
            // Extract properties from cursor
            String id = cursor.getString(cursor.getInt(cursor.getColumnIndex(TempDataFromDB.COLUMN_ID)));
            String mess_name = cursor.getString(cursor.getColumnIndex(TempDataFromDB.COLUMN_MESS_NAME));
            // Populate fields with extracted properties
            tvBody.setText(id);
            tvPriority.setText(mess_name);
        }
    }
---------------------------------------------------------

public class MessDatabase extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "ShareRoom_db";

    public MessDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TempDataFromDB.CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        //this.getWritableDatabase();
    }
--------------------------------------------------------------------------------

public class TempDataFromDB {
    public static final String TABLE_NAME = "Share_room";
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_PARTICIPANTS = "Participants";
    public static final String COLUMN_MESS_NAME = "Mess_Name";


    private int id;
    private String Participants;
    String[] arrayParticipants;
    private String Mess_Name;


    // Create table SQL query
    public static final String CREATE_TABLE =
            "CREATE TABLE " + TABLE_NAME + "("
                    + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + COLUMN_PARTICIPANTS+ " TEXT,"
                    + COLUMN_MESS_NAME+"TEXT"
                    + ")";

    public TempDataFromDB() {
    }

    public TempDataFromDB(int id, String Participants,String Mess) {
        this.id = id;
        this.Participants = Participants;
        this.Mess_Name = Mess;
    }

    public int getId() {
        return id;
    }

    public String getParticipants() {

        //arrayParticipants = convertStringToArray(Participants);
        return this.Participants;
    }

    public void setParticipant(String Participants) {
        this.arrayParticipants = convertStringToArray(Participants);
        //this.arrayParticipants = Participants;
        //this.Participants = convertArrayToString(this.arrayParticipants);
    }

    public String getMess_Name() {
        return Mess_Name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setMess_Name(String Mess_Name) {
        this.Mess_Name = Mess_Name;
    }
}
user1434287
  • 381
  • 3
  • 16
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Vladyslav Matviienko Nov 08 '18 at 12:46

1 Answers1

0

This is solved by changing

public static final String COLUMN_ID = "id";
to
public static final String COLUMN_ID = "_id";

I read it in a post, you should use "_id" instead of "id"

user1434287
  • 381
  • 3
  • 16