如何使用Android SimpleCursorAdapter组合两个或多个列

时间:2011-09-22 19:06:16

标签: android simplecursoradapter

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中显示吗?如果有,怎么样?

2 个答案:

答案 0 :(得分:4)

您可以创建SimpleCursorAdapter.ViewBinder来执行此操作。有关示例,请参阅this answer

基本上,每个视图都会调用ViewBinder。因此,当您访问View时,您希望同时拥有两列,只需检索两个列值,组合它们,然后在View上设置值。

答案 1 :(得分:0)

终于找到了解决方案。我有一个与Android和Xamarin for Android新手相同的问题。要清楚 - 我使用的是Xamarin Studio,而Xamarin是用C#编写的。

虽然看起来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(这是数据库部分)
  5. 我已将所有这些内容尽可能地提供尽可能接近现实生活的完整工作样本。

    这是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
    

    问题: 应该一个SimpleListItem控件有多个字段,或者最多只有一个复选框控件和标签? 是否应该使用网格控件?

    替代品: 而不是做所有这些恶作剧,使用SQL简单地连接所需的值是不是更容易?特别是因为协调两个文本控件的定位可能太复杂了。

    完全披露: 此代码是我在其他帖子和论坛中阅读的代码混合,并根据我自己的特定要求进行了自定义。