RecyclerView ViewHolder转到了错误的自定义适配器

时间:2017-01-15 11:40:19

标签: android android-recyclerview realm android-adapter

基于我实现的相同示例,我创建了一个我的RecyclerView的自定义适配器。代码是这样的:

public class AirportsAdapter extends RealmRecyclerViewAdapter<Airport,RecyclerView.ViewHolder> implements Filterable
{
Context context;
public static final int FILTER_MODE_A = 0;
public static final int FILTER_MODE_B = 1;
OrderedRealmCollection<Airport> listAirports;
RealmResults<Airport> nearby;
Realm realm;

int NEARBY = 0;
int nearbySize;
boolean COUNTRY = false;
int ALL = 0;

public AirportsAdapter(Context context, OrderedRealmCollection<Airport>airports, RealmResults<Airport> nearby, Realm realm)
{
    super(context,airports, true);
    this.context = context;
    this.listAirports = airports;
    this.realm = realm;
    this.nearby = nearby;

    nearbySize = nearby.size();
    ALL = ALL + nearbySize;

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{

    //sono tutti uguali no?
    //sisi l'avevo messo così solo per non fare un mega or

   if(viewType == 0 || viewType== ALL || (viewType == 0 && (COUNTRY)))
    {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.input_custom_item, parent, false);
        MenuHolder menuHolder = new MenuHolder(view);
        return menuHolder;
    }

    else
    {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.airport_show, parent, false);
        AirportClass holder = new AirportClass(view);
        return holder;
    }

}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
   if(position==0)
    {
        MenuHolder mHolder = (MenuHolder)holder;
        mHolder.type.setText("Nearby");
        mHolder.type.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Avenir-Medium.ttf"),Typeface.BOLD);

    }

   else if(position == ALL)
   {
       MenuHolder mHolder = (MenuHolder)holder;
       mHolder.type.setText("All");
       mHolder.type.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Avenir-Medium.ttf"),Typeface.BOLD);
   }

   else if(position == 0 && (COUNTRY))
   {
       MenuHolder mHolder = (MenuHolder)holder;
       mHolder.type.setText("Country");
       mHolder.type.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Avenir-Medium.ttf"),Typeface.BOLD);
   }

   else
   {
        AirportClass mHolder = (AirportClass) holder;

        Airport airport = listAirports.get(position);

        String country = airport.getIsoCountry().toLowerCase();


        if (country.equals("do"))
            country = "dom";

        int imgID = context.getResources().getIdentifier(country, "drawable", context.getPackageName());



        Glide.with(context).load(imgID).into(mHolder.image);

        mHolder.des.setText(airport.getIdent());
        mHolder.name.setText(airport.getName());

        mHolder.des.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Avenir-Book.ttf"), Typeface.BOLD);
        mHolder.name.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Avenir-Book.ttf"));
    }
}

public void filterResults(String text, int filterMode)
{
    String txt = text;
    text = text == null ? null : text.toLowerCase().trim();
    if(text == null || "".equals(text))
    {
        listAirports = realm.where(Airport.class).findAll();
    }
    else
    {
        if(filterMode == FILTER_MODE_A)
        {
            listAirports = realm.where(Airport.class).contains("name", text, Case.INSENSITIVE).findAll();
            COUNTRY = false;
        }
        if(filterMode == FILTER_MODE_B)
        {
            System.out.print(text);
            COUNTRY = true;
            Countries country = realm.where(Countries.class).equalTo("name",txt).findFirst();
            listAirports = realm.where(Airport.class).equalTo("isoCountry", country.getCode(), Case.INSENSITIVE).findAll();

        }
    }
    updateData(listAirports);
}


public Filter getFilter(int filterMode)
{
    AirportFilter filter = new AirportFilter(this, filterMode);
    return filter;
}

@Override
public Filter getFilter() {

    return null;
}


private class AirportFilter extends Filter
{
    private final AirportsAdapter adapter;
    private int filterMode;

    private AirportFilter(AirportsAdapter adapter, int filterMode)
    {
        super();
        this.adapter = adapter;
        this.filterMode = filterMode;
    }


