2017-04-17 40 views
1

我有一個public class User它有一個字段int userID。多個User對象存儲在另一個類中,SharedFlat,存儲在ArrayList<User>中。在活動中,我想爲ArrayList<User>中的每個User對象創建並顯示CheckBox。由於ArrayList中的用戶數量(也就是CheckBox的數量)是可變的,因此我使用了for循環和setID()-方法將當前User對象的userID設置爲相應CheckBox的ID:從可變長度/字段的數組中獲取ID到類?

/* This method displays the CheckBoxes with the user names to the R.layout.new_payment_screen layout file */ 
private void createCheckboxes(SharedFlat flat) { 
    // Find the layout and create a Checkbox Array to store all users 
    LinearLayout ll = (LinearLayout) findViewById(R.id.who_received); 
    final CheckBox[] checkBoxes = new CheckBox[flat.getNumberOfUsers()]; 
    // Create a new Checkbox for each user and set the users name to it 
    for (int i = 0; i < checkBoxes.length; i++) { 
     // create new checkboxes 
     checkBoxes[i] = new CheckBox(this); 
     // add checkboxes to the layout 
     ll.addView(checkBoxes[i]); 
     // set text of the CheckBox to be userName of current User object 
     checkBoxes[i].setText(flat.getSpecificUserName(i)); 
     // set the ID to be the userID of the corresponding User 
     checkBoxes[i].setId(flat.getSpecificUserID(i)); 
    } 
} 

這似乎工作,CheckBoxes和相應的用戶名正確顯示。 (問:有沒有更好的解決辦法嗎?)

我們真正的問題:我有一個是應該檢查檢查其的CheckBox(與.isChecked())按鈕,因爲我後來有一個包含了該User對象上下工夫在調用按鈕的onClick()方法時已被「檢查」。 因爲我必須得到完整的User已檢查的對象,而不僅僅是它們的userID,有沒有辦法通過User的變量userID來得到User對象?有人會建議我如何能更好地解決這個問題嗎? 我想我找到了一種方法找到的檢查User對象的至少userIDs,但我不能肯定這是否會制定出對所有情景:

// Find the CheckBoxes within the LinearLayout and search them for the checked ones (= Receivers) 
    LinearLayout ll = (LinearLayout) findViewById(R.id.who_received); 
    final int checkBoxCount = ll.getChildCount(); 
    ArrayList<Integer> userIdList = new ArrayList<Integer>(); 
    for (int i = 0; i < checkBoxCount; i++) { 
     CheckBox currentCheckBox = (CheckBox) ll.getChildAt(i); 
     if (currentCheckBox.isChecked()) { 
      userIdList.add(i); 
     } 
    } 

或者,如果有人有一個完全不同的解決方案 - 我「只是」需要創建複選框並將ID附加到它們,但我不會有多少複選框將在那裏(取決於users的數量) 如果有人可以幫助我,那將會很棒。正如你所看到的,我對編程頗爲陌生 - 新生:-) 謝謝!

回答

0

我想我可能對你所說的你真正的問題是,即得到具體User對象「連接到」 CheckBox的情況下的解決方案。那麼,它實際上是通過在您的初始for循環中通過setTag()方法附加(不帶引號)對象到CheckBox。你說你在SharedFlat類中有User對象的列表。所以我假設你有(或可以添加)一個方法來得到User對象索引像flat.getUser(i)一樣。 然後,您可以在您的初始for循環中添加此行。

checkBoxes[i].setTag(flat.getUser(i)); 

所以它看起來像這樣。

/* This method displays the CheckBoxes with the user names to the R.layout.new_payment_screen layout file */ 
private void createCheckboxes(SharedFlat flat) { 
    // Find the layout and create a Checkbox Array to store all users 
    LinearLayout ll = (LinearLayout) findViewById(R.id.who_received); 
    final CheckBox[] checkBoxes = new CheckBox[flat.getNumberOfUsers()]; 
    // Create a new Checkbox for each user and set the users name to it 
    for (int i = 0; i < checkBoxes.length; i++) { 
     // create new checkboxes 
     checkBoxes[i] = new CheckBox(this); 
     // add checkboxes to the layout 
     ll.addView(checkBoxes[i]); 
     // set text of the CheckBox to be userName of current User object 
     checkBoxes[i].setText(flat.getSpecificUserName(i)); 
     // set the ID to be the userID of the corresponding User 
     checkBoxes[i].setId(flat.getSpecificUserID(i)); 
     checkBoxes[i].setTag(flat.getUser(i)); 
    } 
} 

這樣,就有效地附接一個整體User對象到相應CheckBox(而不是僅使用標識將它們連接起來),它可以在以後通過使用在每個CheckBoxgetTag()的方法檢索。我不確定您是否需要CheckBox的編號與userID完全匹配。

所以,你的下一段代碼想這

// Find the CheckBoxes within the LinearLayout and search them for the checked ones (= Receivers) 
LinearLayout ll = (LinearLayout) findViewById(R.id.who_received); 
final int checkBoxCount = ll.getChildCount(); 
ArrayList<User> users = new ArrayList<User>(); 
for (int i = 0; i < checkBoxCount; i++) { 
    CheckBox currentCheckBox = (CheckBox) ll.getChildAt(i); 
    if (currentCheckBox.isChecked()) { 
     users.add((User)currentCheckBox.getTag()); 
    } 
} 

而且你必須指定用戶,其相應的CheckBox ES已檢查User對象的列表。

+0

真棒,它的作品!用'setTag'和'getTag'方法的好主意,我還不知道。非常感謝你的幫助!! –

+0

P.S .:你的幫助爲我節省了15-20行代碼,包括一個非常臨時,混亂的方法。再次感謝,很棒的工作! :-) –

0

我認爲你的一切都在正確的軌道上。對於第一個問題,使用for-loop將成爲設置複選框的最佳方式。你可以做的一個優化是檢查當你顯示它們時檢查哪些是你的選項。否則,你可以檢查哪些盒子有檢查,並將這些ID放入一個數組中,然後根據你的User數組值檢查該ID。