自定义适配器中的'无法转换为'错误

时间:2015-01-22 14:57:28

标签: android android-fragments

我有一个带有幻灯片的应用程序,里面有一些片段

我使用了两个自定义适配器,它们都是相同的代码,只有其中一个拥有的图像比另一个更多

customadapter文件是:

package ir.monocode.az;

import ir.monocode.az.R;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/********* Adapter class extends with BaseAdapter and implements with OnClickListener ************/
public class CustomAdapter extends BaseAdapter implements OnClickListener {

    /*********** Declare Used Variables *********/
    private Context activity;
    private ArrayList data;
    private static LayoutInflater inflater=null;
    public Resources res;
    CatsModel tempValues=null;
    int i=0;
    Context context; 
    Typeface face;

    /*************  CustomAdapter Constructor *****************/
    public CustomAdapter(Context context, ArrayList d,Resources resLocal) {

        /********** Take passed values **********/
        activity = context;
        data=d;
        res = resLocal;

        /***********  Layout inflator to call external xml layout () **********************/
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    /******** What is the size of Passed Arraylist Size ************/
    public int getCount() {

        if(data.size()<=0)
            return 1;
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    /********* Create a holder to contain inflated xml file elements ***********/
    public static class ViewHolder{

        public TextView text;
        public ImageView image;

    }

    /*********** Depends upon data size called for each row , Create each ListView row ***********/
    public View getView(int position, View convertView, ViewGroup parent) {

        View vii=convertView;
        ViewHolder holder;

        if(convertView==null){ 

            /********** Inflate tabitem.xml file for each row ( Defined below ) ************/
            vii = inflater.inflate(R.layout.catitems, null); 


            /******** View Holder Object to contain tabitem.xml file elements ************/
            holder=new ViewHolder();
            holder.text=(TextView)vii.findViewById(R.id.ctextView1);
            holder.image=(ImageView)vii.findViewById(R.id.cimageView2);

            Typeface typeface = Typeface.createFromAsset(holder.text.getContext().getAssets(),
                    "fonts/B YEKAN.TTF");
            holder.text.setTypeface(typeface);
            /************  Set holder with LayoutInflater ************/
            vii.setTag(holder);
        }
        else  
            holder=(ViewHolder)vii.getTag();

        if(data.size()<=0)
        {
            holder.text.setText("No Data");

        }
        else
        {
            /***** Get each Model object from Arraylist ********/
            tempValues=null;
            tempValues = (CatsModel) data.get(position);

            /************  Set Model values in Holder elements ***********/
             holder.text.setText(tempValues.getCatName());
             int resID = activity.getResources().getIdentifier("icon"+tempValues.getImage() , "drawable", activity.getPackageName());
             holder.image.setImageResource(resID);

             /******** Set Item Click Listener for LayoutInflater for each row ***********/
             vii.setOnClickListener(new OnItemClickListener(position));
        }
        return vii;
    }

    @Override
    public void onClick(View v) {
            Log.v("CustomAdapter", "=====Row button clicked");


    }

    /********* Called when Item click in ListView ************/
    private class OnItemClickListener  implements OnClickListener{           
        private int mPosition;

        OnItemClickListener(int position){
         mPosition = position;
       }

       @Override
       public void onClick(View arg0) {
           MainActivity sct = (MainActivity)activity;
        sct.customonItemClick(mPosition);

       }



    }   
}

articleadapter是:

package ir.monocode.az;

import ir.monocode.azmoonEstekhdami.R;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/********* Adapter class extends with BaseAdapter and implements with OnClickListener ************/
public class ArticleAdapter extends BaseAdapter implements OnClickListener {

    /*********** Declare Used Variables *********/
    private Context activity;
    private ArrayList data;
    private static LayoutInflater inflater=null;
    public Resources res;
    ArticlesModel tempValues=null;
    int i=0;
    Context context; 
    Typeface face;

    /*************  CustomAdapter Constructor *****************/
    public ArticleAdapter(Context context, ArrayList d,Resources resLocal) {

        /********** Take passed values **********/
        activity = context;
        data=d;
        res = resLocal;

        /***********  Layout inflator to call external xml layout () **********************/
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    /******** What is the size of Passed Arraylist Size ************/
    public int getCount() {

        if(data.size()<=0)
            return 1;
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    /********* Create a holder to contain inflated xml file elements ***********/
    public static class ViewHolder{

        public TextView text;

    }

    /*********** Depends upon data size called for each row , Create each ListView row ***********/
    public View getView(int position, View convertView, ViewGroup parent) {

        View vi=convertView;
        ViewHolder holder;

        if(convertView==null){ 

            /********** Inflate tabitem.xml file for each row ( Defined below ) ************/
            vi = inflater.inflate(R.layout.articleitems, null); 


            /******** View Holder Object to contain tabitem.xml file elements ************/
            holder=new ViewHolder();
            holder.text=(TextView)vi.findViewById(R.id.atextView1);

            Typeface typeface = Typeface.createFromAsset(holder.text.getContext().getAssets(),
                    "fonts/B YEKAN.TTF");
            holder.text.setTypeface(typeface);
            /************  Set holder with LayoutInflater ************/
            vi.setTag(holder);
        }
        else  
            holder=(ViewHolder)vi.getTag();

        if(data.size()<=0)
        {
            holder.text.setText("No Data");

        }
        else
        {
            /***** Get each Model object from Arraylist ********/
            tempValues=null;
            tempValues = (ArticlesModel) data.get(position);

            /************  Set Model values in Holder elements ***********/
             holder.text.setText(tempValues.getArticleName());


        }
        return vi;
    }

    @Override
    public void onClick(View v) {
            Log.v("CustomAdapter", "=====Row button clicked");


    }

    /********* Called when Item click in ListView ************/
    private class OnItemClickListener  implements OnClickListener{           
        private int mPosition;

        OnItemClickListener(int position){
         mPosition = position;
       }

       @Override
       public void onClick(View arg0) {
           MainActivity sct = (MainActivity)activity;
        sct.articleItemClick(mPosition);

       }

    }


}

当然我的项目风格有两个xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/cLinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:gravity="right"
    android:orientation="vertical"
    android:paddingBottom="10dp" >

    <LinearLayout
        android:id="@+id/cLinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:gravity="right" >

        <TextView
            android:id="@+id/ctextView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical|center_horizontal"
            android:paddingRight="10dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#215e76"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/cimageView2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:maxHeight="20dp"
            android:maxWidth="20dp"
            android:padding="5dp"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/cimageView1"
            android:layout_width="10dp"
            android:layout_height="match_parent"
            android:background="#ffc20e" />

    </LinearLayout>

</LinearLayout>

而另一个是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/aLinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:gravity="right"
    android:orientation="vertical"
    android:paddingBottom="10dp" >

    <LinearLayout
        android:id="@+id/aLinearLayout3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:gravity="right" >

        <TextView
            android:id="@+id/atextView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical|center_horizontal"
            android:paddingRight="10dp"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#215e76"
            android:textSize="20sp" />

        <ImageView
            android:id="@+id/aimageView1"
            android:layout_width="10dp"
            android:layout_height="match_parent"
            android:background="#ffc20e" />

    </LinearLayout>

</LinearLayout>

现在当我运行程序时 用customadapter填充的片段非常好 但由articlesadapter填充的片段有错误

java.lang.ClassCastException: ir.monocode.az.ArticlesModel cannot be cast to ir.monocode.az.CatsModel

我必须如何解决我的问题? 你能帮我吗? 谢谢

1 个答案:

答案 0 :(得分:0)

你可以用多态解决。使用接口BaseAdapter

替换失败的声明(转换错误的声明)

这样的事情(你的相关代码没有发布,所以这只是猜测)

CatsModel it = retrieveMyCustomAdapter();

变为

 BaseAdapter it = retrieveMyCustomAdapter();

更正式的方法是声明一个扩展BaseAdapter的抽象类,并使CatsModel和ArticlesModel继承自这个新类。