使用simplecursor adapter

时间:2015-08-08 21:28:05

标签: android sqlite

我一直在尝试使用Simplecursor适配器从sqlite数据库填充listview。我收到以下错误

08-09 06:52:07.875 21262-21262 / com.example.s210121629.myhealth E / AndroidRuntime:FATAL EXCEPTION:main     处理:com.example.s210121629.myhealth,PID:21262     显示java.lang.NullPointerException             at com.example.s210121629.myhealth.contacts.FragmentContact.displayContacts(FragmentContact.java:85)             at com.example.s210121629.myhealth.contacts.FragmentContact.onCreateView(FragmentContact.java:47)             在android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)             在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)             在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)             在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)             在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)             在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:458)             在android.os.Handler.handleCallback(Handler.java:733)             在android.os.Handler.dispatchMessage(Handler.java:95)             在android.os.Looper.loop(Looper.java:136)

联系人表格中有联系人。

我在下面包含了我的数据库助手类( DBHelper

public class DBHelper extends SQLiteOpenHelper {

    public static final String TAG = "DBHelper";

    // Columns of Person Table
    public static final String TABLE_PERSON = "Person";
    public static final String COLUMN_PERSON_ID = "_id";
    public static final String COLUMN_PERSON_USERNAME = "Username";
    public static final String COLUMN_PERSON_PASSWORD = "Password";
    public static final String COLUMN_PERSON_NAME = "Name";
    public static final String COLUMN_PERSON_SURNAME = "Surname";
    public static final String COLUMN_PERSON_GENDER = "Gender";
    public static final String COLUMN_PERSON_DOB = "Dob";
    public static final String COLUMN_PERSON_HEIGHT = "Height";
    public static final String COLUMN_PERSON_WEIGHT = "Weight";
    public static final String COLUMN_PERSON_SYSTOLICBP = "Systolicbp";
    public static final String COLUMN_PERSON_DIASTOLICBP = "Diastolicbp";
    public static final String COLUMN_PERSON_BPDATE = "Bpdate";
    public static final String COLUMN_PERSON_MAXRATE = "Maxhrrate";
    public static final String COLUMN_PERSON_MINRATE = "Minhrrate";

    // Columns of Medical Profile Table
    public static final String TABLE_MEDICAL_PROFILE = "MedicalProfile";
    public static final String COLUMN_MEDICAL_PROFILE_ID = "_id";
    public static final String COLUMN_MEDICAL_PROFILE_USERNAME = COLUMN_PERSON_USERNAME;
    public static final String COLUMN_MEDICAL_PROFILE_DATE_DIAGNOSED = "DateDiagnosed";
    public static final String COLUMN_MEDICAL_PROFILE_CONDITION = "Condition";


    // Columns of Contact Table
    public static final String TABLE_CONTACT = "Contacts";
    public static final String COLUMN_CONTACT_ID = "_id";
    public static final String COLUMN_CONTACT_USERNAME = COLUMN_PERSON_USERNAME;
    public static final String COLUMN_CONTACT_NAME = "Kinname";
    public static final String COLUMN_CONTACT_SURNAME = "Kinsurname";
    public static final String COLUMN_CONTACT_CELLPHONE = "Kincellphone";
    public static final String COLUMN_CONTACT_CAREGIVER_NAME = "Caregivername";
    public static final String COLUMN_CONTACT_CAREGIVER_SURNAME = "Caregiversurname";
    public static final String COLUMN_CONTACT_CAREGIVER_CELLPHONE = "Caregivercell";
    public static final String COLUMN_CONTACT_CAREGIVER_TELEPHONE = "Caregivertel";


    private static final String DATABASE_NAME = "myHealth.db";
    private static final int DATABASE_VERSION = 1;
    public SQLiteDatabase db;

    // SQL statement of the person table creation
    private static final String SQL_CREATE_TABLE_PERSON = "CREATE TABLE " + TABLE_PERSON + "("
            + COLUMN_PERSON_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_PERSON_USERNAME + " TEXT NOT NULL, "
            + COLUMN_PERSON_PASSWORD + " TEXT NOT NULL,"
            + COLUMN_PERSON_NAME + " TEXT NOT NULL,"
            + COLUMN_PERSON_SURNAME + " TEXT NOT NULL,"
            + COLUMN_PERSON_GENDER + " TEXT NOT NULL,"
            + COLUMN_PERSON_DOB + " TEXT NOT NULL,"
            + COLUMN_PERSON_HEIGHT + " REAL NOT NULL,"
            + COLUMN_PERSON_WEIGHT + " REAL NOT NULL,"
            + COLUMN_PERSON_SYSTOLICBP + " INTEGER NOT NULL,"
            + COLUMN_PERSON_DIASTOLICBP + " INTEGER NOT NULL,"
            + COLUMN_PERSON_BPDATE + " TEXT NOT NULL,"
            + COLUMN_PERSON_MAXRATE + " INTEGER NOT NULL,"
            + COLUMN_PERSON_MINRATE + " INTEGER NOT NULL "
            + ");";
    // SQL statement of the medical table creation
    private static final String SQL_CREATE_TABLE_MEDICAL_PROFILE = "CREATE TABLE " + TABLE_MEDICAL_PROFILE + "("
            + COLUMN_MEDICAL_PROFILE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_MEDICAL_PROFILE_USERNAME + " TEXT NOT NULL, "
            + COLUMN_MEDICAL_PROFILE_DATE_DIAGNOSED + " TEXT NOT NULL, "
            + COLUMN_MEDICAL_PROFILE_CONDITION + " TEXT NOT NULL "
            + ");";

    // SQL statement of the contact table creation
    private static final String SQL_CREATE_TABLE_CONTACT = "CREATE TABLE " + TABLE_CONTACT + "("
            + COLUMN_CONTACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_CONTACT_USERNAME + " TEXT NOT NULL, "
            + COLUMN_CONTACT_NAME + " TEXT NOT NULL,"
            + COLUMN_CONTACT_SURNAME + " TEXT NOT NULL,"
            + COLUMN_CONTACT_CELLPHONE + " TEXT NOT NULL,"
            + COLUMN_CONTACT_CAREGIVER_NAME + " TEXT NOT NULL,"
            + COLUMN_CONTACT_CAREGIVER_SURNAME + " TEXT NOT NULL,"
            + COLUMN_CONTACT_CAREGIVER_CELLPHONE + " TEXT NOT NULL,"
            + COLUMN_CONTACT_CAREGIVER_TELEPHONE + " TEXT NOT NULL"
            + ");";

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(SQL_CREATE_TABLE_PERSON);
        database.execSQL(SQL_CREATE_TABLE_MEDICAL_PROFILE);
        database.execSQL(SQL_CREATE_TABLE_CONTACT);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG,
                "Upgrading the database from version " + oldVersion + " to " + newVersion);
        // clear all data

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PERSON);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEDICAL_PROFILE);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT);
        // recreate the tables
        onCreate(db);
    }

    public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }


    /* The following Sql statements are used to manipulate the tables that have been created for the myHealth database */

    public void createPerson(String username, String password, String name, String surname, String gender, String dob, double height, double weight, int systolic, int diastolic, String bpdate, int maxhr, int minhr) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_PERSON_USERNAME, username);
        values.put(COLUMN_PERSON_PASSWORD, password);
        values.put(COLUMN_PERSON_NAME, name);
        values.put(COLUMN_PERSON_SURNAME, surname);
        values.put(COLUMN_PERSON_GENDER, gender);
        values.put(COLUMN_PERSON_DOB, dob);
        values.put(COLUMN_PERSON_HEIGHT, height);
        values.put(COLUMN_PERSON_WEIGHT, weight);
        values.put(COLUMN_PERSON_SYSTOLICBP, systolic);
        values.put(COLUMN_PERSON_DIASTOLICBP, diastolic);
        values.put(COLUMN_PERSON_BPDATE, bpdate);
        values.put(COLUMN_PERSON_MAXRATE, maxhr);
        values.put(COLUMN_PERSON_MINRATE, minhr);
        db.insert(TABLE_PERSON, null, values);
    }

    public void createMedicalProfile(String Username, String condition, String datediagnosed) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_MEDICAL_PROFILE_USERNAME, Username);
        values.put(COLUMN_MEDICAL_PROFILE_CONDITION, condition);
        values.put(COLUMN_MEDICAL_PROFILE_DATE_DIAGNOSED, datediagnosed);
        db.insert(TABLE_MEDICAL_PROFILE, null, values);
    }

    public void createContact(String Username, String name, String surname, String cell, String carename, String caresurname, String carecell, String caretel) {
        db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_CONTACT_USERNAME, Username);
        values.put(COLUMN_CONTACT_NAME, name);
        values.put(COLUMN_CONTACT_SURNAME, surname);
        values.put(COLUMN_CONTACT_CELLPHONE, cell);
        values.put(COLUMN_CONTACT_CAREGIVER_NAME, carename);
        values.put(COLUMN_CONTACT_CAREGIVER_SURNAME, caresurname);
        values.put(COLUMN_CONTACT_CAREGIVER_CELLPHONE, carecell);
        values.put(COLUMN_CONTACT_CAREGIVER_TELEPHONE, caretel);
        db.insert(TABLE_CONTACT, null, values);

    }

    public boolean checkContactTable() {
        db = this.getReadableDatabase();
        Cursor c = null;
        boolean tableexists = true;
        String selectQuery = " SELECT *  FROM " + TABLE_MEDICAL_PROFILE;

        Log.d("LOG", selectQuery);
        try {
            c = db.rawQuery(selectQuery, null);

            if (c.getCount() < 1) {
                c.close();
                tableexists = false;
            }
            c.moveToFirst();
            c.close();
        } catch (Exception e) {
            Log.d(TAG, TABLE_CONTACT + "Does not exist");
        }
        return tableexists;
    }

    public String getPassword(String Username) {
        SQLiteDatabase db = this.getReadableDatabase();

        String selectQuery = " SELECT " + COLUMN_PERSON_PASSWORD + " FROM " + TABLE_PERSON + " WHERE "
                + COLUMN_PERSON_USERNAME + " = '" + Username + "'";

        Log.d("LOG", selectQuery);

        Cursor c = db.rawQuery(selectQuery, null);

        if (c != null)
            c.moveToFirst();
        c.moveToFirst();
        String password = c.getString(c.getColumnIndex("Password"));
        c.close();
        return password;
    }

    public Person getPerson(String Username) {
        SQLiteDatabase db = this.getReadableDatabase();

        String selectQuery = " SELECT * FROM " + TABLE_PERSON + " WHERE "
                + COLUMN_PERSON_USERNAME + " = '" + Username + "'";

        Log.d("LOG", selectQuery);

        Cursor c = db.rawQuery(selectQuery, null);

        if (c != null)
            c.moveToFirst();
        c.moveToFirst();
        Person person = new Person();
        person.setmHeight(c.getDouble(c.getColumnIndex("Height")));
        person.setmWeight(c.getDouble(c.getColumnIndex("Weight")));
        person.setmSystolic(c.getInt(c.getColumnIndex("Systolicbp")));
        person.setmDiastolic(c.getInt(c.getColumnIndex("Diastolicbp")));
        person.setmMaxrate(c.getInt(c.getColumnIndex("Maxhrrate")));
        person.setmMinrate(c.getInt(c.getColumnIndex("Minhrrate")));

        c.close();
        return person;
    }

    public boolean getMedicalProfile(String Username) {
        db = getReadableDatabase();
        Cursor c = null;
        boolean tableexists = true;

        String selectQuery = " SELECT " + COLUMN_MEDICAL_PROFILE_USERNAME + " FROM " + TABLE_MEDICAL_PROFILE + " WHERE "
                + COLUMN_MEDICAL_PROFILE_USERNAME + " = '" + Username + "'";

        Log.d("LOG", selectQuery);
        try {
            c = db.rawQuery(selectQuery, null);

            if (c.getCount() < 1) {
                c.close();
                tableexists = false;
            }
            c.moveToFirst();

            String username = c.getString(c.getInt(c.getColumnIndex("Username")));
            c.close();

        } catch (Exception e) {
            Log.d(TAG, TABLE_MEDICAL_PROFILE + "Does not exist");
        }
        return tableexists;
    }


    public void updatePersonalInfo(String Username, Double height, Double weight, int systolic, int diastolic, int maxrate, int minrate) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues updatedvalues = new ContentValues();
        updatedvalues.put(COLUMN_PERSON_HEIGHT, height);
        updatedvalues.put(COLUMN_PERSON_WEIGHT, weight);
        updatedvalues.put(COLUMN_PERSON_SYSTOLICBP, systolic);
        updatedvalues.put(COLUMN_PERSON_DIASTOLICBP, diastolic);
        updatedvalues.put(COLUMN_PERSON_MAXRATE, maxrate);
        updatedvalues.put(COLUMN_PERSON_MINRATE, minrate);
        db.update(TABLE_PERSON, updatedvalues, COLUMN_PERSON_USERNAME + " = '" + Username + "'", null);

    }

    public void insertMedicalcondition(String Username, String condition, String dateadiagnosed) {
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_MEDICAL_PROFILE_USERNAME, Username);
        values.put(COLUMN_MEDICAL_PROFILE_CONDITION, condition);
        values.put(COLUMN_MEDICAL_PROFILE_DATE_DIAGNOSED, dateadiagnosed);
        db.insert(TABLE_MEDICAL_PROFILE, null, values);


    }

    public void insertContact(String Username, String name, String surname, String cell, String carename, String caresurname, String carecell, String caretel) {
        {
            ContentValues values = new ContentValues();
            values.put(COLUMN_CONTACT_USERNAME, Username);
            values.put(COLUMN_CONTACT_NAME, name);
            values.put(COLUMN_CONTACT_SURNAME, surname);
            values.put(COLUMN_CONTACT_CELLPHONE, cell);
            values.put(COLUMN_CONTACT_CAREGIVER_NAME, carename);
            values.put(COLUMN_CONTACT_CAREGIVER_SURNAME, caresurname);
            values.put(COLUMN_CONTACT_CAREGIVER_CELLPHONE, carecell);
            values.put(COLUMN_CONTACT_CAREGIVER_TELEPHONE, caretel);
            db.insert(TABLE_CONTACT, null, values);
        }
    }

    public Cursor readContacts() {
        db = getWritableDatabase();
        String[] allColumns = new String[]{COLUMN_CONTACT_ID, COLUMN_CONTACT_NAME,
                COLUMN_CONTACT_SURNAME,
                COLUMN_CONTACT_CELLPHONE,
                COLUMN_CONTACT_CAREGIVER_NAME,
                COLUMN_CONTACT_CAREGIVER_SURNAME,
                COLUMN_CONTACT_CAREGIVER_CELLPHONE,
                COLUMN_CONTACT_CAREGIVER_TELEPHONE,
        };
        Cursor c = db.query(TABLE_CONTACT, allColumns, null,
                null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }
}

FragmentContact 片段

public class FragmentContact extends Fragment {

    @InjectView(R.id.contactslist)
    ListView _contactlist;
    private ContactDAO mContactDao;
    String kinname, kinsurname, kincell, caregivername, caregiversurname, caregivercell, caregivertel;
    private SimpleCursorAdapter dataAdapter;
    DBHelper mdbHelper;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = (LinearLayout) inflater.inflate(R.layout.fragment_contacts, container, false);
        ButterKnife.inject(getActivity(), view);
    //    this.mContactDao = new ContactDAO(getActivity());
        mdbHelper = new DBHelper(getActivity());


        displayContacts();

        return view;
    }

    public void displayContacts()
    {
        Cursor cursor = mdbHelper.readContacts();
        String[] columns = new String[]{
                mdbHelper.COLUMN_CONTACT_NAME,
                mdbHelper.COLUMN_CONTACT_SURNAME,
                mdbHelper.COLUMN_CONTACT_CELLPHONE,
                mdbHelper.COLUMN_CONTACT_CAREGIVER_NAME,
                mdbHelper.COLUMN_CONTACT_CAREGIVER_SURNAME,
                mdbHelper.COLUMN_CONTACT_CAREGIVER_CELLPHONE,
                mdbHelper.COLUMN_CONTACT_CAREGIVER_TELEPHONE};


        // the XML defined views which the data will be bound to
        int[] to = new int[]{
                R.id._inputname,
                R.id.inputsurname,
                R.id.inputcell,
                R.id.inputcaregiver_name,
                R.id.inputcaregiver_surname,
                R.id.inputcaregiver_cell,
                R.id.inputcaregiver_tel
        };
        // create the adapter using the cursor pointing to the desired data
        //as well as the layout information
        dataAdapter = new SimpleCursorAdapter(
                getActivity(), R.layout.fragment_contacts,
                cursor,
                columns,
                to,
                0);

        _contactlist.setAdapter(dataAdapter);
    }
}

