mercredi 9 décembre 2015

How to add data from database table to ListView C# Xamarin Android App

Few days ago I asked how to share data between activities and one user told me to use SQLite, so I did. I want to let user click on button in the MainLayout which will redirect him to AddTaskLayout where he can add task name, and by pressing Save button app will redirect him back to MainLayout where his task will be listed in ListView.

So far, I created database, table and everything that I need. My question is: How to add data stored in database table to ListView? Every answer I've found was written in Java so searching old StackOverflow questions wasn't so helpful :/

Here's the code:

My DBRepository is class that represents creating database, creating table, inserting data to table and getting that same data:

    public class DBRepository
{

    public void CreateDatabase()
    {
        string dbPath = Path.Combine(System.Environment.GetFolderPath
            (System.Environment.SpecialFolder.Personal), "database.db3");
        var db = new SQLiteConnection(dbPath);
    }

    public void CreateTable()
    {
        string dbPath = Path.Combine(System.Environment.GetFolderPath
           (System.Environment.SpecialFolder.Personal), "database.db3");
        var db = new SQLiteConnection(dbPath);
        db.CreateTable<ToDoTasks>();
    }

    public string InsertRecord(string task)
    {
        string dbPath = Path.Combine(System.Environment.GetFolderPath
           (System.Environment.SpecialFolder.Personal), "database.db3");
        var db = new SQLiteConnection(dbPath);
        ToDoTasks item = new ToDoTasks();
        item.Task = task;
        db.Insert(item);
        return task;
    }

    public string GetData()
    {
        string dbPath = Path.Combine(System.Environment.GetFolderPath
           (System.Environment.SpecialFolder.Personal), "database.db3");
        var db = new SQLiteConnection(dbPath);
        string output = "";
        var table = db.Table<ToDoTasks>();
        foreach(var item in table)
        {
            output += item;

        }            
        return output;

    }


}

ToDoTasks class where I create table:

    [Table("ToDo")]
public class ToDoTasks
{
    [PrimaryKey, AutoIncrement, Column("_Id")]
    public int Id { get; set; }

    [MaxLength(100)]
    public string Task { get; set; }
}

My AddTaskActivity represents second Layout where user enters task name:

    protected override void OnCreate(Bundle bundle)
    {

        base.OnCreate(bundle);
        SetContentView(Resource.Layout.AddTask);

        //define buttons
        Button save, cancel;
        save = FindViewById<Button>(Resource.Id.save);
        cancel = FindViewById<Button>(Resource.Id.cancel);

        save.Click += save_click;
        cancel.Click += cancel_click;


    }

    private void save_click(object sender, EventArgs e)
    {
        DBRepository dbr = new DBRepository();
        EditText name = FindViewById<EditText>(Resource.Id.taskName);
        //enter user's input(task name) to table

        var result = dbr.InsertRecord(name.Text);
        StartActivity(typeof(MainActivity));
    }

    private void cancel_click(object sender, EventArgs e)
    {
        StartActivity(typeof(MainActivity));
    }

My MainActivity where I want to populate listView:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set view
        SetContentView(Resource.Layout.Main);

        //create database if it doesn't exist
        DBRepository dbr = new DBRepository();
        dbr.CreateDatabase();

        //create table (if it doesn't exist)
        dbr.CreateTable();

        //Define buttons
        Button addTask;
        ListView list;
        addTask = FindViewById<Button>(Resource.Id.addTask);
        addTask.Click += addTask_click;


    }             

    private void addTask_click(object sender, EventArgs e)
    {
        StartActivity(typeof(AddTaskActivity));
    }

I really appreciate your help. I know these are pretty basic questions, but someone have to ask them for himself and many others (future) C# android developers. Thanks!

Aucun commentaire:

Enregistrer un commentaire