Forum Xamarin.Android

How to bind data in listview

merbsmerbs PHMember ✭✭
edited September 2016 in Xamarin.Android

okay, so my code works fine but whenever I searched the selected item.. it returns the 1st case.. which is the Mt.Batulao(batulaoAct). what should I do? please help me.. I'm stuck here :(

my Code:

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

        SetContentView(Resource.Layout.Main);
        mListView = FindViewById<ListView>(Resource.Id.listView);
        mSearch = FindViewById<EditText>(Resource.Id.etSearch);
        mContainer = FindViewById<LinearLayout>(Resource.Id.llContainer);

        mSearch.Alpha = 0;
        mSearch.TextChanged += mSearch_TextChanged;

        mMountains = new List<Mountain>();
        mMountains.Add(new Mountain()
        {
            MtName = "Mt. BATULAO",
            Masl = "830 Masl",
            Difficulty = "3/9",
            ImgResourceId = Resource.Drawable.batulao
        });
        mMountains.Add(new Mountain()
        {
            MtName = "Mt. TALAMITAM",
            Masl = "630 Masl",
            Difficulty = "2/9",
            ImgResourceId = Resource.Drawable.talamitam
        });
        mMountains.Add(new Mountain()
        {
            MtName = "Mt. MAKILING",
            Masl = "1090 Masl",
            Difficulty = "5/9",
            ImgResourceId = Resource.Drawable.makiling
        });
        mMountains.Add(new Mountain()
        {
            MtName = "Mt. CRISTOBAL",
            Masl = "1480 Masl",
            Difficulty = "5/9",
            ImgResourceId = Resource.Drawable.cristobal
        });
        mMountains.Add(new Mountain()
        {
            MtName = "Mt. ULAP",
            Masl = "1846 Masl",
            Difficulty = "3/9",
            ImgResourceId = Resource.Drawable.ulap
        });

        mListView.ItemClick += mListView_ItemClick;
 mAdapter = new MountainsAdapter(this, Resource.Layout.ml_model, mMountains);
        mListView.Adapter = mAdapter;
        mListView.ItemClick += mListView_ItemClick;           

    }

    private void mListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        Intent myIntent;
        if (mMountains[e.Position].MtName == "Mt. BATULAO")
        {
            StartActivity(typeof (batulaoAct));
        }
        else if (mMountains[e.Position].MtName == "Mt. TALAMITAM")
        {
            StartActivity(typeof (talamitamAct));
        }
        else if (mMountains[e.Position].MtName == "Mt. MAKILING")
        {
            StartActivity(typeof (makilingAct));
        }
        else if (mMountains[e.Position].MtName == "Mt. CRISTOBAL")
        {
            StartActivity(typeof (cristobalAct));
        }
        else if (mMountains[e.Position].MtName == "Mt. ULAP")
        {
            StartActivity(typeof (ulapAct));
        }           
    }

void mSearch_TextChanged(object sender, Android.Text.TextChangedEventArgs e)
{

        List<Mountain> searchedMountains = (from mountain in mMountains
                                        where mountain.MtName.Contains(mSearch.Text, StringComparison.OrdinalIgnoreCase) || mountain.Masl.Contains(mSearch.Text, StringComparison.OrdinalIgnoreCase)
                                        select mountain).ToList();

        mAdapter = new MountainsAdapter(this, Resource.Layout.ml_model, searchedMountains);
        mListView.Adapter = mAdapter;
    }
Tagged:

