您好我在ArrayList
设置值现在我如何将这些值存储在不同的textview中。
例如:
ArrayList<String> mylist =new ArrayList<String>();
for(int i=0;i<mylist.size();i++)
{
//Here mylist contains 10 values and I have 10 different textviews. Now How can i add values 1 to 10 in different textview. set value to First textview 1,second textview to 2 etc.
}
请帮我找到这个。
答案 0 :(得分:5)
这就是::
TextView []tv=new TextView[10];
tv[0]=(TextView)findViewById(R.id.____);
tv[1]=(TextView)findViewById(R.id.____);
tv[2]=(TextView)findViewById(R.id.____);
tv[3]=(TextView)findViewById(R.id.____);
tv[4]=(TextView)findViewById(R.id.____);
tv[5]=(TextView)findViewById(R.id.____);
tv[6]=(TextView)findViewById(R.id.____);
tv[7]=(TextView)findViewById(R.id.____);
tv[8]=(TextView)findViewById(R.id.____);
tv[9]=(TextView)findViewById(R.id.____);
for(int i=0;i<mylist.size();i++)
{
tv[i].setText(mylist.get(i));
}
答案 1 :(得分:4)
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
ArrayList<String> mylist = new ArrayList<String>();
mylist.add("1");
mylist.add("2");
mylist.add("3");
mylist.add("4");
LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
for (int i = 0; i < mylist.size(); i++)
{
TextView tv = new TextView(this);
tv.setLayoutParams(lparams);
tv.setText(mylist.get(i));
layout.addView(tv);
}
和XML文件
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
答案 2 :(得分:2)
如果你有相同的no文本视图,然后数据创建一个视图数组,那么使用for循环:
ArrayList<String> mylist =new ArrayList<String>();
mylist.add("1st");
mylist.add("2nd");
mylist.add("3rd");
mylist.add("4th");
// mylist.add("5th");
one=(TextView)findViewById(R.id.textView1);
two=(TextView)findViewById(R.id.textView2);
three=(TextView)findViewById(R.id.textView3);
four=(TextView)findViewById(R.id.textView4);
ArrayList<TextView>text=new ArrayList<TextView>(Arrays.asList(one,two,three,four));
for(int i=0;i<mylist.size();i++)
{
text.get(i).setText(mylist.get(i));
//Here mylist contains 10 values and I have 10 different textviews. Now How can i add values 1 to 10 in different textview. set value to First textview 1,second textview to 2 etc.
}
答案 3 :(得分:1)
你不需要在这里循环
你需要调用textView.setText(mylist.get(0));
...你需要在get方法中增加值,并使用diff文本视图实例调用它10次。
编辑:与您的评论相同:此处假设您已创建
TextView [] tv; // you need to initialized array here with textviews
ArrayList<String> mylist =new ArrayList<String>();
for(int i=0;i<mylist.size();i++)
{
tv[i].setText(mylist.get(i));
}
答案 4 :(得分:1)
然后你不需要for循环。只需手动将文本设置为您的文本视图
as
ArrayList<String> mylist =new ArrayList<String>();
TextView txt1 = (TextView)findviewbyId(R.id.txt1);
txt1.setText(mylist.get(0);
....//upto 10 textViews
TextView txt10 = (TextView)findviewbyId(R.id.txt10);
txt10.setText(mylist.get(9);
答案 5 :(得分:1)
您可以尝试这样的事情:
for(int i=0;i<mylist.size();i++)
{
TextView tv = findViewById(getResources().getIdentifier("textView"+i, "id",getPackageName()));
tv.setText(mylist.get(i));
}
我不确定你是不是想做。我使用getIdentifier
方法来检索textViews。
否则,您可以循环扩展textView:
for(int i=0;i<mylist.size();i++)
{
TextView tv = (TextView)LayoutInflater.from(context).inflate(R.layout.myTextView, null);
tv.setText(mylist.get(i));
}
希望这会对你有所帮助
答案 6 :(得分:1)
for(int i=0;i<mylist.size();i++)
{
tv[i].setText(mylist.get(i));
}