0

Please check this edited code.. its working but not showing the data of database..check box is getting tick but other 2 columns r printing as id and date..Not getting whats wrong in my setviewvalue method..

protected override void OnCreate (Bundle SaveInstace)
        {
            base.OnCreate (SaveInstace);
            SetContentView (Resource.Layout.ListView_layout);
            ActionBar.SetDisplayHomeAsUpEnabled (true);
            //Gets ListView object instance
            Database sqldb1 = ((GlobalClass)this.Application).sqldb;
            listItems = FindViewById<ListView> (Resource.Id.listItems);

            GetCursorView ();
        }

        void GetCursorView()
        {
            Database sqldb1 = ((GlobalClass)this.Application).sqldb;
            Android.Database.ICursor sqldb_cursor = sqldb1.GetRecordCursor();
            if (sqldb_cursor != null) 
            {
                sqldb_cursor.MoveToFirst ();
                string[] from = new string[] {"_id","date","Value"};
                int[] to = new int[] {
                    Resource.Id.ListRow1,
                    Resource.Id.ListRow2,
                    Resource.Id.ListRow3

                };
                    //Creates a SimplecursorAdapter for ListView object
                SimpleCursorAdapter sqldb_adapter = new SimpleCursorAdapter (this, Resource.Layout.record_view, sqldb_cursor, from, to);
                sqldb_adapter.ViewBinder = new MyCustomView();

                listItems.Adapter = sqldb_adapter;
            } 
            else 
            {

            }
        }

        public class MyCustomView:Java.Lang.Object, SimpleCursorAdapter.IViewBinder
        {

            public bool SetViewValue (View view, Android.Database.ICursor cursor, int i)
            {
                if (view.Id == Resource.Id.ListRow3) 

                { 
                    // If the column is IS_STAR then we use custom view.
                    int is_val = cursor.GetInt (i);
                    CheckBox cb = (CheckBox) view;
                    if (is_val != 0)
                    {
                        // set the visibility of the view to GONE

                        cb.Checked = true;
                        return true;
                } 
                    else
                {
                    return true;
                }
                // For others, we simply return false so that the default binding
                // happens.

            }
                return true;
       }
    }

2 Answers2

0

Looks good, just added checkbox click, have a look.

public class MyCustomView : Java.Lang.Object, SimpleCursorAdapter.IViewBinder
{

    public bool SetViewValue(View view, Android.Database.ICursor cursor, int i)
    {
        if (view.Id == Resource.Id.action_bar)
        {
            // If the column is IS_STAR then we use custom view.
            int is_val = cursor.GetInt(i);
            CheckBox cb = (CheckBox)view;
            cb.Click += cb_Click;
            if (is_val != 0)
            {
                // set the visibility of the view to GONE
                cb.Checked = true;
                return true;
            }
            else
            {
                // cb.Checked = false; //in case you want to make it (uncheck)
                return true; 
            }
            // For others, we simply return false so that the default binding
            // happens.

        }
        return true;
    }

    void cb_Click(object sender, EventArgs e)
    {
        //Handle checkbox click because value will be cahnge while clicking on checkbox
    }
}
Mohammad Riyaz
  • 1,544
  • 1
  • 17
  • 26
  • Thank you for the replay.....but i dnt want check box to be clickable. check box must be set if the db column 3 has value 1, if 0 not set.. my code is doing this task but other 2 columns r not working if i put this code the data from the database is not accessing instead its printing row column title as ID and Date – Sunita Hiremath Jul 31 '15 at 04:48
  • and i dint get y you are using action bar..please tell me – Sunita Hiremath Jul 31 '15 at 04:54
  • i have a issue with this code can u help me..http://stackoverflow.com/questions/31562079/save-the-state-of-button-in-xamarin-android – Sunita Hiremath Jul 31 '15 at 06:29
0

this code worked me thank you...:)

public class MyCustomView:Java.Lang.Object, SimpleCursorAdapter.IViewBinder
            {

                public bool SetViewValue (View view, Android.Database.ICursor cursor, int i)
                {
                    if (view.Id == Resource.Id.ListRow1) {
                        int val = cursor.GetInt (i);
                        TextView txt = (TextView)view;
                        txt.Text = val.ToString ();
                    }
                    if (view.Id == Resource.Id.ListRow2) {
                        string val1 = cursor.GetString (i);
                        TextView txt = (TextView)view;
                        txt.Text = val1;
                    }
                    if (view.Id == Resource.Id.ListRow3) 

                    { 
                        // If the column is IS_STAR then we use custom view.
                        int is_val = cursor.GetInt (i);
                        CheckBox cb = (CheckBox) view;
                        if (is_val != 0)
                        {
                            // set the visibility of the view to GONE

                            cb.Checked = true;
                            return true;
                    } 
                        else
                    {
                            cb.Clickable = false;
                        return true;
                    }
                    // For others, we simply return false so that the default binding
                    // happens.

                }
                    return true;
           }