我使用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之間。它取決於 –