In this class we want to see a simple CRUD example in Xamarin. Our component or widget is ListView, which allows us display one dimensional data vertically.

We will also use an edittext and a button. The aim is to add data to our ListView, edit them and delete as well.

Let’s go

 

SECTION 1: OUR MAIN ACTIVITY

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Collections;

namespace ListView_CRUD
{
    [Activity(Label = "ListView CRUD", MainLauncher = true, Icon = "@drawable/simplesmile")]
    public class MainActivity : Activity
    {
        ArrayList names;
        ListView lv;
        ArrayAdapter adapter;
        CRUD crud;
        EditText nameTxt;
        Button addBtn, updateBtn, deleteBtn,clearBtn;
        int selectedItem = -1;

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

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            lv = FindViewById<ListView>(Resource.Id.lv);
            nameTxt = FindViewById<EditText>(Resource.Id.nameTxt);
            addBtn = FindViewById<Button>(Resource.Id.addBtn);
            updateBtn = FindViewById<Button>(Resource.Id.updateBtn);
            deleteBtn = FindViewById<Button>(Resource.Id.deleteBtn);
            clearBtn = FindViewById<Button>(Resource.Id.clearBtn);

           names = new ArrayList();

           adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, names);
           lv.Adapter = adapter;

           crud = new CRUD(adapter, names);

           lv.ItemClick += lv_ItemClick;
           addBtn.Click += addBtn_Click;
           updateBtn.Click += updateBtn_Click;
           deleteBtn.Click += deleteBtn_Click;
           clearBtn.Click += clearBtn_Click;

        }

        void clearBtn_Click(object sender, EventArgs e)
        {
            crud.clear();
            nameTxt.Text = "";
            adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, names);
            lv.Adapter = adapter;
        }

        void deleteBtn_Click(object sender, EventArgs e)
        {
            if (crud.delete(selectedItem))
            {
                nameTxt.Text = "";
                adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, names);
                lv.Adapter = adapter;
            }
        }

        void updateBtn_Click(object sender, EventArgs e)
        {
            if(crud.update(nameTxt.Text, selectedItem))
            {
                adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, names);
                lv.Adapter = adapter;
            }
        }

        void addBtn_Click(object sender, EventArgs e)
        {
            if(crud.add(nameTxt.Text))
            {
                nameTxt.Text = "";
                adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, names);
                lv.Adapter = adapter;
            }

        }

        void lv_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
        {
            this.selectedItem = e.Position;
            nameTxt.Text = names[selectedItem].ToString();
        }

    }
}

SECTION 2 : OUR MAIN LAYOUT

<?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:minWidth="25px"
    android:minHeight="25px">
    <TableRow
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tableRow1">
        <EditText
            android:inputType="textPersonName"
            android:id="@+id/nameTxt"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TableRow>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minWidth="25px"
        android:minHeight="25px">
        <Button
            android:text="Add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/addBtn"
            android:layout_marginRight="0.0dp" />
        <Button
            android:text="Update"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/updateBtn" />
        <Button
            android:text="Delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/deleteBtn" />
        <Button
            android:text="Clear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/clearBtn" />
    </LinearLayout>
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/lv" />
</LinearLayout>

Best Regards.

Categorized in: