-2

ここでは、このアプリケーションにデータベースからレコードを取得させ、データベースから 5 つのエントリを取得し、このコードを使用して取得しました。

Cursor c = dbconnection.rawQuery("SELECT * from Patients", null);

その後、ループして、データベース内のデータごとの行を取得しました。

c.moveToFirst();
while(!c.isAfterLast())
{
 //Some code to put records per column in to a Patient Object 
 c.moveToNext();
}

したがって、私の問題は、ループに入るとエミュレーターがフリーズし、レコードごとにログに表示しようとしたときに、エミュレーター自体が既にフリーズしていないことです。

誰かこの問題について教えてくれませんか? この問題は私にとって本当に新しいものです

編集

はい、すでにバヤとオフィールが提案したことを試しました..彼らはうまくいきましたが、このループコードの反復中にこのヌルエラーが発生しました

Cursor c = dbHelper.retrieveAllData();

                c.moveToFirst();

                while(c.moveToNext())
                {
                    Log.d("dbcheck",Integer.toString(c.getPosition()));
                    //Log.d("dbcheck",c.getString(c.getColumnIndex("firstname")));
                    //Log.d("dbcheck",c.getString(c.getColumnIndex("lastname")));
                    **p.setFname(c.getString(c.getColumnIndex("firstname")));**
                    p.setMi(c.getString(c.getColumnIndex("middleinitial")));
                    p.setLname(c.getString(c.getColumnIndex("lastname")));
                    p.setAddr(c.getString(c.getColumnIndex("address")));
                    p.setAge(c.getInt(c.getColumnIndex("age")));
                    p.setMed_history(c.getString(c.getColumnIndex("med_history")));
                    p.setPat_status(c.getString(c.getColumnIndex("status")));
                    patientList.add(p);

                }

p.setFname() 行に null 例外エラーがあります.どのようにして null になったのかはわかりませんが、実際には、コメントアウトされたコードを使用して Log で既に表示しています..

4

2 に答える 2

4

ちょうど試して、

         Cursor c = dbconnection.rawQuery("SELECT * from Patients", null);
    if (c.getCount() > 0) {

        Patient p;

        while(c.moveToNext) {

            //initialize ur object to store new patient info.
            p = new Patient();
            p.setFname(c.getString(c.getColumnIndex("firstname")));

            //add newly created patient to ur list
            patientList.add(p);

        }
    }
c.close();
于 2012-07-29T06:35:58.597 に答える
0

次のようにしてみてください。

    // return all columns
    Cursor cursor = mDb.query(Patients, null, null, null, null, null, null);

    if ((cursor != null) && (cursor.getCount() > 0)) {
       while(cursor.moveToNext()){

    //Some code to put records per column in to a Patient Object 

    }
                    cursor.close();
   }
于 2012-07-29T06:36:32.573 に答える