专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

Xamarin.Android 使用ListView绑定数据

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:56:12       共计:3631 浏览
使用ListView进行数据绑定

第一步:新建一个类,用于存储显示字段。

namespace ListViewDataBIndDemo
{
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string IdCard { get; set; }
        public string Sex { get; set; }
        public string Age { get; set; }
    }
}

第二步:在Mian.axml 中绘制界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="10dp">
        <TextView
            android:text="姓名"
            android:textSize="23sp"
            android:layout_width="150dp"
            android:layout_height="match_parent" />
        <EditText
            android:id="@+id/personName"
            android:layout_width="280dp"
            android:layout_height="wrap_content"
            android:singleLine="true" />
    </LinearLayout>
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp">
        <LinearLayout
            android:id="@+id/LinearLayoutdasb2"
            android:layout_width="wrap_content"
            android:orientation="vertical"
            android:layout_height="match_parent">
            <LinearLayout
                android:id="@+id/LinearLayoutdasb3"
                android:layout_width="wrap_content"
                android:orientation="horizontal"
                android:layout_height="27dp"
                android:background="#C0C0C0">
                <TextView
                    android:text="序号"
                    android:layout_width="50dp"
                    android:gravity="center"
                    android:textColor="#000000"
                    android:textSize="20sp"
                    android:layout_height="wrap_content" />
                <TextView
                    android:text="姓名"
                    android:layout_width="100dp"
                    android:gravity="center"
                    android:textSize="20sp"
                    android:textColor="#000000"
                    android:layout_height="wrap_content" />
                <TextView
                    android:text="身份证号"
                    android:layout_width="250dp"
                    android:gravity="center"
                    android:textSize="20sp"
                    android:textColor="#000000"
                    android:layout_height="wrap_content" />
                <TextView
                    android:text="性别"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:textSize="20sp"
                    android:textColor="#000000"
                    android:layout_width="80dp" />
                <TextView
                    android:text="年龄"
                    android:layout_width="80dp"
                    android:gravity="center"
                    android:textColor="#000000"
                    android:textSize="20sp"
                    android:layout_height="wrap_content" />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/LinearLayout54"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFAF0">
                <ListView
                    android:id="@+id/listViewPerson"
                    android:minWidth="30dp"
                    android:minHeight="30dp"
                    android:layout_width="550dp"
                    android:layout_height="match_parent"
                    android:divider="#000000"
                    android:dividerHeight="1dp" />
            </LinearLayout>
        </LinearLayout>
    </HorizontalScrollView>
</LinearLayout>

第三步:创建一个 Person_Items.axml 用于存放数据

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dip">
  <TextView
      android:id="@+id/item_id"
      android:layout_width="40dp"
      android:layout_height="wrap_content"
      android:textColor="#000000"
      android:layout_marginLeft="20dp"
      android:gravity="left"
      android:textSize="20sp"  />
  <TextView
      android:id="@+id/item_name"
      android:layout_width="90dp"
      android:layout_height="wrap_content"
      android:textColor="#000000"
      android:layout_marginLeft="20dp"
      android:gravity="left"
      android:textSize="20sp" />
  <TextView
      android:id="@+id/item_idcard"
      android:layout_width="240dp"
      android:layout_height="wrap_content"
      android:textColor="#000000"
      android:gravity="left"
      android:layout_marginLeft="10dp"
      android:textSize="20sp"  />
  <TextView
      android:id="@+id/item_sex"
      android:layout_width="70dp"
      android:layout_height="wrap_content"
      android:textColor="#000000"
      android:gravity="left"
      android:layout_marginLeft="10dp"
      android:textSize="20sp"  />
  <TextView
      android:id="@+id/item_age"
      android:layout_width="70dp"
      android:layout_height="wrap_content"
      android:textColor="#000000"
      android:gravity="left"
      android:layout_marginLeft="10dp"
      android:textSize="20sp" />
</LinearLayout>

第四步:创建一个继承于BaseAdapter的类名为ListViewAdapter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

namespace ListViewDataBIndDemo
{
    public class ListViewAdapter:BaseAdapter<Person>
    {
        Activity context;
        public List<Person> mings;

        public ListViewAdapter(Activity context, List<Person> mings)
        {
            this.context = context;
            this.mings = mings;
        }

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

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

        public override Person this[int position]
        {
            get
            {
                return this.mings[position];
            }
        }

        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var itme = this.mings[position];

            convertView = LayoutInflater.From(context).Inflate(Resource.Layout.Person_Items, parent, false);

            TextView item_id = convertView.FindViewById<TextView>(Resource.Id.item_id);
            TextView item_name = convertView.FindViewById<TextView>(Resource.Id.item_name);
            TextView item_idcard = convertView.FindViewById<TextView>(Resource.Id.item_idcard);
            TextView item_sex = convertView.FindViewById<TextView>(Resource.Id.item_sex);
            TextView item_age = convertView.FindViewById<TextView>(Resource.Id.item_age);

            item_id.Text = (position + 1).ToString();
            item_name.SetText(itme.Name, TextView.BufferType.Normal);
            item_idcard.SetText(itme.IdCard, TextView.BufferType.Normal);
            item_sex.SetText(itme.Sex, TextView.BufferType.Normal);
            item_age.SetText(itme.Age, TextView.BufferType.Normal);

            return convertView;
        }
    }
}

第五步:在MainActivity进行对数据绑定

using Android.App;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using System;

namespace ListViewDataBIndDemo
{
    [Activity(Label = "ListViewDataBIndDemo", MainLauncher = true, Theme = "@android:style/Theme.Holo.Light")]
    public class MainActivity : Activity
    {
        private List<Person> main_data = new List<Person>();
        private Person person;

        ListView lv_Person;
        EditText personName;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Main);

            lv_Person = FindViewById<ListView>(Resource.Id.listViewPerson);
            personName = FindViewById<EditText>(Resource.Id.personName);

            for (int i = 1; i <= 20; i++)
            {
                person = new Person();
                person.Id = i;
                person.Name = "张三" + i.ToString();
                person.IdCard = "20172619979999" + new Random().Next(1000, 10000).ToString();
                person.Sex = new Random().Next(1, 3).ToString() == "1" ? "男" : "女";
                person.Age = new Random().Next(15, 30).ToString();
                main_data.Add(person);
            }
            lv_Person.Adapter = new ListViewAdapter(this, main_data);


            lv_Person.ItemClick += (s, e) =>
            {
                Toast.MakeText(this, "姓名:"+main_data[e.Position].Name +"\n身份证号:"+main_data[e.Position].IdCard, ToastLength.Long).Show();
            };

            //搜索姓名
            personName.TextChanged += (s, e) =>
             {
                 List<Person> per = selectList(main_data, personName.Text);
                 lv_Person.Adapter = new ListViewAdapter(this, per);
             };
        }

        private List<Person> selectList(List<Person> list, string value)
        {
            return list.FindAll(delegate (Person info)
            {
                if ((!string.IsNullOrEmpty(info.Name) && info.Name.Contains( value)))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            });
        }
    }
}

效果图:

链接: https://pan.baidu.com/s/1MRQjgvAuY5M96cbBi895-g

密码: i4fh
版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:Xamarin.Android 自定义VideoView 进度条 | ·下一条:Xamarin.Android PlotView控件之折线图+柱形图+饼图使用

Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有