    @Override
    protected FilterResults performFiltering(CharSequence constraint)
    {
        if (filterMode == FILTER_MODE_A)
            return new Filter.FilterResults();
        if (filterMode == FILTER_MODE_B)
            return new Filter.FilterResults();

        return new Filter.FilterResults();
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results)
    {
        adapter.filterResults(constraint.toString(),filterMode);
    }
}


private class AirportClass extends RecyclerView.ViewHolder
{
    TextView name, des;
    ImageView image;

    public AirportClass(View itemView)
    {
        super(itemView);

        name = (TextView)itemView.findViewById(R.id.name);
        des = (TextView)itemView.findViewById(R.id.item);
        image = (ImageView)itemView.findViewById(R.id.color);

    }
}

private class MenuHolder extends RecyclerView.ViewHolder
{
    TextView type;
    public MenuHolder(View itemView)
    {
        super(itemView);
        type = (TextView)itemView.findViewById(R.id.init);
    }
}

}

它类似于其他适配器,但是当我调试它时,我有这个错误:

 Process: com.example, PID: 1663
              java.lang.ClassCastException: com.example.Airports.AirportsAdapter$MenuHolder cannot be cast to com.example.Airports.AirportsAdapter$AirportClass
                  at com.example.Airports.AirportsAdapter.onBindViewHolder(AirportsAdapter.java:118)
                  at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6279)
                  at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6312)
                  at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5258)
                  at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5521)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5363)
                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5359)
                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2141)
                  at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1525)
                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1488)
                  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
                  at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3506)
                  at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:2969)
                  at android.view.View.measure(View.java:19730)
                  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                  at android.view.View.measure(View.java:19730)
                  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                  at android.view.View.measure(View.java:19730)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                  at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
                  at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
                  at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1375)
                  at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
                  at android.view.View.measure(View.java:19730)
                  at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1081)
                  at android.view.View.measure(View.java:19730)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                  at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                  at android.view.View.measure(View.java:19730)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                  at android.view.View.measure(View.java:19730)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                  at android.view.View.measure(View.java:19730)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                  at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
                  at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
                  at android.view.View.measure(View.java:19730)
                  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6120)
                  at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                  at com.android.internal.policy.DecorView.onMeasure(DecorView.java:687)
                  at android.view.View.measure(View.java:19730)
                  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2271)

