Search code examples
androidlistviewlayout

Which listview is better?


I created a list using scrollview+linearlayout. I am creating customviews inside linear layout, here is my code:

list.xml:

 <ScrollView
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:id="@+id/scroll_container"
            android:orientation="vertical">
        </LinearLayout>

    </ScrollView>

In my Activity:

int length = array.length();
for (int i = 0; i < length; i++) {
                JSONObject objLoop = array.getJSONObject(i);
                String driverid = objLoop.getString("driverid");
                String name = objLoop.getString("name");
                String dlnum = objLoop.getString("dlnum");
                String contact = objLoop.getString("contact");
                //INITIALIZE TABLE LAYOUT
                TableLayout tableLayout = new TableLayout(getBaseContext());
                TableLayout.LayoutParams tableparams = new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                //row layout params
                TableRow.LayoutParams paramsRow = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                paramsRow.setMargins(5, 5, 5, 5);
                tableparams.setMargins(10, 10, 10, 10);
                tableLayout.setLayoutParams(tableparams);
                //first row
                TableRow row1 = new TableRow(getBaseContext());
                row1.setPadding(5, 5, 5, 5);
                row1.setLayoutParams(paramsRow);
                row1.setBackgroundResource(R.drawable.edit_border);
                TextView textDriverName, firstIndex;
                textDriverName = new TextView(getBaseContext());
                firstIndex = new TextView(getActivity());
                ViewGroup.LayoutParams paramsTextView = new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
                textDriverName.setLayoutParams(paramsTextView);
                firstIndex.setLayoutParams(paramsTextView);
                textDriverName.setText(name);
                firstIndex.setText("Driver Name");
                row1.addView(firstIndex);
                row1.addView(textDriverName);

                tableLayout.addView(row1);

                scrollcontainer.addView(tableLayout);
            }

Well this code is working perfectly. It is creating listview like list. Another approach is:

list.xml:

<Listview
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/myid" />

In activity:

ListView list = (ListView) findViewById(R.id.mylist);
list.setAdapter(myadapter);

My question is:

Which one is better?

First approach or second approach

I am not able to decide which one I should use. Please forgive me for my weak grammer.

Thanks in advance


Solution

  • ListView can scale to handle enormous numbers of list items (usually, similar items).This means if you have small numbers of items, ScrollView and ListView are not different too much. But when your app works with a huge data, by using ViewHolder pattern, ListView is better choice for performance. Please read more about scrollview vs listview here and here.