2016-05-18 2 views
0

template.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:layout_width="match_parent" 
    android:layout_below="@+id/header" 
    android:orientation="horizontal" 
    android:id="@+id/template_linear_layout_container" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <LinearLayout 
     android:id="@+id/template_linear_layout"> 
     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/template_title_TV" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="ALL" 
     </RelativeLayout> 
    </LinearLayout> 
</LinearLayout> 

Я создал TextView внутри template.xml и я заселена эту линейную компоновку template.xml внутри parent_view с помощью Inflater.Как установить разные тексты в TextViews, заполненные надувом?

for(int i=0;i<3;i++){ 
    View container_template = getLayoutInflater().inflate(R.layout.templates, parent_layout, false); 
    parent_layout.addView(container_template); 
    TextView title_tv_template = (TextView) findViewById(R.id.template_title_TV); 
    title_tv_template.setText(1+i+"text changed "); 
} 

Я хочу изменить текст каждого заполненного текста, но над кодом изменяется текст только с первого текста.

+0

и 'ID' это число, так как они всегда указывают на тот же идентификатор, то его всегда тот же ресурс (а TextView), который быть целенаправленным. Вам либо нужен идентификатор для каждого разного 'View', либо иметь один экземпляр внутри другого уникального' View' .. так что «mainLayout» с 3 «LinearLayout» (a, b, c), а затем с помощью 'mainLayout. a.title', 'mainLayout.b.title',' mainLayout.c.title' – Bonatti

ответ

0

Как Bonatti отметил, findViewById() цели тот же ресурс три раза. Таким образом, вместо того, чтобы доступ к template_title_TV непосредственно, я к нему доступ через container_template зрения:

TextView title_tv_template = (TextView)container_template.findViewById(R.id.template_title_TV); 
0

Приведенный выше код не так, вы должны сначала установить текст и добавить его в родительское представление следующий ..

for(int i=0;i<3;i++){ 
    View container_template = getLayoutInflater().inflate(R.layout.templates, parent_layout, false); 
    TextView title_tv_template = (TextView) container_template. findViewById(R.id.template_title_TV); 
    title_tv_template.setText(1+i+"text changed "); 
    parent_layout.addView(container_template); 
} 

Этот код будет работать ..

+0

Нет, это не сработало. – Prince