我在单个布局页面中有4个微调器,每个都有来自服务器的不同数据集。我曾使用Base-Adapter类(GETVIEW)方法来设置显示成员部分。
我的问题是:是否可以将单个Base-Adapter类用于多个微调控件?如何在GETVEW方法中设置不同的显示成员部分?
注意:我们正在使用WCF RESTful服务来获取在线数据。
在下面找到Base Adapter中的示例代码。
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;
}
}
Activitiy Class:
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; }
}
我是andriod开发的新手。如果我的理解是错误的,请指导我。
答案 0 :(得分: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);
}
....
....
请注意,在任何一种情况下,您只需要一个适配器及其多个实例。希望这能为你解决问题。