list on the android listview ios sample sqlite data

HalukmyHalukmy ✭✭TRMember ✭✭
edited January 2014 in Xamarin.Android

i developed a sqlite app on ios and its simple to show on listview, but i try to port android, i got a question

public void SqList(string databaseFile,string opinion)
{
try
{
if (!File.Exists (databaseFile))
{

           return;

        }
        //end if

        // Connect to database
        using (SqliteConnection sqlCon = new SqliteConnection (String.Format ("Data Source = {0}", databaseFile))) 
        {

           sqlCon.Open ();

           using (SqliteCommand sqlCom = new SqliteCommand (sqlCon)) 
           {
              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 ());
           }
           sqlCon.Close ();
        }
     } catch (Exception ex) 
     { }
  }

this is ios sample but i checked the android version

if(this._taskListView != null)
         {
            this._taskListView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) => {
               var taskDetails = new Intent (this, typeof (TaskDetailsScreen));
               taskDetails.PutExtra ("TaskID", this._tasks[e.Position].ID);
               this.StartActivity (taskDetails);
            };
         }

its shows on listview

i guess i convert this line to

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

Android

thanks for help

Posts

  • SebastianSeidel.9226SebastianSeidel.9226 ✭✭✭✭ DEInsider, University ✭✭✭✭
    edited January 2014

    On Android you have to use Adapters. You can easily implement your own adapter by subclassing from ArrayAdapter and add this adapter to your table view like this:

     TableView.Adapter = myAdapter;
    

    .

    A simple adapter could look like this:

     var taskNameList = tasks.Select(t=>t.Name).ToList();
    
     myAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, taskNameList);
    

    This will add the name of the task to you list view.

Sign In or Register to comment.