0

这有点难以解释,这可能是我到目前为止还没有在网上找到任何解决方案的原因......

我有一个包含两个变量的类 - 一个字符串和一个字符串的 LinkedList:

String name;
LinkedList<String> stringlist;

stringlist 通常包含 1-4 个字符串。现在我想创建一个 Activity,它显示此类对象的内容并动态显示 EditText 字段,具体取决于 LinkedList 中的值的数量。

当列表中有 4 个值时,我想它看起来像这样:

Title: <name>
=============
Value: <stringlist[0]>
Value: <stringlist[1]>
Value: <stringlist[2]>
Value: <stringlist[3]>

...如果只有一个值,就像这样:

Title: <name>
=============
Value: <stringlist[0]>

如何定义我的布局 xml 文件才能做到这一点?我是否只在我的布局文件和代码中声明我的“TextView-EditText-Combination”一次以重新打印它,直到它与 stringlist.length() 匹配?我可以以某种方式对整个布局块进行这种重复吗?

4

1 回答 1

0
// try this way hope this will help you solve your problem.

我的.xml

    <EditText
        android:id="@+id/edtTitle"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:hint="Title"/>

    <LinearLayout
        android:id="@+id/lnrDynamicString"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_marginTop="5dp"
        android:orientation="vertical">

    </LinearLayout>

</LinearLayout>

我的活动

public class MyActivity extends Activity {

    My my;
    private EditText edtTitle;
    private LinearLayout lnrDynamicString;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my);

        edtTitle =(EditText) findViewById(R.id.edtTitle);
        lnrDynamicString=(LinearLayout)findViewById(R.id.lnrDynamicString);

        my = new My();
        my.setTitle("MyTitle");
        LinkedList<String> tempString = new LinkedList<String>();
        tempString.add("Child1");
        tempString.add("Child2");
        tempString.add("Child3");
        tempString.add("Child4");
        tempString.add("Child5");
        my.setStringlist(tempString);

        edtTitle.setText(my.getTitle());
        for (int i=0;i<my.getStringlist().size();i++){
            EditText edtChild = new EditText(this);
            edtChild.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            edtChild.setId(i);
            edtChild.setText(my.getStringlist().get(i));
            lnrDynamicString.addView(edtChild);
        }


    }

}

**My**
public class My {
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    private String title;

    public LinkedList<String> getStringlist() {
        return stringlist;
    }

    public void setStringlist(LinkedList<String> stringlist) {
        this.stringlist = stringlist;
    }

    private LinkedList<String> stringlist;
}
于 2013-11-07T04:19:14.327 回答