Reputation: 581
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
Upvotes: 0
Views: 85
Reputation: 5941
Scrollview is ideal for screens where scrolling is required, but it is not efficient when scroll view is used to render a larger data set. Instead, you can use specialized adapter views like ListView, GridView and Recycler View (Introduced in Android Lollipop) for long lists.
ListView is an Android ViewGroup, used extensively to display the collection of data in vertical scrollable rows. The list items are automatically inserted into the list using an Adapter and Adapter pulls data from data source sources such as an array, cursor, etc.
Check out the following examples, which might help.
Upvotes: 1
Reputation: 2140
As per my opinion use of Native widgets are bettor then your approach. There are I have reason for that. Reasons :
In Your Approach :
In other hand native widgets Benefits :
So we can say that Native Approach is better. :)
Upvotes: 1
Reputation: 8073
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.
Upvotes: 1
Reputation: 559
Simple answer, use list views for the linear layout.
They work best if you have larger amounts of data.
Upvotes: 1