1
votes

If you remember tasky demos from xamarin samples, we can connect the Tasks ( like id, title, sub) with

TableView.Source = new RootTableSource(tasks.ToArray ());

In monodroid, its bit complicated. I do use sqlite and here my monotouch sqlite code

{
    sqlCom.CommandText="SELECT * FROM Personel";

    using (SqliteDataReader dbReader = sqlCom.ExecuteReader ())
    {
        if (dbReader.HasRows)
        {
            int i;
            // Advance through each row
            tasks = new List<TaskX> ();
            while (dbReader.Read ())
            {     
                TaskX dtask= new TaskX() {Id = Convert.ToInt16( dbReader["ID"]), 
                    Name = String.Format (Convert.ToString (dbReader["UserName"])),
                    Notes=String.Format ( Convert.ToString (dbReader["Password"])),
                    Done=false};
                tasks.Add(dtask);
            };
        }
    };
    TableView.Source = new RootTableSource(tasks.ToArray ());
}

and i noticed the code lines takes the source values

protected override void OnResume ()
{
    base.OnResume ();

    this._tasks = Tasky.BL.Managers.TaskManager.GetTasks();

    // create our adapter
    this._taskList = new Adapters.TaskListAdapter(this, this._tasks);

    //Hook up our adapter to our ListView
    this._taskListView.Adapter = this._taskList;
}

is any way to show sqlite values like id, username on android list view? thanks

1

1 Answers

3
votes

On Android you will have to create a custom adapter. Check out this documentation at Xamarin, especially the section Referencing a Custom Row View.

You will have to subclass BaseAdapter, like

public class TaskAdapter : BaseAdapter<Task>
{
public override View GetView(int position, View convertView, ViewGroup parent)
   {
       var task = items[position];
   }
}

The important part is GetView where you access your list of tasks and then populate a view and return that:

View view = convertView;
if (view == null) // no view to re-use, create new
view = context.LayoutInflater.Inflate(Resource.Layout.YourTaskView, null);
view.FindViewById<TextView>(Resource.Id.TaskId).Text = task.Id.ToString();

If you return a lot of tasks from your database, be sure to check out the CursorAdapter, too.