我已经包含了填充simplecursor适配器所需的所有变量,并且应用程序仍然崩溃。我已经包含了数据库助手类和片段类的代码,我实现了simplecursor适配器

2 个答案:

答案 0 :(得分:1)

根据堆栈跟踪,你得到一个

  

java.lang.IllegalArgumentException:列'_id'不存在

要避免这种情况,请将表“联系人”的列“person_id”重命名为“_id”。

除此之外,在'readContacts()'方法集

      String[] allColumns = new String[]{ COLUMN_CONTACT_NAME,
            COLUMN_CONTACT_SURNAME,
            COLUMN_CONTACT_CELLPHONE,
            COLUMN_CONTACT_CAREGIVER_NAME,
            COLUMN_CONTACT_CAREGIVER_SURNAME,
            COLUMN_CONTACT_CAREGIVER_CELLPHONE,
            COLUMN_CONTACT_CAREGIVER_TELEPHONE,
            COLUMN_CONTACT_ID
    };

使数据库列的顺序与TextViews的顺序匹配。出于正式原因包含'_id'列,但如果您对其值不感兴趣,则无需提供相应的TextView。

通常,如果要使用某种CursorAdapter,请确保您的表包含名为“_id”的列(如主键),并使该列成为所选列的一部分。

最后,您需要将ListView'_contactlist'绑定到'onCreateView()'中布局文件中的相应ListView:

