0

大家好,我想显示记录。但是,我不想在 listview 中显示它们,而是在 textview 中显示它们。我必须在我的新换行符 \n 中完成这个技巧,但在我的程序中,它只显示第一条记录。

这是我迄今为止尝试过的:

MainActivity.class

 Bundle extras = getIntent().getExtras();
    if (extras != null) {
        dog_name = extras.getString("dog_name");
        cursor = dbHelper.fetchbBreedByName(dog_name);

        strID = cursor.getString(0);
        strDesc = cursor.getString(cursor.getColumnIndexOrThrow("description"));
        strDiet = cursor.getString(cursor.getColumnIndexOrThrow("diet"));
        strShelter = cursor.getString(cursor.getColumnIndexOrThrow("shelter"));
        strHygiene = cursor.getString(cursor.getColumnIndexOrThrow("hygiene"));
        strMedication = cursor.getString(cursor.getColumnIndexOrThrow("medication"));
        strBreed = cursor.getString(cursor.getColumnIndexOrThrow("breed"));

        Log.d("Animal ID", "Animal ID is " + strID + " and breed is " + strBreed);
        Log.d("Desc", "Desc " + strDesc);
        Description.setText(strDesc);
        Diet.setText(strDiet);
        Shelter.setText(strShelter);
        Hygene.setText(strHygiene);
        Medication.setText(strMedication); }

DBHelper.class

        public Cursor fetchbBreedByName(CharSequence inputText) throws SQLException {

      Cursor mCursor = null;
      if (inputText == null  ||  inputText.length () == 0)  {
       mCursor = myDataBase.query(DB_TABLE, new String[] { KEY_ID, KEY_DESCRIPTION, 
               KEY_DIET, KEY_SHELTER, KEY_HYGIENE, KEY_MEDICATION, KEY_BREED },
         null, null, null, null, null);

      }
      else {
          String qry = "SELECT _id, description, diet, shelter, hygiene, medication, " +
                "breed FROM tblAnimalInfo WHERE breed LIKE '%" + inputText + "%';";

          mCursor = myDataBase.rawQuery(qry, null);
       //mCursor = myDataBase.query(DB_TABLE, new String[] { KEY_ID, KEY_DESCRIPTION, 
        //     KEY_DIET, KEY_SHELTER, KEY_HYGIENE, KEY_MEDICATION, KEY_BREED },
       //  KEY_BREED + " like '%" + inputText + "%'", null, null, null, null);
      }
      if (mCursor != null) {
       mCursor.moveToFirst();
      }
      return mCursor;

}

我不知道怎么了。请帮我弄清楚我的代码中缺少什么。提前致谢。

4

2 回答 2

0

您似乎只加载了光标给出的第一行。你必须使用 cursor.moveToNext(); 能够获取您的其余数据。看看下面的例子:

// Getting All Contacts
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
    do {
        Contact contact = new Contact();
        contact.setID(Integer.parseInt(cursor.getString(0)));
        contact.setName(cursor.getString(1));
        contact.setPhoneNumber(cursor.getString(2));
        // Adding contact to list
        contactList.add(contact);
    } while (cursor.moveToNext());
}

// return contact list
return contactList;

}

于 2013-10-05T00:05:34.233 回答
0

如前所述,您缺少用于移动到下一条记录的 cursor.moveToNext() 函数。此外,我想提一下编码风格中的其他一些问题。您不应该在查询函数本身中调用 moveToFirst(),而是在实际遍历记录的地方使用它。还要确保适当地关闭光标。所以你的代码应该是这样的:

cursor = dbHelper.fetchbBreedByName(dog_name);
if (cursor != null) {
   if (cursor.moveToFirst()) {
      do {
          ....
          < access the record >
          ...       
      } while (cursor.moveToNext());
   }

   cursor.close();  // very important
}

并从 fetchbBreedByName() 中删除这些行

if (mCursor != null) {
    mCursor.moveToFirst();
}
于 2013-10-05T03:22:49.623 回答