Android – programmatically adding views to LinearLayout does not set text on everything except one
I have a linear layout and want to dynamically add a variable number of list items. (it cannot be the recycle bin view because the list is already in the recycle bin view and is considered a small list.) however, for some reasons, it does not save the text I set (except the last item). If I add another item with a different title, the last added item will be displayed, Other items will show "default". Does anyone know what happened? It's really strange, thank you!
This is the code:
settings_ item.xml
<?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="wrap_content"
android:orientation="horizontal">
<TextView
style="?android:listSeparatorTextViewStyle"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#00000000"
android:text="Default"
android:layout_weight="1"/>
<Check@R_809_2419@
android:id="@+id/check@R_809_2419@"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end|center_vertical"
android:layout_weight="0"/>
</LinearLayout>
root_ settings_ view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="5dp">
</LinearLayout>
SettingsView.java:
public class SettingsView extends LinearLayout {
//Views
private View view;
/**
* Constructor for android tools to use.
*/
public SettingsView(Context context) {
super(context);
}
/**
*
* @param parent
*/
public SettingsView(ViewGroup parent) {
super(parent.getContext());
//Get the inflated layout
LinearLayout view = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.root_settings_view, parent, false);
View itemView = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView.findViewById(R.id.title)).setText("Hi");
((Check@R_809_2419@) itemView.findViewById(R.id.check@R_809_2419@)).setChecked(true);
View itemView2 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView2.findViewById(R.id.title)).setText("Hello");
((Check@R_809_2419@) itemView2.findViewById(R.id.check@R_809_2419@)).setChecked(false);
View itemView3 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView3.findViewById(R.id.title)).setText("Bye");
((Check@R_809_2419@) itemView3.findViewById(R.id.check@R_809_2419@)).setChecked(true);
View itemView4 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView4.findViewById(R.id.title)).setText("Test");
((Check@R_809_2419@) itemView4.findViewById(R.id.check@R_809_2419@)).setChecked(false);
addView(view);
}
resolvent:
Thank you @ rod_ Algonquin's comments convinced me that a widget must be made for this. Fortunately, I have done widgets before, so I know what to do. I just want to avoid this situation, but if we don't use widgets, I think it will have problems with duplicate IDs
The following code makes it work:
Added a new class settingitemview.java:
/**
* A item view with the title and a check@R_809_2419@.
*/
public class SettingItemView extends LinearLayout {
private TextView mTitle;
private Check@R_809_2419@ mCheck@R_809_2419@;
public SettingItemView(Context context) {
super(context);
inflate(context, R.layout.settings_item, this);
mTitle = (TextView) findViewById(R.id.title);
mCheck@R_809_2419@ = (Check@R_809_2419@) findViewById(R.id.check@R_809_2419@);
}
public void setTitle(String title) {
mTitle.setText(title);
}
public void setCheck@R_809_2419@(boolean checked) {
mCheck@R_809_2419@.setChecked(checked);
}
}
This construction method has been changed:
/**
*
* @param parent
*/
public SettingsView(ViewGroup parent) {
super(parent.getContext());
//Get the inflated layout.
LinearLayout view = LayoutInflater.from(getContext()).inflate(R.layout.root_settings_view, parent, false);
SettingItemView itemView = new SettingItemView(parent.getContext());
itemView.setTitle("Hi");
itemView.setCheck@R_809_2419@(true);
view.addView(itemView);
SettingItemView itemView2 = new SettingItemView(parent.getContext());
itemView2.setTitle("Hello");
itemView2.setCheck@R_809_2419@(true);
view.addView(itemView2);
SettingItemView itemView3 = new SettingItemView(parent.getContext());
itemView3.setTitle("Bye");
itemView3.setCheck@R_809_2419@(true);
view.addView(itemView3);
addView(view);
}