2012-10-18 52 views
0

從數據庫到ArrayList添加不同的對象,這是我使用的代碼:將對象添加到ArrayList失敗。始終拷貝的相同

try { 
    Statement stat = con.createStatement(); 
    ResultSet rs = stat.executeQuery("SELECT DISTINCT Id, Username, FirstName, LastName FROM USER WHERE Username LIKE '%"+name+"%' or FirstName LIKE '%"+name+"%' or LastName LIKE '%"+name+"%'"); 
    while (rs.next()){ 
     finded.setUsername(rs.getString("Username")); 
     finded.setFirstName(rs.getString("FirstName")); 
     finded.setLastName(rs.getString("LastName")); 
     finded.setId(rs.getInt("Id")); 
     findList.add(finded); 
     Log.d("this is the content of the List:",((Integer) findList.get(j).getId()).toString()); 
     Log.d("this is the content of the List 0:",((Integer) findList.get(0).getId()).toString()); 

    } 
    con.close(); 

這是什麼Log.d給了我各一次。

10-18 08:31:39.407: D/this is the content of the List:(427): 3 
10-18 08:31:39.407: D/this is the content of the List 0:(427): 3 
10-18 08:31:39.427: D/this is the content of the List:(427): 15 
10-18 08:31:39.427: D/this is the content of the List 0:(427): 15 
10-18 08:31:39.437: D/this is the content of the List:(427): 13 
10-18 08:31:39.437: D/this is the content of the List 0:(427): 13 
10-18 08:31:39.447: D/this is the content of the List:(427): 50 
10-18 08:31:39.447: D/this is the content of the List 0:(427): 50 
10-18 08:31:39.460: D/this is the content of the List:(427): 34 
10-18 08:31:39.460: D/this is the content of the List 0:(427): 34 
10-18 08:31:39.467: D/this is the content of the List:(427): 49 
10-18 08:31:39.467: D/this is the content of the List 0:(427): 49 
10-18 08:31:39.479: D/this is the content of the List:(427): 53 
10-18 08:31:39.479: D/this is the content of the List 0:(427): 53 

正如你可以看到它拷貝的相同物體在ArrayList的所有位置。我也試圖用findList.add(index, finded)index是一個int誰正在遞增)但我得到相同的結果。

+0

Heiko的答案似乎正確。如果一次又一次添加SAME對象,那麼當您設置用戶名時,您可以在添加此對象的任何地方設置它。這就是爲什麼每次插入時都需要創建一個NEW對象,如Heiko所述。 –

回答

4

你需要創建一個新的對象finded循環內

while (rs.next()){ 
    finded = new Finded(); // <<-- here 
    finded.setUsername(rs.getString("Username")); 

否則你只是在添加相同的對象,並再次

0

也許你可以使用「group by」而不是「distinct」。

select .... from ... group by ...

試試這個怎麼樣?

+0

omg ..我犯了一個大錯誤....大聲笑 –

2

更改爲以下幾點:

int j=0; 
try { 
       Statement stat = con.createStatement(); 
       ResultSet rs = stat.executeQuery("SELECT DISTINCT Id, Username, FirstName, LastName FROM USER WHERE Username LIKE '%"+name+"%' or FirstName LIKE '%"+name+"%' or LastName LIKE '%"+name+"%'"); 
       while (rs.next()){ 
        Finded finded=new Finded(); 
        finded.setUsername(rs.getString("Username")); 
        finded.setFirstName(rs.getString("FirstName")); 
        finded.setLastName(rs.getString("LastName")); 
        finded.setId(rs.getInt("Id")); 
        findList.add(finded); 
        Log.d("this is the content of the List:",((Integer) findList.get(j).getId()).toString()); 
        Log.d("this is the content of the List 0:",((Integer) findList.get(0).getId()).toString()); 
j++; 
       } 
       con.close(); 
+0

我投這個答案,因爲不僅所有的列表條目指向同一個對象,j沒有增加,所以你繼續顯示條目[0]。 – StevieB

1

你查詢正在返回多個記錄,但是,您正在重寫finded對象,而不是在循環內創建一個新對象。 在您的循環內創建一個新對象

Finded finded =null; 
while (rs.next()){ 
        finded = new Finded(); 
        finded.setUsername(rs.getString("Username")); 
        finded.setFirstName(rs.getString("FirstName")); 
        finded.setLastName(rs.getString("LastName")); 
        finded.setId(rs.getInt("Id")); 
        findList.add(finded); 


       }