【发布时间】:2013-12-10 15:16:43
【问题描述】:
我正在尝试生成一个包含“textview”、“edittext”、“button”和“listview”的动态屏幕。我正在尝试将 edittext 中写入的内容添加到我的列表视图中。我没有收到任何错误,当我调试代码时,我可以在我的 ArrayList 中看到添加的项目。但是 listview 只显示添加到列表中的第一个项目。
LayoutParams layoutMatchParent = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutParams layoutWrapContent = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutParams layoutMatchParentWidth = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ScrollView scrollView = new ScrollView(this);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(linearLayout);
setContentView(scrollView, layoutMatchParent);
LinearLayout vgMul=new LinearLayout(this);
vgMul.setOrientation(LinearLayout.VERTICAL);
TextView tvMul=new TextView(this);
tvMul.setText("Some Label");
tvMul.setId(1);
tvMul.setLayoutParams(layoutWrapContent);
vgMul.addView(tvMul);
EditText edtMul=new EditText(this);
edtMul.setId(2);
edtMul.setLayoutParams(layoutMatchParentWidth);
vgMul.addView(edtMul);
Button btnAddMul=new Button(this);
btnAddMul.setText("Add");
btnAddMul.setLayoutParams(layoutMatchParentWidth);
vgMul.addView(btnAddMul);
ListView lvMul =new ListView(this);
lvMul.setId(3);
lvListItems =new ArrayList<String>();
lvArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lvListItems);
lvMul.setAdapter(lvArrayAdapter);
lvMul.setScrollContainer(false);
vgMul.addView(lvMul);
btnAddMul.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
EditText edtMul=(EditText)findViewById(2);
lvListItems.add(edtMul.getText().toString());
lvArrayAdapter.notifyDataSetChanged();}
});
linearLayout.addView(vgMul);
这是因为滚动视图吗?我将我的控件添加为线性布局的子级,但我被卡住了...任何帮助将不胜感激...谢谢...
【问题讨论】:
-
几乎可以肯定,是的。 ScrollView 和 ListView 不能很好地共存。 stackoverflow.com/questions/3495890/…
标签: android listview android-listview