0

I'm trying to generate a dynamic screen that contains a "textview", "edittext","button" and "listview". I'm trying to add what is written in the edittext to my listview. I'm not getting any errors and i can see added items inside my ArrayList when i debug the code. But listview shows only the very first item added into the list.

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

Is this because of the scrollview? I'm adding my controls as children of a linear layout but i'm stuck...Any help will be appreciated...Thanks...

Emre Çınar
  • 30
  • 2
  • 9
  • 1
    Almost certainly, yes. ScrollView and ListView don't co-exist well. http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a-scrollview-without-it-collapsing – NigelK Dec 10 '13 at 15:23

1 Answers1

1

Remove your scroll view, a listview already implements a scrollview.

Alright, so I assume you have an activity, some part of that activity has a listview, you want to scroll on the listview and also scroll the whole activity.

You can do it by creating the listview in another Relative Layout (or Linear layout). Let me write an example for you in 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>

Does this make more sense now?

Akshat Agarwal
  • 2,837
  • 5
  • 29
  • 49
  • Thanks bro but how to scroll the whole screen after removing the scrollview?As you see i'm trying to generate a dynamic screen. I should be able to scroll the screen when it grows up? – Emre Çınar Dec 10 '13 at 16:46