0

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)?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Rio
  • 1
  • 2
  • 1
    If I understand your question correctly, I think you need to use yourSpinner.setSelection(int pos); – user1086500 Mar 26 '17 at 09:10
  • @user1086500 - (int pos) is a position, right? No, I need to set the default value based on countryId not position, because if I disable some countries the position will change. – Rio Mar 27 '17 at 08:23
  • What about this? See Adams answer. http://stackoverflow.com/questions/8769368/how-to-set-position-in-spinner – user1086500 Mar 31 '17 at 15:33

1 Answers1

-1

i think that you want to set country position by countryID . Then you have sort you list then assign to new list then set adaptor.