1

您好,我在ArrayList现在设置值如何将这些值存储在不同的文本视图中。

例如:

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.
}

请帮我找到这个。

4

7 回答 7

6

这是方法::

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));
}
于 2012-07-16T09:29:25.200 回答
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>
于 2012-07-16T09:32:21.027 回答
2

If u have the same no of text views and the data then create a array of views then use for loop as:

      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.
        }
于 2012-07-16T09:35:38.653 回答
1

然后你不需要for循环。只需手动将文本设置为您的文本视图

作为

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);
于 2012-07-16T09:26:33.097 回答
1

You could try something like this:

for(int i=0;i<mylist.size();i++)
{
    TextView tv = findViewById(getResources().getIdentifier("textView"+i, "id",getPackageName())); 
    tv.setText(mylist.get(i));
}

I'm not sure that is you want to do. I used the getIdentifier method to retrieve your textViews. Else, you can inflate your textViews in a loop:

for(int i=0;i<mylist.size();i++)
{
    TextView tv = (TextView)LayoutInflater.from(context).inflate(R.layout.myTextView, null); 
    tv.setText(mylist.get(i));
}

Hope this will help you

于 2012-07-16T09:35:08.083 回答
1

你不需要在这里循环

您需要调用textView.setText(mylist.get(0));...您需要调用此方法 10 次,并在 get 方法中增加值.. 并使用 diff 文本视图实例。

编辑:与您的评论一样:这里假设您已经创建

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));
   }
于 2012-07-16T09:23:42.297 回答
1
for(int i=0;i<mylist.size();i++)
{
  tv[i].setText(mylist.get(i));
}
于 2012-12-10T12:09:32.027 回答