2011-09-22 64 views
4
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
    this, 
    R.layout.mytaskslayout, 
    cursor, 
    new String[] {"Aircraft","Discrepancy","ARR_FLT"}, 
    new int[] {R.id.ac, R.id.discrepancy, R.id.arrinfo} 
); 

我可以組合兩個或多個列並在一個R.id.XXX中顯示嗎?如果是,如何?如何將兩個或多個列與Android SimpleCursorAdapter組合使用

+1

試着給出一個更完整的標題來解決你的問題。 – Cristian

+0

使您的owwn適配器基於simplecursoradapter並重寫getView ..或者像thiz一樣構建查詢select tab1.column || tabx.column作爲名稱來自.... – Selvin

回答

4

您可以創建一個SimpleCursorAdapter.ViewBinder來執行此操作。一個例子見this answer

基本上你的ViewBinder將被調用每個視圖。因此,當你到達想要同時擁有兩列的視圖時,只需檢索兩個列值,將它們合併,然後在自己的視圖上設置值。

0

終於找到了解決辦法。我遇到了與Android和Xamarin for Android新手一樣的問題。要清楚 - 我正在使用C#編寫的Xamarin Studio和Xamarin for Android。

雖然看起來像SimpleCursorAdapter可以容納多個領域,結合類似的SimpleListItem只有一個字段被使用時。

我第一次嘗試這樣的:

 string[] fromColumns = new string[]{ "checkListName","checkListDesc" }; 
     int[] toControlIDs = new int[] {Android.Resource.Id.Text1, Android.Resource.id.Text1}; 

     try{ 
      listView.Adapter = new SimpleCursorAdapter(this,Android.Resource.Layout.SimpleListItem1 , c, fromColumns, toControlIDs, 0);   
     } 
     catch(SQLiteException e){ 
      Console.WriteLine ("whoops, " + e.Message.ToString()); 
     } 

但所有我曾經得到的是光標行的最後一個字段。

我才知道,需要一個CUSTOM的LISTVIEW。以下是代碼文件四個文件:

  1. MainActivity.cs
  2. myList.xml
  3. Main.axml
  4. PreFloat.cs(這是數據庫的一部分)

我有包括所有這些儘可能提供儘可能接近現實生活的完整工作樣本。

這裏是MainActivity.cs其中規定內容來看,使用遊標從SQLite數據庫獲取數據,並定義了

using System; 

using Android.App; 
using Android.Content; 
using Android.Database; 
using Android.Database.Sqlite; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 

namespace Darjeeling 
{ 
[Activity (Label = "Darjeeling", MainLauncher = true, Icon = "@drawable/icon")] 
public class MainActivity : Activity 
{  

    ListView listView; 
    Darjeeling.PreFloatDatabase pdb; 
    ICursor c; 
    protected override void OnCreate (Bundle bundle) 
    { 
     base.OnCreate (bundle); 

     // Set our view from the "main" layout resource 
     SetContentView (Resource.Layout.Main); 
     listView = FindViewById<ListView> (Resource.Id.listView1); 
     pdb = new PreFloatDatabase (this); 
     // Assign the cursor to a query 
     c = pdb.ReadableDatabase.RawQuery ("select * from checkLists", null); 
     StartManagingCursor (c); 
     // A ListView needs an adapter -- so we'll assign our instantiated listView's adapter to our customized adapter called HomeScreenCursorAdapter. 
     listView.Adapter = (IListAdapter)new HomeScreenCursorAdapter (this,c); 
    } 
    // End onCreate method 

