我有一个包含大约 20 列和记录数的表,其范围从 5 到 20+ 不等。我想使用水平和垂直滚动条在屏幕上显示所有这些数据。为了做到这一点,我很担心哪种方法最好。我已经开始使用 listview 解决方案,但有点卡住了。以下是我的布局的 XML(我压缩了表格行,但你明白了)..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnHome"
style="@style/homeButton"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="@string/btnHomeText"
android:textColor="@color/white"
android:textStyle="bold" />
<RelativeLayout
android:id="@+id/InnerRelativeLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btnHome">
<LinearLayout
style="@style/stdWindow"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:baselineAligned="false"
android:orientation="vertical">
<TextView
android:id="@+id/tvDriverReport"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="@color/VeryDarkGray"
android:text="@string/futureFeatureText"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/OMSGold" />
<TableLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tableLayout1">
<TableRow
android:id="@+id/tableRow1">
<TextView
android:text="@string/summaryTicketNumber"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_column="0"
android:id="@+id/textView10" />
<TextView
android:text="@string/summaryDate"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_column="1"
android:id="@+id/textView11" />
<TextView
android:text="@string/summaryTruckNumber"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_column="2"
android:id="@+id/textView12" />
<TextView
android:text="@string/summaryBeginOdometer"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_column="3"
android:id="@+id/textView21" />
<TextView
android:text="@string/summaryEndOdometer"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_column="4"
android:id="@+id/textView22" />
</TableRow>
<TableRow
android:id="@+id/tableRow2">
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_column="0"
android:id="@+id/tvTicketNumber" />
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_column="1"
android:id="@+id/tvSummaryDate" />
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_column="2"
android:id="@+id/tvTruckNumber" />
<TextView
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_column="3"
android:id="@+id/tvBeginOdometer" />
<TextView
android:layout_column="4"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/tvEndOdometer" />
</TableRow>
</TableLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
所以基本上表第 1 行是静态标题数据,但之后的每一行都会从数据库中动态填充,所以行数是可变的。
我开始使用自定义列表适配器解决方案,但似乎无法使其正常工作。这是我的自定义列表适配器
public class CustomListAdapter : BaseAdapter
{
Activity context;
public List<TicketSummaryMapper> items;
public CustomListAdapter (Activity context)
: base()
{
this.context = context;
var db = new SQLiteConnection (db);
items = db.Query<TicketSummaryMapper> ("select * from mydatabase");
}
public override int Count
{
get { return items.Count; }
}
public override Java.Lang.Object GetItem(int position)
{
return position;
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView (int position, View convertView, ViewGroup parent)
{
var item = items [position];
var view = (convertView ??
context.LayoutInflater.Inflate (
Resource.Layout.ticket_summary,
parent,
false)) as LinearLayout;
//Find references to each subview in the list item's view
var textTicketNo = view.FindViewById (Resource.Id.tvTicketNumber) as TextView;
var textSummaryDate = view.FindViewById (Resource.Id.tvSummaryDate) as TextView;
var textTruckNumber = view.FindViewById (Resource.Id.tvTruckNumber) as TextView;
var textBeginOdometer = view.FindViewById (Resource.Id.tvBeginOdometer) as TextView;
//Assign this item's values to the various subviews
textTicketNo.SetText (item.TicketNumber, TextView.BufferType.Normal);
textSummaryDate.SetText (item.TransDate, TextView.BufferType.Normal);
textTruckNumber.SetText (item.EquipmentNo, TextView.BufferType.Normal);
textBeginOdometer.SetText (item.BeginOdometer, TextView.BufferType.Normal);
//Finally return the view
return view;
}
public TicketSummaryMapper GetItemAtPosition(int position)
{
return items[position];
}
}
然后最后我的活动......
[Activity (Label = "AdminActivity")]
public class AdminActivity : Activity
{
CustomListAdapter listAdapter;
public AdminActivity() { }
public AdminActivity(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer) { }
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
//Set the Activity's view to our list layout
SetContentView (Resource.Layout.customlist);
//Create our adapter
listAdapter = new CustomListAdapter (this);
//Find the listview reference
var listView = FindViewById<ListView> (Resource.Id.listView);
//Hook up our adapter to our ListView
listView.Adapter = listAdapter;
}
}
自定义列表布局基本上只有一个线性布局,后跟一个列表视图,仅此而已。感谢您花时间看这个...