这是我第一次遇到这个错误,我尝试解决它,但我只能通过以下方式解决它:

  1. 删除MenuHolder并仅使用AirportClass或
  2. 删除AirportClass并仅使用MenuHolder
  3. 有没有办法解决这个错误?

    编辑:这是新代码,它不会给出以前的错误,但会保持位置状态==0ç

    public class AirportsAdapter extends RealmRecyclerViewAdapter<Airport,RecyclerView.ViewHolder> implements Filterable
    {
    Context context;
    public static final int FILTER_MODE_A = 0;
    public static final int FILTER_MODE_B = 1;
    OrderedRealmCollection<Airport> listAirports;
    RealmResults<Airport> nearby;
    Realm realm;
    
    int NEARBY = 0;
    int nearbySize;
    boolean COUNTRY = false;
    int ALL = 0;
    
    public AirportsAdapter(Context context, OrderedRealmCollection<Airport>airports, RealmResults<Airport> nearby, Realm realm)
    {
        super(context,airports, true);
        this.context = context;
        this.listAirports = airports;
        this.realm = realm;
        this.nearby = nearby;
    
        nearbySize = nearby.size();
        ALL = ALL + nearbySize;
    
    }
    
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
    
        //sono tutti uguali no?
        //sisi l'avevo messo così solo per non fare un mega or
    
       if(viewType == NEARBY )
        {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.input_custom_item, parent, false);
            MenuHolder menuHolder = new MenuHolder(view);
            return menuHolder;
        }
    
        else
        {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.airport_show, parent, false);
            AirportClass holder = new AirportClass(view);
            return holder;
        }
    
    }
    
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
       if(getItemViewType(position)==NEARBY)
        {
            Log.i("ivt",String.valueOf(position));
            MenuHolder mHolder = (MenuHolder)holder;
            mHolder.type.setText("Nearby");
            mHolder.type.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Avenir-Medium.ttf"),Typeface.BOLD);
    
        }
    
    /*   else if(getItemViewType(position) == ALL)
       {
           MenuHolder mHolder = (MenuHolder)holder;
           mHolder.type.setText("All");
           mHolder.type.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Avenir-Medium.ttf"),Typeface.BOLD);
       }
    
       else if(getItemViewType(position) == 0 && (COUNTRY))
       {
           MenuHolder mHolder = (MenuHolder)holder;
           mHolder.type.setText("Country");
           mHolder.type.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Avenir-Medium.ttf"),Typeface.BOLD);
       }
       */
       else
       {
            AirportClass mHolder = (AirportClass) holder;
    
            Airport airport = listAirports.get(position);
    
            String country = airport.getIsoCountry().toLowerCase();
    
    
            if (country.equals("do"))
                country = "dom";
    
            int imgID = context.getResources().getIdentifier(country, "drawable", context.getPackageName());
    
    
    
            Glide.with(context).load(imgID).into(mHolder.image);
    
            mHolder.des.setText(airport.getIdent());
            mHolder.name.setText(airport.getName());
    
            mHolder.des.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Avenir-Book.ttf"), Typeface.BOLD);
            mHolder.name.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/Avenir-Book.ttf"));
        }
    }
    
    
    @Override
    public int getItemViewType(int position)
    {
        return super.getItemViewType(position);
    }
    
    @Override
    public int getItemCount()
    {
        if(nearbySize == 0)
        {
            return 1 + listAirports.size();
        }
        else
        {
          return 2 + nearbySize + listAirports.size();
        }
    }
    
    public void filterResults(String text, int filterMode)
    {
        String txt = text;
        text = text == null ? null : text.toLowerCase().trim();
        if(text == null || "".equals(text))
        {
            listAirports = realm.where(Airport.class).findAll();
        }
        else
        {
            if(filterMode == FILTER_MODE_A)
            {
                listAirports = realm.where(Airport.class).contains("name", text, Case.INSENSITIVE).findAll();
                COUNTRY = false;
            }
            if(filterMode == FILTER_MODE_B)
            {
                System.out.print(text);
                COUNTRY = true;
                Countries country = realm.where(Countries.class).equalTo("name",txt).findFirst();
                listAirports = realm.where(Airport.class).equalTo("isoCountry", country.getCode(), Case.INSENSITIVE).findAll();
    
            }
        }
        updateData(listAirports);
    }
    
    
    public Filter getFilter(int filterMode)
    {
        AirportFilter filter = new AirportFilter(this, filterMode);
        return filter;
    }
    
    @Override
    public Filter getFilter() {
    
        return null;
    }
    
    
    private class AirportFilter extends Filter
    {
        private final AirportsAdapter adapter;
        private int filterMode;
    
        private AirportFilter(AirportsAdapter adapter, int filterMode)
        {
            super();
            this.adapter = adapter;
            this.filterMode = filterMode;
        }
    
    
        @Override
        protected FilterResults performFiltering(CharSequence constraint)
        {
            if (filterMode == FILTER_MODE_A)
                return new Filter.FilterResults();
            if (filterMode == FILTER_MODE_B)
                return new Filter.FilterResults();
    
            return new Filter.FilterResults();
        }
    
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results)
        {
            adapter.filterResults(constraint.toString(),filterMode);
        }
    }
    
    
    private class AirportClass extends RecyclerView.ViewHolder
    {
        TextView name, des;
        ImageView image;
    
        public AirportClass(View itemView)
        {
            super(itemView);
    
            name = (TextView)itemView.findViewById(R.id.name);
            des = (TextView)itemView.findViewById(R.id.item);
            image = (ImageView)itemView.findViewById(R.id.color);
    
        }
    }
    
    private class MenuHolder extends RecyclerView.ViewHolder
    {
        TextView type;
        public MenuHolder(View itemView)
        {
            super(itemView);
            type = (TextView)itemView.findViewById(R.id.init);
        }
     }
    
    }
    

    由于

0 个答案:

没有答案