Reputation: 13933
I have a template layout file called
template_dashboard_item.xml
its contents are
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/DashboardItemContainer">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" style="@style/DashboardItem">
<TextView style="@style/DashboardItemText" android:text="Application for Jane Doe has been added" android:textColor="@color/black" />
<TextView style="@style/DashboardItemText" android:text="Added today at ..." android:layout_marginTop="10dp" />
</LinearLayout>
I am using this template layout by inflating it and inserting it into a view like so
LinearLayout item = (LinearLayout) getLayoutInflater().inflate(R.layout.template_dashboard_item, items, false);
items.addView(item);
Like that, I am inserting it multiple times.
I would like to know, before I insert it (addView) how to I update the TextViews (text) that are there ?
In other words how do I fetch the objects for the TextView ? (I guess findViewById would not work in this case).
Upvotes: 0
Views: 1338
Reputation: 5618
Why dont you create a custom layout like this
public class CustomView extends RelativeLayout {
private TextView header;
private TextView description;
public Card(Context context) {
super(context);
init();
}
public Card(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public Card(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
inflate(getContext(), R.layout.view_layout, this);
this.header = (TextView) findViewById(R.id.header);
this.description = (TextView) findViewById(R.id.description);
}
public void setHeader(String header) {
this.header.setText(header);
}
public void setDescription(String description) {
this.description.setText(description);
}
}
And to add the layout
LinearLayout items = (LinearLayout) getLayoutInflater().inflate(R.layout.template_dashboard_item, items, false);
CustomView item = new CustomView(this);
item.setHeader("xxx);
item.setDescription("yyy");
items.addView(item);
Upvotes: 1
Reputation: 157457
you can either assign an id to the TextView
s and use item.findViewById
to retrieve it, or use ViewGroup.getChildAt(int index)
for (int i = 0 i < item.getChildCount(); i++) {
View view = item.getChildAt(i);
if (view instanceof TextView) {
}
}
Upvotes: 1
Reputation: 5146
Why can't you add an id to your two text views in the XML and use TextView text = item.findViewById(R.id.your_id);
Upvotes: 2