创建游标适配器时崩溃

时间:2018-11-08 12:31:53

标签: android list adapter

我正在构建一个由光标适配器操纵的列表框。创建游标适配器的调用崩溃。我已经写了下面的代码。 RoomShare是保存列表的活动。 MessDatabase是从SQLiteOpenHelper扩展的类。 TempDataFromDB是保存从DB2派生的ecord的临时类。DB2CursorAdapter是游标适配器。 logcat- java.lang.IllegalArgumentException:列'_id'不存在。可用列:[id,参与者,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;
    }
}

1 个答案:

答案 0 :(得分:0)

这可以通过更改来解决

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

我在帖子中读过它,应该使用“ _id”而不是“ id”