Ravi
Ravi

Reputation: 1784

Xamarin android: How to get GridView Data Source in android like asp.net

I have a WebService which return user details like User Name, its Date of Birth and Address. I am calling this web service to my android application developed Xamarin c#. But don't know how to show data (bind data) in Grid View. can some one provide sample code or explain how to show data in Grid View.

Upvotes: 0

Views: 6525

Answers (1)

Stam
Stam

Reputation: 2490

What you need to do, is to create an adapter for the GridView and axml layout (the view of the components in the GridView). For example, suppose that you want to make a GridView of Tiles with blue background and with a text at the left bottom corner with white color:

tile.axml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tile_layout"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:background="#0000FF">

    <TextView
        android:id="@+id/tile_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:paddingLeft="6dp"
        android:paddingBottom="2dp"
        android:textColor="#ffffff" />

</RelativeLayout>

TileAdapter.cs

public class TileAdapter : BaseAdapter<BaseTileItem>
    {
        protected Activity _context = null;
        protected List<BaseTileItem> _tileList = new List<BaseTileItem>();


        public TileAdapter(Activity context, List<BaseTileItem> tileList)
            : base()
        {
            this._context = context;
            this._tileList = tileList;


        }


        public override BaseTileItem this[int position]
        {
            get { return this._tileList[position]; }
        }


        public override long GetItemId(int position)
        {
            return position;
        }

        public override int Count
        {
            get { return this._tileList.Count; }
        }


        /// Inflates the Tile layout and sets the values
        public override Android.Views.View GetView(int position, Android.Views.View convertView, Android.Views.ViewGroup parent)
        {

            // Get our object for this position
            var item = this._tileList[position];

            //Try to reuse convertView if it's not  null, otherwise inflate it from our item layout
            var view = (convertView ??
                    this._context.LayoutInflater.Inflate(
                    Resource.Layout.tile,
                    parent,
                    false)) as RelativeLayout;

            // Find references to each subview in the list item's view
            TextView itemText = view.FindViewById<TextView>(Resource.Id.tile_text);

            //Assign this item's values to the various subviews
            itemText.Text = this._tileList[position].Text;

            //Finally return the view
            return view;
        }
    }
}

TileBaseItem.cs

//This is what you retrieve from the webservice 
public class BaseTileItem
{

    private string _text;


    public BaseTileItem(string text)
    {
        this._text = text;
    }

    public string Text {
        get {return  this._text; }
        set { this._text = value; }
    }

And then in your activity :

private List<BaseTileItem> _usersList;
private TileAdapter _usersAdapter;
private GridView _usersGridView;

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

    this.SetContentView(Resource.Layout.startactivity);



    //inflate all views
    InflateViews();


    this._usersList = //populate the list with BaseTileItem items
    this._usersAdapter = new TileAdapter(this, this._usersList);

    //creating the GridView
   this._usersGridView = new GridView(this);
   this._usersGridView.SetNumColumns(2);
   this._usersGridView.SetGravity(GravityFlags.Center);
   this._usersGridView.SetPadding(0, 20, 0, 20);
   this._usersGridView.SetVerticalSpacing(20);
   this._usersGridView.Adapter = this._usersAdapter;
}

Upvotes: 2

Related Questions