以编程方式将视图添加到 LinearLayout,而不是在除一个以外的所有视图上设置文本

Adding views to LinearLayout programmatically not setting text on all but one

我有一个线性布局,想动态添加可变数量的列表项。 (不能是recycler view,因为这个list已经在recycler view了,假设是一个小list。)但是不知为什么,除了最后一项,它没有保存我设置的文本。如果我添加了另一个标题不同的项目,那么将显示最后添加的项目,而其他项目将显示 "default"。有谁知道发生了什么事?这真的很奇怪。谢谢!

代码如下:

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);
    }

你基本上已经 运行 完成了,只添加了最后一个,这段代码应该可以工作:

public SettingsView(ViewGroup parent) { 超级(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);

谢谢@Rod_Algonquin 的评论,这让我确信我必须为此制作一个小部件。幸运的是我以前做过小部件,所以我知道该怎么做。我只是想避免它,但我想如果我们不使用小部件,它就会出现重复 ID 的问题。

以下代码使其工作:

添加了新的 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);
    }
}

更改了此构造方法:

/**
 *
 * @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);
    }