2017-05-04 86 views
1

我想獲取數組中的值到文本視圖,我正在使用ViewFlipper,我想在圖像更改的同時更改文本視圖。如何在文本視圖中設置文本

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的是靜態的,在文本視圖中的文本是整個陣列,我只是想一個值,我正在考慮將這個數組與'我'從句子連接起來,我該怎麼做?

+0

是什麼類別的格式?數據類型? – Pehlaj

+0

爲什麼你需要將數組轉換爲字符串?只是傳遞你想在textView中設置的數組索引 –

+0

是類別簡單數組的數組列表? – Pehlaj

回答

1

以下嘗試,

info_text3.setText(categories[i]); 
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); 

添加自定義視圖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]); 
} 
+0

我以前試過這個,但它沒有工作,或者至少我雖然如此,它確實工作,但最後一個值保持在那裏,我什至不能看到其他人,怎麼可以我在與圖像同時改變它? – alb

+0

你在適配器中使用這個嗎?請分享完整的代碼,以確定最後的價值問題 – Pehlaj

+0

不,這是一個普通的視圖flipper – alb

相關問題