如何修复Spinner setAdapter()不起作用?

时间:2019-04-12 09:09:25

标签: java android sqlite spinner

当我调用包含该微调器的对话框时,我的android应用程序不断崩溃,我确定我的XML可以正常使用

我试图通过将代码作为注释来发现问题,结果发现问题出在setAdapter行上

请任何人帮助,它会很酷。

    CountryManager cm = new CountryManager(this);
    user = new UserAppManager(this).getUser();
    String countries = user.getCountries();
    ArrayList<Country> countriesListe = cm.getCountryByCodes(countries);
    Spinner countrieSpinner = mDialogStart.findViewById(R.id.sp_countries);
    CountriesListAdapter adapter = new CountriesListAdapter(this, R.layout.country_list_item, countriesListe);
    countrieSpinner.setAdapter(adapter);
    country = user.getDefaultCountry();
    int position = adapter.indexOf(country);
    Log.w(TAG, "countries::" + countries + "|| country::" + country + " || position::" + position);
    countrieSpinner.setSelection(position);
    countrieSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long l) {
            country = Objects.requireNonNull(adapter.getItem(pos)).getCode();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
            country = user.getDefaultCountry();
        }

适配器列表的代码

    public class CountriesListAdapter extends ArrayAdapter<Country> {
private final Context context;
//private final String[] values;
private ArrayList<Country> countries;

public CountriesListAdapter(Context context,int ressource,ArrayList<Country> countries) {
    super(context, ressource, new CountryManager(context).getAllPays());
    this.context = context;
    this.countries = countries;
}

@Override
public int getCount() {
    return countries.size();
}

@Override
public Country getItem(int i) {
    return countries.get(i);
}

public int getPosition(Country item) {
    return countries.indexOf(item);
}

@Override
public long getItemId(int i) {
    return i;
}

public int indexOf( String code) {
    for (int index = 0, count = getCount(); index < count; index++)
    {
        if (getItem(index).getCode().equals(code))  {
            return index;
        }
    }
    return -1;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.country_list_item, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.txtViewCountryName);
    //ImageView imageView = (ImageView) rowView.findViewById(R.id.imgViewFlag);

    String prefix = getItem(position).getPrefix();
    String code = getItem(position).getCode();
    String label = getItem(position).getLabel();
    textView.setText(" "+code);

    String buttonID = "drawable/" + code.toLowerCase();
    int resID = context.getResources().getIdentifier(buttonID, "id", context.getPackageName());
    Drawable img = context.getResources().getDrawable( resID);
    textView.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);

    return rowView;
}

@Override
public View getDropDownView(int position,  View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.country_list_item, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.txtViewCountryName);
    //ImageView imageView = (ImageView) rowView.findViewById(R.id.imgViewFlag);

    String prefix = getItem(position).getPrefix();
    String code = getItem(position).getCode();
    String label = getItem(position).getLabel();
    textView.setText(" "+code);
    textView.setGravity(Gravity.LEFT);

    String buttonID = "drawable/" + code.toLowerCase();
    int resID = context.getResources().getIdentifier(buttonID, "id", context.getPackageName());
    Drawable img = context.getResources().getDrawable( resID);
    textView.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);

    return rowView;
}

private String GetCountryZipCode(String ssid){
    Locale loc = new Locale("", ssid);

    return loc.getDisplayCountry().trim();
}
}

我希望从SQLite获取每个用户的国家/地区

1 个答案:

答案 0 :(得分:0)

问题是适配器是空指针。在调用 setContentView(<YourView>); 之前,您必须先调用 setAdapter