    // This handles the cursor when the user is done with the activity 
    protected override void OnDestroy() 
    { 
     StopManagingCursor(c); 
     c.Close(); 
     base.OnDestroy(); 
    } 
    // Here's the magic -- 
    public class HomeScreenCursorAdapter : CursorAdapter { 
     Activity context; 
     public HomeScreenCursorAdapter(Activity context, ICursor c) 
      : base (context, c) 
     { 
      this.context = context; 
     } 
     // This overridden BindView method is going to let me assign TextView controls that I've set up in an XML file, to specific fields in the cursor. 
     public override void BindView(View view, Context context, ICursor cursor) 
     { 
      var txtCheckListName = view.FindViewById<TextView> (Resource.Id.txtCheckListName); //(Android.Resource.Id.Text1); 
      var txtCheckListDesc = view.FindViewById<TextView> (Resource.Id.txtCheckListDesc); //(Android.Resource.Id.Text2); 
// For testing purposes, I first assigned static values to txtCheckListName and txtCheckListDesc, for instance, txtCheckListName.Text = "Hello"; and txtCheckListDesc.Text = "World"; 
      txtCheckListName.Text = cursor.GetString (3); 
      txtCheckListDesc.Text = cursor.GetString (4); 
     } 
     // This overridden View inflates each row (I think). This could inflate a built-in ListView control like SimpleListItem, OR, in this case, it references a custom written XML file called myList. 
     public override View NewView(Context context, ICursor cursor, ViewGroup parent) 
     { 
      return this.context.LayoutInflater.Inflate (Resource.Layout.myList, parent, false); 
     } 
    } 
} 

}

這裏的Main.axml文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/linearLayout1" 
android:minWidth="25px" 
android:minHeight="25px"> 
<ListView 
    android:minWidth="25px" 
    android:minHeight="25px" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/listView1" /> 
</LinearLayout> 

最後,myList.xml文件基本上是ListView的行的定義:

<?xml version="1.0" encoding="utf-8"?> 
<!---<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="200dp" 
      android:layout_height="match_parent" 
      android:background="#FF0000FF"> 
--> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/linearLayout1" 
android:minWidth="25px" 
android:minHeight="25px"> 
<TextView 
    android:id="@+id/txtCheckListName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="" /> 
<TextView 
    android:id="@+id/txtCheckListDesc" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0" 
    android:text="" /> 
</LinearLayout> 
<!---</FrameLayout>--> 

而這裏的數據庫文件:

using System; 
using Android.Database.Sqlite; 
using Android.Content; 

namespace Darjeeling { 
class PreFloatDatabase : SQLiteOpenHelper { 
    public static readonly string create_checkLists_table = "create table if not exists checkLists([_id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE, checkListID INTEGER, checkListType INTEGER, checkListName TEXT, checkListDesc TEXT);"; 
    public static readonly string DatabaseName = "prefloat.db"; 
    public static readonly int DatabaseVersion = 1; 
    public PreFloatDatabase (Context context) : base (context, DatabaseName, null, DatabaseVersion){  } 

    public override void OnCreate(SQLiteDatabase db){ 
     // fire the statement that creates the checkLists table 
     try{ 
     db.ExecSQL (create_checkLists_table);   
     // Now pre-fill the checkLists table 
      db.ExecSQL ("insert into checkLists (checkListID, checkListType, checkListName, checkListDesc) values (0, 0, 'Widgeon','Widgeon Daysailer');"); 
      db.ExecSQL ("insert into checkLists (checkListID, checkListType, checkListName, checkListDesc) values (1, 1, 'Widgeon','Widgeon Daysailer');"); 
      db.ExecSQL ("insert into checkLists (checkListID, checkListType, checkListName, checkListDesc) values (2, 0, 'Bowrider', 'Mo Motor, Mo Fun');"); 
      db.ExecSQL ("insert into checkLists (checkListID, checkListType, checkListName, checkListDesc) values (3, 1, 'Bowrider', 'Mo Motor, Mo Fun');"); 
      db.ExecSQL ("insert into checkLists (checkListID, checkListType, checkListName, checkListDesc) values (4, 0, 'HobieCat','Hang yer ass out fun');"); 
      db.ExecSQL ("insert into checkLists (checkListID, checkListType, checkListName, checkListDesc) values (5, 1, 'HobieCat','Hang yer ass out fun');"); 
     } 
     catch(SQLiteException e){ 
      Console.WriteLine ("Problem with the database " + e.Message.ToString()); 
     } 

    } 
    public override void OnUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){ 
     throw new NotImplementedException(); 
    } 

} // matches with class PreFloatDatabase 
} // matches with namespace Darjeeling 

問題: SHOULD一個SimpleListItem控制比單個字段或至多CheckBox控件和標籤嗎? 應該使用網格控件嗎?

替代品: 而不是所有這些shenanigans,簡單地連接所需的值使用SQL是不是更容易?特別是因爲協調兩個文本控件的位置可能太複雜了。

完全披露: 此代碼是我在其他帖子和論壇中閱讀的代碼的混合物,並根據我自己的特定要求進行了自定義。

相關問題