I'm trying to find a way to create a custom views dynamically, but I didn't find a useful post for my problem.
I have the next view.
And this is the tree of the layout:
I want to create as many copies as items received, but I need to change the textviews inside per each item.
I don't know If I have explained me well. Thank you.
EDIT:
I follow some of your advises, and I make this:
private void addInputHeader(int timestamp)
{
LinearLayout mRootLayout = (LinearLayout) findViewById(R.id.event_container);
View mChildView = getLayoutInflater().inflate(R.layout.program_event_day_header, mRootLayout, false);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mRootLayout.addView(mChildView, params);
TextView tv_day = (TextView) mChildView.findViewById(R.id.tv_day);
tv_day.setText(getDayForHeader(timestamp) + getResources().getString(R.string.of) + getMonthForHeader(timestamp));
}
private void addInputDescription(int timestamp, String description)
{
LinearLayout mRootLayout = (LinearLayout) findViewById(R.id.event_container);
View mChildView = getLayoutInflater().inflate(R.layout.program_event_day_header, mRootLayout, false);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mRootLayout.addView(mChildView, params);
TextView tv_event_hour = (TextView) mChildView.findViewById(R.id.tv_event_hour);
TextView tv_event_message = (TextView) mChildView.findViewById(R.id.tv_event_message);
tv_event_hour.setText(getHour(timestamp) + " - ");
tv_event_message.setText(description);
}
private String getDayForHeader(long timeStamp){
Date df = new Date(timeStamp*1000);
return new SimpleDateFormat("cccc dd").format(df);
}
private String getMonthForHeader(long timeStamp){
Date df = new Date(timeStamp*1000);
return new SimpleDateFormat("MM").format(df);
}
private String getHour(long timeStamp){
Date df = new Date(timeStamp*1000);
return new SimpleDateFormat("HH:mm").format(df);
}
private int getD(long timeStamp){
Date df = new Date(timeStamp*1000);
return Integer.parseInt(new SimpleDateFormat("dd").format(df));
}
private int getM(long timeStamp){
Date df = new Date(timeStamp*1000);
return Integer.parseInt(new SimpleDateFormat("MM").format(df));
}
private int getY(long timeStamp){
Date df = new Date(timeStamp*1000);
return Integer.parseInt(new SimpleDateFormat("yyyy").format(df));
}
But I'm getting a NullPointerException when the addInputDescription tries to set text on tv_event_hour.
Create an VO for your items and add it in to the layout. Like, layout.add(item)
See the following code if you're trying to add a child view (or multiple) to your root LinearLayout
:
LinearLayout mRootLayout = (LinearLayout) findViewById(R.id.ll_root);
View mChildView = getLayoutInflater().inflate(R.layout.ll_child, mRootLayout,
false);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
mRootLayout.addView(mChildView, params);
TextView tvEventHour = (TextView) mChildView.findViewById(R.id.tv_event_hour);
TextView tvEventMessage = (TextView) mChildView.findViewById(R.id.tv_event_message);
tvEventHour.setText("10:30");
tvEventMessage.setText("Fugia corum...");
Note: If the list is about to get very long you should consider using a ListView with an Adapter since this is alot better performance-wise.
// Add row view layout that represent ui row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/leftTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left Top" />
<TextView
android:id="@+id/RightTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right Top" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/leftBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left Bottom" />
<TextView
android:id="@+id/RightBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right Bottom" />
</LinearLayout>
</LinearLayout>
// Add view as row with msg
private void addMsg() {
View msgView = getViewWithMsg("TopLeft", "TopRight", "LeftBottom", "Right Bottom");
// msgView represent one row
// You can add it to linearlayout desendent to scrollview
}
// This method create one msg row layout and update msg
private View getViewWithMsg(String TopLeft, String TopRight, String LeftBottom,
String RightBottom) {
LayoutInflater inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View root = inflator.inflate(R.layout.row, null);
TextView topLeft = (TextView) root.findViewById(R.id.leftTop);
TextView topRight = (TextView) root.findViewById(R.id.RightTop);
TextView bottomLeft = (TextView) root.findViewById(R.id.leftBottom);
TextView botomRight = (TextView) root.findViewById(R.id.RightBottom);
topLeft.setText(TopLeft);
topRight.setText(TopRight);
bottomLeft.setText(LeftBottom);
botomRight.setText(RightBottom);
return root;
}
You can inflate from xml:
View placeHolderView = inflater.inflate(R.layout.view_header_placeholder, mListView);
What do you mean by "change the textviews inside per each item."? If you need the textview inside the inflated layout try this:
TextView myTextView = (TextView)placeHolderView.findViewById(R.id.my_text_view);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.