动态添加的ListView仅显示第一个项目

时间:2013-12-10 15:16:44

标签: android listview android-listview

我正在尝试生成一个包含“textview”,“edittext”,“button”和“listview”的动态屏幕。我正在尝试将edittext中写入的内容添加到listview中。我没有收到任何错误,我可以在调试代码时看到我的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吗?我正在添加我的控件作为线性布局的孩子,但我被卡住...任何帮助将不胜感激...谢谢......

1 个答案:

答案 0 :(得分:1)

删除滚动视图,列表视图已实现滚动视图。

好吧,所以我假设你有一个活动,该活动的某些部分有一个列表视图,你想滚动列表视图并滚动整个活动。

您可以通过在另一个相对布局(或线性布局)中创建列表视图来实现。让我在xml中为你写一个例子

    <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

        <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
                 <RelativeLayout
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content" >
        //Your text views and other stuff goes here


                 </RelativeLayout>
        </ScrollView>


    <RelativeLayout
        android:id="@+id/SecondRelativeLayoutDetailedPage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:background="@color/holoActionBarColor">

            <ListView
                android:id="@+id/myRewardsActiveList"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/myActiveRewardsHeadingLinLayout">
            </ListView>        

    </RelativeLayout>
</RelativeLayout>

这现在更有意义了吗?

相关问题