線形レイアウトがあり、可変数のリストアイテムを動的に追加したいと思います。(このリストはすでにリサイクラービューにあり、小さなリストであると想定されているため、リサイクラービューにすることはできません。)しかし、何らかの理由で、最後の項目を除いて設定したテキストが保存されません。別のタイトルで別のアイテムを追加した場合、最後に追加されたアイテムが表示され、他のアイテムには「デフォルト」と表示されます。誰かが何が起こっているのか知っていますか?本当に不思議です。ありがとう!
コードは次のとおりです。
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);
}
@Rod_Algonquinのコメントに感謝します。これにより、ウィジェットを作成する必要があると確信しました。幸いなことに、私は以前にウィジェットを作成したことがあるので、その方法を知っていました。私はそれを避けようとしていましたが、ウィジェットを使用しないと、IDの重複に問題があると思います。
次のコードはそれを機能させました:
新しいクラス、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);
}
}
このコンストラクタメソッドを変更しました:
/**
*
* @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);
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加