View view = (LinearLayout) inflater.inflate(R.layout.fragment_contacts, container, false);
...
 // in the layout file 'fragment_contacts.xml'
 // let the ListView have an attribute 'android:id="@+id/theListViewId" '
_contactlist = (ListView)view.findViewById(R.id.theListViewId);

答案 1 :(得分:0)

第一个问题是我的光标没有返回Contact表中的所有列。 第二个问题是我没有实例化listview。最终代码如下:

public class FragmentContact extends Fragment {

    @InjectView(R.id.contactslist)
    ListView _contactlist;
    private ContactDAO mContactDao;

    private SimpleCursorAdapter dataAdapter;
    DBHelper mdbHelper;
    @InjectView(R.id._inputname)
    TextView _inputname;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = (LinearLayout) inflater.inflate(R.layout.fragment_contacts, container, false);
        ButterKnife.inject(getActivity(), view);
    //    this.mContactDao = new ContactDAO(getActivity());
        mdbHelper = new DBHelper(getActivity());
        _contactlist = (ListView)view.findViewById(R.id.contactslist);

        displayContacts();

        return view;
    }

    public void displayContacts()
    {
        Cursor cursor = mdbHelper.readContacts();
        String[] columns = new String[]{

                mdbHelper.COLUMN_CONTACT_NAME,
                mdbHelper.COLUMN_CONTACT_SURNAME,
                mdbHelper.COLUMN_CONTACT_CELLPHONE,
                mdbHelper.COLUMN_CONTACT_CAREGIVER_NAME,
                mdbHelper.COLUMN_CONTACT_CAREGIVER_SURNAME,
                mdbHelper.COLUMN_CONTACT_CAREGIVER_CELLPHONE,
                mdbHelper.COLUMN_CONTACT_CAREGIVER_TELEPHONE,
                mdbHelper.COLUMN_CONTACT_ID
                };



      //  Toast.makeText(getActivity(), "" + cursor, Toast.LENGTH_SHORT).show();

        // the XML defined views which the data will be bound to
        int[] to = new int[]{
                R.id._inputname,
                R.id.inputsurname,
                R.id.inputcell,
                R.id.inputcaregiver_name,
                R.id.inputcaregiver_surname,
                R.id.inputcaregiver_cell,
                R.id.inputcaregiver_tel
        };
        // create the adapter using the cursor pointing to the desired data
        //as well as the layout information
        dataAdapter = new SimpleCursorAdapter(
                getActivity(), R.layout.contacts_listitems,
                cursor,
                columns,
                to,
                0);

        _contactlist.setAdapter(dataAdapter);

    }
}