spinner自定义适配器视图持有者异常

时间:2016-03-16 11:26:37

标签: android

我正在使用自定义适配器来填充我的微调器,但在getDropDownView中我正在使用

java.lang.ClassCastException: java.lang.Integer cannot be cast to com.votingsystem.tsiro.adapters.FirmNamesSpnrNothingSelectedAdapter$ViewHolder

FirmNamesSpnrNothingSelectedAdapter:

public class FirmNamesSpnrNothingSelectedAdapter extends ArrayAdapter<FirmNameWithID> {

private static final String debugTag = FirmNamesSpnrNothingSelectedAdapter.class.getSimpleName();
protected static final int EXTRA = 1;
protected SpinnerAdapter adapter;
protected Context context;
protected int nothingSelectedLayout;
protected int nothingSelectedDropdownLayout;
protected LayoutInflater layoutInflater;
protected ArrayList<FirmNameWithID> firmNameWithIDs;

public FirmNamesSpnrNothingSelectedAdapter(Context context, int resource, ArrayList<FirmNameWithID> objects) {
    super(context, resource, objects);
    this.firmNameWithIDs                = objects;
    //this.adapter                        = spinnerAdapter;
    this.nothingSelectedLayout          = resource;
    this.context                        = context;
    this.layoutInflater                 = LayoutInflater.from(context);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return getNothingSelectedView(parent);
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    Log.e(debugTag,"getDropDownView: "+position+" "+convertView);
    View view = convertView;
    ViewHolder viewHolder;
    if (convertView == null) {
        view                 = layoutInflater.inflate(R.layout.spinner_dropdown_item, parent, false);
        viewHolder           = new ViewHolder(view);
        view.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) view.getTag();
    }
    FirmNameWithID firmNameWithID = firmNameWithIDs.get(position);
    viewHolder.dropdownItemTtv.setText(firmNameWithID.getFirm_name());
    viewHolder.dropdownItemTtv.setTag(firmNameWithID.getId());

    return view;
}

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

protected View getNothingSelectedView(ViewGroup parent) {
    return layoutInflater.inflate(nothingSelectedLayout, parent, false);
}

static class ViewHolder {
    TextView dropdownItemTtv;

    public ViewHolder(View view) {
        dropdownItemTtv = (TextView) view.findViewById(R.id.spnrDropdownItem);
    }
}
}

和spinner实例化:

 @Override
public void onSuccessfulFirmNamesSpnrLoad(ArrayList<FirmNameWithID> firmNameWithIDArrayList) {
 pickFirmSpnr.setAdapter(new FirmNamesSpnrNothingSelectedAdapter(getActivity(), R.layout.spinner_selection_item, firmNameWithIDArrayList));

}

谁能告诉我我做错了什么?  view = layoutInflater.inflate(R.layout.spinner_dropdown_item,parent,false); 和 dropdownItemTtv =(TextView)view.findViewById(R.id.spnrDropdownItem); 包含相同的视图

模特课:

@Override
public void populateFirmNamesSpnr(final ArrayList<FirmNameWithID> firmNameWithIDArrayList, final RegisterInputFieldFinishedListener registerInputFieldFinishedListener) {
    Call<Firm> call = apiService.getFirmNames("getFirmNames");
    call.enqueue(new Callback<Firm>() {
        @Override
        public void onResponse(Response<Firm> response, Retrofit retrofit) {
            List<Firm.FirmElement> firmElementList = response.body().getFirm_element();
            for (int i = 0; i < firmElementList.size(); i++) {
                Log.d(debugTag, "firm_id: " + firmElementList.get(i).getFirm_id() + " firm_name: " + firmElementList.get(i).getFirm_name());
                firmNameWithIDArrayList.add(new FirmNameWithID(firmElementList.get(i).getFirm_name(), firmElementList.get(i).getFirm_id()));
            }
            registerInputFieldFinishedListener.onSuccessfirmNamesSpnrLoad(firmNameWithIDArrayList);
        }
        @Override
        public void onFailure(Throwable t) {
            registerInputFieldFinishedListener.onFailurefirmNamesSpnrLoad();
        }
    });
}

FirmNameWithID class:

public class FirmNameWithID {

private String firm_name;
private int id;

public FirmNameWithID(String firm_name, int id) {
    this.firm_name  = firm_name;
    this.id         = id;
}

public String getFirm_name() { return firm_name; }

public void setFirm_name(String firm_name) { this.firm_name = firm_name; }

public int getId() { return id; }

public void setId(int id) { this.id = id; }

@Override
public String toString() {
    return firm_name;
}
}

spinner_dropdown_item.xml

<com.rey.material.widget.TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/spnrDropdownItem" android:layout_width="match_parent" android:layout_height="34dp" android:paddingLeft="8dp" android:singleLine="true" android:textSize="12sp" android:ellipsize="marquee" style="?android:attr/spinnerDropDownItemStyle" />

1 个答案:

答案 0 :(得分:0)

这些代码存在问题

viewHolder.dropdownItemTtv.setText(firmNameWithID.getFirm_name());
viewHolder.dropdownItemTtv.setTag(firmNameWithID.getId());

检查firmNameWithID.getFirm_name()firmNameWithID.getId()返回字符串值,如果返回的不是字符串,则需要将其转换为字符串并设置为文本视图。