2016-03-28 74 views
0

嗨我有這個代碼,它從我創建的SQLite本地數據庫檢索數據。顯示所有抓取的數據SQLite

下面的代碼是一個公共光標來檢索所有我從數據庫

public Cursor RetriveAdvertData (DatabasOpperations DBOpp, String Username, String AdvertType){ 
    SQLiteDatabase SQDB = DBOpp.getReadableDatabase(); 
    String[] Coloumns = {TableData.TableInfo.USERNAME, TableData.TableInfo.ADVERT_NAME, TableData.TableInfo.ADVERT_EMAIL, TableData.TableInfo.ADVERT_ADDRESS, TableData.TableInfo.ADVERT_NUMBER, TableData.TableInfo.ADVERT_TYPE}; 
    String Where = TableData.TableInfo.USERNAME + " LIKE ? AND " + TableData.TableInfo.ADVERT_TYPE + " LIKE ?"; 
    String Argument[] = {Username, AdvertType}; 
    Cursor Cur = SQDB.query(TableData.TableInfo.TABLE_NAME2, Coloumns, Where, Argument, null, null, null); 
    return Cur; 
} 

需要的數據,那麼我調用另一個Java頁面

Context Contx = this; 
public DatabasOpperations DB = new DatabasOpperations(Contx); 
public Cursor Cur; 
Cur = DB.RetriveAdvertData(DB, DBUsername, "Restaurant"); 

String AdName = ""; 
String AdEmail = ""; 
String AdAddress = ""; 
String AdNumber = ""; 
String AdType = ""; 

if (Cur.moveToFirst()){ 
      do { 
       AdName = Cur.getString(Cur.getColumnIndex("AdvertsName")); 
       AdEmail = Cur.getString(Cur.getColumnIndex("AdvertEmail")); 
       AdAddress = Cur.getString(Cur.getColumnIndex("AdvertAddress")); 
       AdNumber = Cur.getString(Cur.getColumnIndex("AdvertNumber")); 
       AdType = Cur.getString(Cur.getColumnIndex("AdvertType")); 

       final String[] Adverts = new String[]{ 
         AdName, AdEmail, AdAddress, AdNumber, AdType 
       }; 

       ArrayAdapter setListAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, Adverts); 
       LV1.setAdapter(setListAdapter); 
      }while (Cur.moveToNext()); 
     } 
     Cur.close(); 

代碼的功能只顯示一個光標從數據庫中的行,我如何修改代碼以顯示數據庫中的所有行?

+0

如果只顯示一行,因爲要重設每個循環中的適配器。 –

+0

你有什麼建議來解決這個問題? – RexDough

+0

擺脫了嗎?並在 – RexDough

回答

0

儘量把setAdapter出while循環:

List<String[]> arr = new ArrayList<>(); 

    if (Cur.moveToFirst()) { 
     do { 
      AdName = Cur.getString(Cur.getColumnIndex("AdvertsName")); 
      AdEmail = Cur.getString(Cur.getColumnIndex("AdvertEmail")); 
      AdAddress = Cur.getString(Cur.getColumnIndex("AdvertAddress")); 
      AdNumber = Cur.getString(Cur.getColumnIndex("AdvertNumber")); 
      AdType = Cur.getString(Cur.getColumnIndex("AdvertType")); 

      final String[] Adverts = new String[]{ 
        AdName, AdEmail, AdAddress, AdNumber, AdType 
      }; 

      arr.add(Adverts) 
     } while (Cur.moveToNext()); 
    } 
    Cur.close(); 

    ArrayAdapter setListAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arr); 
    LV1.setAdapter(setListAdapter); 
+0

當我查看列表視圖時,它不顯示除[Ljava.lang.String'@32c6d061以外的任何其他任何線索爲什麼? – RexDough

+0

@RexDough Becuase'android.R.layout.simple_list_item_1'只是在其元素上調用'toString'。你可以看到[這篇文章](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4)以供參考。 –

+0

哦,好吧,所以它打印該字符串的整數值,我如何修改它來顯示數據而不是@ingerger字符串的值? – RexDough