我使用scrollview+linearlayout
创建了一个列表。我创建customviews
内linear layout
,这里是我的代码:哪个listview更好?
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>
在我的活动:
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);
}
那么这段代码可以正常使用。它创建像列表一样的列表视图。 另一种方法是:
list.xml:
<Listview
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/myid" />
在活动:
ListView list = (ListView) findViewById(R.id.mylist);
list.setAdapter(myadapter);
我的问题是:
哪一个更好?
第一种方法或第二种方法
我不能决定我应该使用哪一个。请原谅我的弱语法。
预先感谢
使用listview和设置listview标题这是你的线性布局 –
你有多少行数据? –
1到100之间。它取决于 –