2013-10-04 34 views
0

大家好我想显示记录。但是,我不想在listview中显示它们,而是在textview中显示它们。我已经在做新的换行\ n来完成这个技巧,但是在我的程序中,它只是显示了第一条记录。在textview中显示多行Android

这是我到目前为止已经试过:

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; 

} 

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

回答

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; 

}

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(); 
} 
+0

感谢您的回答,我可能会问什么应该放在访问记录中我的应用程序? – Dunkey

+0

用cursor.getString()读取记录的所有字段,你已经在做它了。 – sjdutta