Java 遍历列表android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18556180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
looping through List android
提问by Rakeeb Rajbhandari
I have the following method:
我有以下方法:
public List<String> getAllValue(){
List<String> list = new ArrayList<String>();
if(pref.getString(KEY_NUMBER1 , "").length()>2)
list.add(pref.getString(KEY_NUMBER1 , ""));
if(pref.getString(KEY_NUMBER2 , "").length()>2)
list.add(pref.getString(KEY_NUMBER2 , ""));
if(pref.getString(KEY_NUMBER3 , "").length()>2)
list.add(pref.getString(KEY_NUMBER3 , ""));
if(pref.getString(KEY_NUMBER4 , "").length()>2)
list.add(pref.getString(KEY_NUMBER4 , ""));
if(pref.getString(KEY_NUMBER5 , "").length()>2)
list.add(pref.getString(KEY_NUMBER5 , ""));
return list;
}
What I need to do now is to assign these numbers(like KEY_NUMBER1
) to the following editTexts
:
我现在需要做的是将这些数字(如KEY_NUMBER1
)分配给以下内容editTexts
:
EditText phoneNumber1, phoneNumber2, phoneNumber3, phoneNumber4, phoneNumber5;
Being new to working with Lists, I am having a hard time trying to figure out a way to loop through and assign values to these editTexts, like
作为使用 Lists 的新手,我很难想出一种方法来循环遍历这些 editTexts 并为其分配值,例如
phoneNumber1.setText(KEY_NUMBER1);
phoneNumber2.setText(KEY_NUMBER2);
phoneNumber3.setText(KEY_NUMBER3);
采纳答案by Shobhit Puri
Assuming list
is your List<String>
retuned from the function. You may loop over it like:
假设list
是您List<String>
从功能中重新调整的。你可以像这样循环:
for (int i=0; i<list.size(); i++) {
System.out.println(list.get(i));
}
For assigning the EditText, you can just use the index, if you the number of items and it is fixed( which seem to be 5 here):
对于分配 EditText,您可以只使用索引,如果您的项目数是固定的(这里似乎是 5):
phoneNumber1.setText(list.get(0));
phoneNumber2.setText(list.get(1));
//so on ...