0

I'm trying to create an app where there is a ScrollView taking up part of a screen, and I've added a linearlayout within that, but I need to dynamically add rows of views (a textView, 2 editText views, and a checkbox on each row) and be able to pull data from those to be put into a database.

I'm pretty new to Android development, so I'm really not sure how to go about doing this, any help would be appreciated.

2 Answers 2

3

Create this layout xml (arrange the way you want) say item_layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView2" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText" />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editText2" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New CheckBox"
    android:id="@+id/checkBox" />
</LinearLayout>

Adding views

LayoutInflater inflater = (LayoutInflater)context.getSystemService
      (Context.LAYOUT_INFLATER_SERVICE);
LinearLayout scrollViewLinearlayout = (LinearLayout)findViewById(R.id.scroll_view_linear_layout); // The layout inside scroll view
for(int i = 0; i < count; i++){
    LinearLayout layout2 = new LinearLayout(context);
    layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    View item = inflater.inflate(R.layout.item_layout, null, false);
    layout2.setId(i);
    layout2.addView(item);
    scrollViewLinearlayout.addView(layout2);    
}

Getting values from the view

for(int i = 0; i < count; i++){
    LinearLayout itemLl = (LinearLayout)scrollViewLinearlayout.findViewById(i);
    EditText et1 = (EditText)itemLl.findViewById(R.id.editText);
    String et1Str = et1.getText().toString();
    // Similarly get other values and add them in the database
}
Sign up to request clarification or add additional context in comments.

9 Comments

how can you access layout2 from outside the block of for loop?scrollViewLl .addView(layout2)
further scrollViewLl .addView(layout2) should be inside loop right?
I think you also need to change type of scrollViewLl to ScrollView.Thats what the user intend to do.
No. scrollViewLl is the linear layout inside the scroll view. You can't have more than one child in a scrollview. Thats why this linear layout is used.
Ok. but try to use meaningfull names such as wrappers instead of scrollview1.
|
-2

Use a ListView instead of a ScrollView, and dymanically add/remove elements.

Once you learn about ListView you'll find how to do this quite easy:

https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

1 Comment

This was helpful thanks! I might have to use a ListView or GridView to do what I want instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.