Finally found a solution. I had the same question as someone new to Android and Xamarin for Android. To be clear - I use Xamarin Studio and Xamarin for Android, written in C #.
Although it seems that a SimpleCursorAdapter can hold multiple fields, when binding to something like SimpleListItem, only one field is used.
I first tried this:
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 ()); }
But all I ever got was the last field on the cursor line.
I found out that CUSTOM LISTVIEW is needed . Below are the code files for the four files:
- MainActivity.cs
- myList.xml
- Main.axml
- PreFloat.cs (this is part of the database)
I included all this in order to maximize the use of a fully working model, which is as close as possible to real life.
Here, MainActivity.cs, which sets the content view, uses the cursor to retrieve data from the SQLite database and defines
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);
}
Here is the Main.axml file:
<?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>
And finally, the myList.xml file, which is essentially the definition of a ListView string:
<?xml version="1.0" encoding="utf-8"?> <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>
And here is the database file:
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){
Questions: SHOULD a SimpleListItem control have more than one field or, at most, a checkbox control and a label? Should grid management be used instead?
Alternatives: Instead of doing all these frauds, would it be easier to simply combine the necessary values ββusing SQL? Moreover, coordinating the positioning of two text controls can be too complicated.
Full disclosure: This code is an amalgam of code that I read in other posts and forums, and is configured according to my own requirements.