IlyaGulya
IlyaGulya

Reputation: 967

Android how to fill layout with data

I have such layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<me.amasawa.studchat.views.MessageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test"
        android:layout_gravity="center" android:singleLine="false"
        android:autoText="false" android:background="@drawable/text_view_message" android:padding="1dp"
        android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="2dp"/>
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Это очень длинное сообщение для проверки разметки. Ну как?"
        android:layout_gravity="center"
        android:layout_toRightOf="@+id/textView" android:layout_alignBaseline="@+id/textView"
        android:layout_marginLeft="5dp"/>
</RelativeLayout>

I want to inflate it multiple times. How can I identify second TextView to fill it with data? Maybe something like this:

RelativeLayout tmp = getLayoutInflater().inflate(R.layout.message, null);
TextView tv = (TextView) tmp.findViewByID(...);
tv.setText(data);

But id is unique for all app. What should I use in this case?

Upvotes: 0

Views: 489

Answers (3)

If you use it in list view, you should call findViewById() in your getView() method

private String[] data={"string1","string2","string3"}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    convertView.findViewById(R.id.textView).setText(data[position]);
}

your output list would look like

string1
string2
string3

Upvotes: 0

Himanshu Dudhat
Himanshu Dudhat

Reputation: 1609

You can access that second textview using its parent id.

First you need to define id to that textview. Also you need to define its parent id when you are including it in main layout.

Then you can access that parent by that id.

Using that inflated parent you can inflate it's child using parentview.findviewbyid(R.id.textviewid);

Upvotes: 0

Bette Devine
Bette Devine

Reputation: 1194

When you will inflate your view, you will have a view as parent. Here, say e.g. parentView and say the id of textview above is textview then you can run the following code to identify the TextView as follows:-

TextView textView = (TextView)parentView.findViewById(R.id.textview);
textView.setText("Your desiredvalue");

Upvotes: 1

Related Questions