Reputation: 9576
I have a linear layout and want to dynamically add a variable number of list items. (It can't be a recycler view because this list is already in a recycler view and is assumed to be a small list.) But for some reason, it does not save the text I set except for the last item. If I added another item with a different title, then the last one added will be shown, and the others will say "default". Does anyone know what is going on? It's really strange. Thanks!
Here 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"/>
<CheckBox
android:id="@+id/checkbox"
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");
((CheckBox) itemView.findViewById(R.id.checkbox)).setChecked(true);
View itemView2 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView2.findViewById(R.id.title)).setText("Hello");
((CheckBox) itemView2.findViewById(R.id.checkbox)).setChecked(false);
View itemView3 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView3.findViewById(R.id.title)).setText("Bye");
((CheckBox) itemView3.findViewById(R.id.checkbox)).setChecked(true);
View itemView4 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView4.findViewById(R.id.title)).setText("Test");
((CheckBox) itemView4.findViewById(R.id.checkbox)).setChecked(false);
addView(view);
}
Upvotes: 3
Views: 6386
Reputation: 9576
Thank you @Rod_Algonquin for your comment, which convinced me that I have to make a widget for this. Luckily I had done widgets before, so I knew how to do it. I just was trying to avoid it, but I guess it has problems with the duplicate IDs if we do not use widgets.
The following code made it work:
Added new class, SettingItemView.java:
/**
* A item view with the title and a checkbox.
*/
public class SettingItemView extends LinearLayout {
private TextView mTitle;
private CheckBox mCheckBox;
public SettingItemView(Context context) {
super(context);
inflate(context, R.layout.settings_item, this);
mTitle = (TextView) findViewById(R.id.title);
mCheckBox = (CheckBox) findViewById(R.id.checkbox);
}
public void setTitle(String title) {
mTitle.setText(title);
}
public void setCheckbox(boolean checked) {
mCheckBox.setChecked(checked);
}
}
Changed this constructor method:
/**
*
* @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.setCheckbox(true);
view.addView(itemView);
SettingItemView itemView2 = new SettingItemView(parent.getContext());
itemView2.setTitle("Hello");
itemView2.setCheckbox(true);
view.addView(itemView2);
SettingItemView itemView3 = new SettingItemView(parent.getContext());
itemView3.setTitle("Bye");
itemView3.setCheckbox(true);
view.addView(itemView3);
addView(view);
}
Upvotes: 3
Reputation: 625
You are basically running over it and adding only the last one , this code should work:
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");
((CheckBox) itemView.findViewById(R.id.checkbox)).setChecked(true);
addView(view);
View itemView2 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView2.findViewById(R.id.title)).setText("Hello");
((CheckBox) itemView2.findViewById(R.id.checkbox)).setChecked(false);
addView(view);
View itemView3 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView3.findViewById(R.id.title)).setText("Bye");
((CheckBox) itemView3.findViewById(R.id.checkbox)).setChecked(true);
addView(view);
View itemView4 = LinearLayout.inflate(parent.getContext(), R.layout.settings_item, view);
((TextView) itemView4.findViewById(R.id.title)).setText("Test");
((CheckBox) itemView4.findViewById(R.id.checkbox)).setChecked(false);
addView(view);
Upvotes: 1