Best Answer

  • MabroukMabrouk US ✭✭✭
    edited September 2016 Accepted Answer

    @merbs

    Hi friend,

    A general note :

    I think that your Mountain class is like this :

    public class Mountain 
    {
       public string MtName {get; set;}
       public string   Masl {get; set;}
       public string   Difficulty {get; set;}
       public int ImgResourceId {get; set;}
    }
    

    if you add an other prop "Type" like :

    public class Mountain 
    {
       public string MtName {get; set;}
       public string   Masl {get; set;}
       public string   Difficulty {get; set;}
       public int ImgResourceId {get; set;}
       public Type MClassType {get; set;}  // You will use it to start intents
    }
    

    then, your ItemClick event will be too easy like :

            private void mListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
            {
                    Intent myIntent=new Intent(this,mMountains[e.Position].MClassType);
                    StartActivity(myIntent);
            }
    

    Now, I recommend you to use the RecyclerView.Adapter.. it will be something like this :

       class CustomAdapter : RecyclerView.Adapter
        {
            public event EventHandler<int> ItemClick;
            private List<Mountain> _list = new List<Mountain>();
            private Context _context;
            public CustomAdapter(List<Mountain> list, Context ctx)
            {
                this._context = ctx;
                this._list = list;
            }
    
            public override int ItemCount
            {
                get { return _list.Count; }
            }
    
            public void OnClick(int position)
            {
                if (ItemClick != null)
                    ItemClick(this, position);
            }
    
            public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
            {
                YourViewHolder vh = holder as YourViewHolder;
                Mountain montain=_list[position];          
                vh.MtName.Text = montain.MtName;
                vh.Masl.Text = montain.Masl;
                .....
                .....
            }
    
            public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
            {
                    View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.YOUR_LAYOUT, parent, false);
                    YourViewHolder vh = new YourViewHolder(itemView, OnClick);
                    return vh;
            }
        }
    

    The ViewHolder :

        public class YourViewHolder : RecyclerView.ViewHolder
        {
            public TextView MtName { get; private set; }  // for example
            ....
            public YourViewHolder(View itemView, Action<int> listener)
                : base(itemView)
            {
                    MtName = itemView.FindViewById<TextView>(Resource.Id.ELEMENT_ID);
    
                    itemView.Click += (sender, e) => listener(base.Position);
    
            }
        }
    

    And in your LAYOUT you should have something like this :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    </RelativeLayout>
    

    I hope that helps you ...

    Mabrouk.

Answers

  • MabroukMabrouk USMember ✭✭✭
    edited September 2016 Accepted Answer

    @merbs

    Hi friend,

    A general note :

    I think that your Mountain class is like this :

    public class Mountain 
    {
       public string MtName {get; set;}
       public string   Masl {get; set;}
       public string   Difficulty {get; set;}
       public int ImgResourceId {get; set;}
    }
    

    if you add an other prop "Type" like :

    public class Mountain 
    {
       public string MtName {get; set;}
       public string   Masl {get; set;}
       public string   Difficulty {get; set;}
       public int ImgResourceId {get; set;}
       public Type MClassType {get; set;}  // You will use it to start intents
    }
    

    then, your ItemClick event will be too easy like :

            private void mListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
            {
                    Intent myIntent=new Intent(this,mMountains[e.Position].MClassType);
                    StartActivity(myIntent);
            }
    

    Now, I recommend you to use the RecyclerView.Adapter.. it will be something like this :

       class CustomAdapter : RecyclerView.Adapter
        {
            public event EventHandler<int> ItemClick;
            private List<Mountain> _list = new List<Mountain>();
            private Context _context;
            public CustomAdapter(List<Mountain> list, Context ctx)
            {
                this._context = ctx;
                this._list = list;
            }
    
            public override int ItemCount
            {
                get { return _list.Count; }
            }
    
            public void OnClick(int position)
            {
                if (ItemClick != null)
                    ItemClick(this, position);
            }
    
            public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
            {
                YourViewHolder vh = holder as YourViewHolder;
                Mountain montain=_list[position];          
                vh.MtName.Text = montain.MtName;
                vh.Masl.Text = montain.Masl;
                .....
                .....
            }
    
            public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
            {
                    View itemView = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.YOUR_LAYOUT, parent, false);
                    YourViewHolder vh = new YourViewHolder(itemView, OnClick);
                    return vh;
            }
        }
    

    The ViewHolder :

        public class YourViewHolder : RecyclerView.ViewHolder
        {
            public TextView MtName { get; private set; }  // for example
            ....
            public YourViewHolder(View itemView, Action<int> listener)
                : base(itemView)
            {
                    MtName = itemView.FindViewById<TextView>(Resource.Id.ELEMENT_ID);
    
                    itemView.Click += (sender, e) => listener(base.Position);
    
            }
        }
    

    And in your LAYOUT you should have something like this :

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    </RelativeLayout>
    

    I hope that helps you ...

    Mabrouk.

  • merbsmerbs PHMember ✭✭

    thank you my friend Mabrouk! helps a lot! :D

Sign In or Register to comment.