I found many similar questions which most I've seen are in Java, the thing is I don't know how to convert a Java code into C# Xamarin.
Here is the code I found: from here
month.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
List<String> s = Arrays.asList(getResources().getStringArray(R.array.item_day));
if (pos == 0 || pos == 2 || pos == 4 || pos == 8 || pos == 9
|| pos == 11) {
ArrayAdapter<String> dayadapter = new ArrayAdapter<String>(Latlondemo.this, android.R.layout.simple_spinner_item,s);
day.setAdapter(dayadapter);
} else if (pos == 1) {
s = s.subList(0,28);
ArrayAdapter<String> dayadapter = new ArrayAdapter<String>(Latlondemo.this, android.R.layout.simple_spinner_item,s);
day.setAdapter(dayadapter);
} else {
s = s.subList(0,30);
ArrayAdapter<String> dayadapter = new ArrayAdapter<String>(Latlondemo.this, android.R.layout.simple_spinner_item,s);
day.setAdapter(dayadapter);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
I'm specifically referring to this line:
List<String> s = Arrays.asList(getResources().getStringArray(R.array.item_day));
What is its C# counterpart?
Here are my codes so far:
spinner1 = FindViewById<Spinner>(Resource.Id.spinner1);
spinner2 = FindViewById<Spinner>(Resource.Id.spinner2);
var adapter1 = ArrayAdapter.CreateFromResource(
this, Resource.Array.building_array, Android.Resource.Layout.SimpleSpinnerItem);
var adapter2 = ArrayAdapter.CreateFromResource(
this, Resource.Array.level_array, Android.Resource.Layout.SimpleSpinnerItem);
adapter1.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
adapter2.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner1.Adapter = adapter1;
spinner2.Adapter = adapter2;
spinner1.ItemClick += (sender, e) => {
// code here
};
String resource:
<resources>
<string-array name="building_array">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
<string-array name="level_array">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
</resources>
The C# counterpart is
var arr = Resources.GetStringArray(Resource.Array.building_array);
This is valid in a Android Context, eg your Activity, because Resources
is a property of it.
If you want it to be a List<sting>
, just append a .ToList()
(needs: using System.Linq;
) to the call.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.