2012-06-05 29 views
1

我有一個約20列的表和數量將從5-20 +不等的記錄。我想使用水平和垂直滾動條在屏幕上顯示所有這些數據。爲了做到這一點,我很想知道哪種方法最好。我已經開始使用listview解決方案,但有一些卡住了。以下是我的佈局XML(IVE凝聚錶行,但你的想法)..MonoDroid使用水平滾動條顯示數據庫字段

<?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是靜態的航向數據,但每一行後,將被從數據庫中動態填充,所以行數將是可變的。

我開始使用自定義列表適配器解決方案,但似乎無法使其工作。這裏是我的customlistadapter

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; 
    } 
    } 

的customlist佈局基本上只是一直遵循的一個ListView一個的LinearLayout和多數民衆贊成它。感謝您抽出時間來看看這個...

+0

好了上面的代碼工作正常,除了它基本上重建整個XML我發佈在頂部(每行重複)的文件,而不僅僅是來自數據庫的數據行。 – k1komans

回答

0

嘗試用滾動型設置水平和垂直滾動和主佈局內

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scrollbars="horizontal|vertical"> 

    <LinearLayout 
     android:id="@+id/LinearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

    ..... 

</ScrollView> 
+0

謝謝你試試看 – k1komans