1

I am trying to get the values I have in an array to a text view, I am using a ViewFlipper and I would like to change the textview at the same time the image changes.

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);

The image part is working and setting the text to the textview is also working, the problem is that the textview is static and that the text in the text view is the entire array and I just want one value, I was thinking linking the array with the 'i' from the for sentence, how can I do this?

4
  • What is the format of categories? Data type? Commented May 4, 2017 at 9:46
  • Why would you need to convert array into string? just pass the array index of which you want to set in textView Commented May 4, 2017 at 9:47
  • Is categories a array list of simple array? Commented May 4, 2017 at 9:47
  • It's a array of strings Commented May 4, 2017 at 9:51

2 Answers 2

1

Try below,

  info_text3.setText(categories[i]);
Sign up to request clarification or add additional context in comments.

Comments

1

You can directly access arrays values at any index

info_text3.setText(categories[i]);

1. Create a layout with an image and a text view as mentioned below:

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. Inflate that layout

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

3. Add custom view to viewflipper.

simpleViewFlipper.addView(customView);

FINAL CODE

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]);
}

7 Comments

I've tried this before, but it wasn't working or at least I though so, it does work but the last value stays there and I don't even get to see the others, how can I make it change at the same time as the image?
Are you using this in adapter? Please share the full code to identify last value issue
yes, I'm doing that right now (see anddevmanu answer) but the textview isn't showing up only the image, I didn't noticed you'de edited your code
Please see the updated code (step by step). You can directly run the FINAL CODE in the answer
It doesn't let inflate the layout, it says 'Cannot resolve method getLayoutInflater()'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.