0

我在单个布局页面中有 4 个微调器,每个微调器都有来自服务器的不同数据集。我使用 Base-Adapter 类 (GETVIEW) 方法来设置显示成员部分。

我的问题是:是否可以将单个 Base-Adapter 类用于多个微调器控件?如何在 GETVEW 方法中设置不同的显示成员部分?

注意:我们使用 WCF RESTful 服务来获取在线数据。

在下面找到基本适配器的示例代码。

class EyeColorAdapter : BaseAdapter<MMS>
{
    List<MMS> items;
    Activity context;
    public EyeColorAdapter(Activity context, IEnumerable<MMS> items)
        : base()
    {
        this.context = context;
        this.items = items.ToList();
    }
    public override long GetItemId(int position)
    {
        return position;
    }
    public override MMS this[int position]
    {
        get { return items[position]; }
    }
    public override int Count
    {
        get { return items.Count; }
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        if (view == null)
        {
            view = context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItem1, null);
        }
        // This is single spinner control display part. How to Check multiple spinner control condition here to set Display member part.
        view.FindViewById<TextView>(Android.Resource.Id.Text1).Text = items[position].WONO.ToString();
        view.FindViewById<TextView>(Android.Resource.Id.Text1).SetTextColor(Android.Graphics.Color.Black);
        return view;
    }
}

活动课:

  public class Activity1 : Activity
    {
        List<MMS> items1;
        protected override void OnCreate(Bundle bundle)
        {
            try
            {
                base.OnCreate(bundle);
                SetContentView(Resource.Layout.Main);
                var request = HttpWebRequest.Create("http://192.168.0.72/eFACiLiTYPhone/mobileservice/WinPhoneWCFService.svc/listingworkorder/x");
                request.ContentType = "application/json";
                request.Method = "GET";
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        var content = reader.ReadToEnd();
                        if (string.IsNullOrWhiteSpace(content))
                        {
                            Console.Out.WriteLine("Response contained empty body...");
                        }
                        else
                        {
                            Console.Out.WriteLine("Response Body: \r\n {0}", content);
                        }
                        List<MMS> myDeserializedObjList =
                            (List<MMS>)Newtonsoft.Json.JsonConvert.DeserializeObject(content, typeof(List<MMS>));
                        Spinner spinr = FindViewById<Spinner>(Resource.Id.spinner1);
                        spinr.Adapter = new EyeColorAdapter(this, myDeserializedObjList);
                    }
                }
                var request1 = HttpWebRequest.Create("http://192.168.0.72/eFACiLiTYPhone/mobileservice/WinPhoneWCFService.svc/listingworkorder/x");
                request1.ContentType = "application/json";
                request1.Method = "GET";
                using (HttpWebResponse response1 = request1.GetResponse() as HttpWebResponse)
                {
                    using (StreamReader reader1 = new StreamReader(response1.GetResponseStream()))
                    {
                        var content1 = reader1.ReadToEnd();
                        if (string.IsNullOrWhiteSpace(content1))
                        {
                            Console.Out.WriteLine("Response contained empty body...");
                        }
                        else
                        {
                            Console.Out.WriteLine("Response Body: \r\n {0}", content1);
                        }
                        List<MMS> myDeserializedObjList1 =
                            (List<MMS>)Newtonsoft.Json.JsonConvert.DeserializeObject(content1, typeof(List<MMS>));

                        Spinner spinr1 = FindViewById<Spinner>(Resource.Id.spinner2);
                        spinr1.Adapter = new EyeColorAdapter(this, myDeserializedObjList1);

                        items1 = myDeserializedObjList1;
                        int index = items1.IndexOf(items1.Where(x => x.WONO + "" == Convert.ToString("INDIA2085")).FirstOrDefault());
                        spinr1.SetSelection(index);
                    }
                }
            }
            catch (Exception ex)
            {
                Toast.MakeText(this, ex.InnerException.ToString(), ToastLength.Long);
            }
        }
    }
    public class MMS
    {
        public string WONO { get; set; }
        public string EquipmentNo { get; set; }
        public string Status { get; set; }
        public string JobDescription { get; set; }
        public object Raised { get; set; }
        public string Hier { get; set; }
    }

我是安卓开发的新手。如果我的理解有误,请指导我。

4

1 回答 1

0

这可以完成,您可以使用单个适配器实现来完成。

您的要求并不具体,因此我可以想到两种情况:

~ 您有 4 个显示不同数据的微调器,但使用相同的布局。

您只需要一个适配器类的 4 个不同实例。每个实例当然会有不同的数组对象作为其构造函数中的参数:

MyArrayAdapter adapter1 = new MyArrayAdapter(...., items1, .....);
....
....
MyArrayAdapter adapter4 = new MyArrayAdapter(...., items4, .....);

spinner1.setAdapter(adapter1);
....
....
spinner4.setAdapter(adapter4);

~ 第二种可能性是拥有 4 个微调器小部件,每个小部件都有其独特的布局。

在这种情况下,您需要自定义 getView() 方法。为了给您一个想法,您将在适配器的构造函数中传递一个标志。基于此标志,您将膨胀不同的视图:

if (flag == 1) {
    view = context.LayoutInflater.Inflate(R.layout.unique_layout_1, null);
} else if ( flag == 2) {
    view = context.LayoutInflater.Inflate(R.layout.unique_layout_1, null);
}
....
....

请注意,在任何一种情况下,您都只需要一个适配器和它的多个实例。希望这可以为您解决问题。

于 2013-07-22T09:31:10.817 回答