我在单个布局页面中有 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; }
}
我是安卓开发的新手。如果我的理解有误,请指导我。