如何在数组中设置textview中的文本

时间:2017-05-04 09:45:25

标签: java android arrays textview flipview

我正在尝试将数组中的值添加到文本视图中,我使用的是ViewFlipper,我想在图像更改的同时更改textview。

for (int i = 0; i < imagens.length && i < categories.length; i++) {
    ImageView imageView = new ImageView(this);
    imageView.setImageResource(imagens[i]);
    simpleViewFlipper.addView(imageView);

    info_text3.setText(Arrays.toString(categories));
}

Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
simpleViewFlipper.setInAnimation(in);
simpleViewFlipper.setOutAnimation(out);
simpleViewFlipper.setFlipInterval(5000);
simpleViewFlipper.setAutoStart(true);

图像部分正在工作并且将文本设置为textview也正常工作,问题是textview是静态的,文本视图中的文本是整个数组而我只想要一个值,我想是链接&#39; i&#39;从句子来看,我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

尝试以下,

  info_text3.setText(categories[i]);

答案 1 :(得分:1)

您可以直接访问任何索引处的数组值

info_text3.setText(categories[i]);

1。使用如下所述的图片和文字视图创建布局:

<强> custom_view

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/element_spacing"
        android:text="Text" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/yourimage" />

</LinearLayout>

2。对该布局进行充气

View customView = activity.getLayoutInflater().inflate(R.layout.custom_view, null);

3. 将自定义视图添加到viewflipper。

simpleViewFlipper.addView(customView);

最终代码

for (int i = 0; i < imagens.length && i < categories.length; i++) {
    View customView = context.getLayoutInflater().inflate(R.layout.custom_view, null);
    TextView info_text3 = customView.findViewById(R.id.text);
    ImageView imageView = customView.findViewById(R.id.image);
    imageView.setImageResource(imagens[i]);
    simpleViewFlipper.addView(customView);

    info_text3.setText(categories[i]);
}
相关问题