I use a custom spinner to display generated country list from mysql database using json.
My spinner container:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_countries"></Spinner>
My custom spinner content list:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingRight="1dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/countryId"
android:visibility="invisible"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/countryName"/>
</RelativeLayout>
So, there are two values for each country (countryId and countryName).
And, I use a simple BaseAdapter to put the country list to the spinner content list:
public class AddressCountriesAdapter extends BaseAdapter {
private List<AddressResponse.CountriesBean> mCountryitem;
private Context mContext;
private LayoutInflater inflater;
public AddressCountriesAdapter(Context mContext, List<AddressResponse.CountriesBean> mCountryitem) {
this.mContext = mContext;
this.mCountryitem = mCountryitem;
}
@Override
public int getCount() {
return mCountryitem.size();
}
@Override
public Object getItem(int position) {
return mCountryitem.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String empty = "";
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Countries
View listView = inflater.inflate(R.layout.content_country_list, parent, false);
AddressResponse.CountriesBean item = (AddressResponse.CountriesBean) getItem(position);
TextView country_id = (TextView) listView.findViewById(R.id.countryId);
TextView name = (TextView) listView.findViewById(R.id.countryName);
country_id.setText(item.getCountry_id());
name.setText(Html.fromHtml(item.getName()));
return listView;
}
}
My problem is, how to set default value based on countryId (NOT position)?