ClassCastException:无法将字符串强制转换为

时间:2017-06-23 18:44:06

标签: android android-recyclerview adapter classcastexception

我的代码中有错误。

logcat的:

  

java.lang.ClassCastException:java.lang.String无法强制转换为   com.example.aymen.schoolmanager.dates                                                                                        在   com.example.aymen.schoolmanager.Adapter.onBindViewHolder(Adapter.java:68)                                                                                        在   com.example.aymen.schoolmanager.Adapter.onBindViewHolder(Adapter.java:20)                                                                                        在   android.support.v7.widget.RecyclerView $ Adapter.onBindViewHolder(RecyclerView.java:6356)                                                                                        在   android.support.v7.widget.RecyclerView $ Adapter.bindViewHolder(RecyclerView.java:6389)                                                                                        在   android.support.v7.widget.RecyclerView $ Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)                                                                                        在   android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)                                                                                        在   android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5440)                                                                                        在   android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5436)                                                                                        在   android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2224)                                                                                        在   android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)                                                                                        在   android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)                                                                                        在   android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)                                                                                        在   android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)                                                                                        在   android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3312)                                                                                        在   android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3844)                                                                                        在android.view.View.layout(View.java:17637)                                                                                        在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                                        在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)                                                                                        在android.view.View.layout(View.java:17637)                                                                                        在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                                        在android.support.v4.view.ViewPager.onLayout(ViewPager.java:1795)                                                                                        在android.view.View.layout(View.java:17637)                                                                                        在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                                        在   android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131)                                                                                        在   android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)                                                                                        在   android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1391)                                                                                        在   android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:870)                                                                                        在android.view.View.layout(View.java:17637)                                                                                        在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                                        在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                                        在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                                        在android.view.View.layout(View.java:17637)                                                                                        在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                                        在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)                                                                                        在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)                                                                                        在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)                                                                                        在android.view.View.layout(View.java:17637)                                                                                        在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                                        在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                                        在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                                        在android.view.View.layout(View.java:17637)                                                                                        在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                                        在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)                                                                                        在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)                                                                                        在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)                                                                                        在android.view.View.layout(View.java:17637)                                                                                        在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                                        在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)                                                                                        在android.widget.FrameLayout.onLayout(FrameLayout.java:261)                                                                                        在com.android.internal.policy.DecorView.onLayout(DecorView.java:726)                                                                                        在android.view.View.layout(View.java:17637)                                                                                        在android.view.ViewGroup.layout(ViewGroup.java:5575)                                                                                        在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2346)                                                                                        在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2068)   06-22 10:11:41.107 22646-22646 / com.example.aymen.schoolmanager   E / AndroidRuntime:at   android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)                                                                                        在   android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6337)                                                                                        在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:874)                                                                                        在android.view.Choreographer.doCallbacks(Choreographer.java:686)                                                                                        在android.view.Choreographer.doFrame(Choreographer.java:621)                                                                                        在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:860)                                                                                        在android.os.Handler.handleCallback(Handler.java:751)                                                                                        在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                        在android.os.Looper.loop(Looper.java:154)                                                                                        在android.app.ActivityThread.main(ActivityThread.java:6119)                                                                                        at java.lang.reflect.Method.invoke(Native Method)                                                                                        在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

第68行指向:

holder.textViewHead.setText(idd.get(position).getSubject());

第20行指向:

class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>

这是我的代码:

我的数据库类处理程序:

public class DBhandler extends SQLiteOpenHelper {
    private static final String databas_name="newdata.db";
    public static final int databas_version=1;
    public static String ID="id";
    int id;
    SQLiteDatabase dj;
    ArrayList<dates> er=new ArrayList<dates>();
    SQLiteDatabase db;
    DBhandler helper;
    String TYPE="type";
    String sub="Subject";
    String Title="Title";
    String det="Detail";
    String TabN="ClassExams";
    int i;

    public DBhandler(Context context)
    {
        super(context,databas_name,null,databas_version);
    }




    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE if not EXISTS ClassExams(id INTEGER primary key,type TEXT,Subject TEXT,Title TEXT,Detail TEXT) ");
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP table if EXISTS ClassExams");
        onCreate(db);

    }



    public void insert(String type, String sub, String title, String detaill)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("type",type);
        contentValues.put("Subject",sub);
        contentValues.put("Title",title);
        contentValues.put("Detail",detaill);
        db.insert("ClassExams",null,contentValues);
        db.close();



     }


    public ArrayList getAs(String Type)
    {


        String DATABASE_TABLE="ClassExams";
        String type="type";
        String quer = "SELECT * FROM " + DATABASE_TABLE + " WHERE " + type + "='" + Type+"'";
        ArrayList arrayList=new ArrayList();
        SQLiteDatabase db=this.getReadableDatabase();

        Cursor res=db.rawQuery(quer,null);
        res.moveToFirst();
        while (res.isAfterLast()==false){
            arrayList.add(res.getString(res.getColumnIndex("id")));

            //  arrayList.add(res.getString(res.getColumnIndex("Subject")));
            // arrayList.add(res.getString(res.getColumnIndex("Title")));
            // arrayList.add(res.getString(res.getColumnIndex("Detail")));
            res.moveToNext();

        }
        return arrayList;

    }


    public void deleteNote(int x){
        String TABLE_NAME="ClassExams";
        String ID="id";
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_NAME, ID + " = ?",new String[] {String.valueOf(x)});
    }





} 

我的适配器类:

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
    Context activity;
    private ArrayList<dates> idd;

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView textViewHead;
        public TextView textViewDown;
        public TextView buttonViewOption;
        public int position;
        public ViewHolder(View itemView) {
            super(itemView);

            textViewHead = (TextView) itemView.findViewById(id.text1);

            textViewDown = (TextView) itemView.findViewById(id.text2);

            buttonViewOption = (TextView) itemView.findViewById(R.id.textViewOptions);
        }
    }


    public Adapter (Context context, ArrayList<dates> id) {
      this.activity=context;
         this.idd=id;

    }
    @Override
   public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(activity);
      //  View row=inflater.inflate(layout.costum_row,parent,false);
        View view = LayoutInflater.from(parent.getContext()).inflate(layout.costum_row, null);

        ViewHolder holder=new ViewHolder(view);
        return holder;
    }
    public void onBindViewHolder(final Adapter.ViewHolder holder, final int position) {
        //  dates object=idd.get(position);
        // String firstText = object.getDetail();
        // String secondText = object.getSubject();


        holder.textViewDown.setText("Somthing");
        holder.textViewHead.setText(idd.get(position).getSubject());
        holder.buttonViewOption.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                PopupMenu popup = new PopupMenu(activity, holder.buttonViewOption);

                popup.inflate(R.menu.option_menu);

                popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {

                        switch (item.getItemId()) {
                            case id.Update:

                                break;
                            case R.id.menu2:

                                break;
                            case id.Delete:

                                delete();
//db.deleteNote(Integer.parseInt(j));

                                // db.deleteNote(getid);
                        }
                        return false;
                    }
                });
                //displaying the popup
                popup.show();
            }

            public void delete() {
                idd.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, idd.size());
                holder.itemView.setVisibility(View.GONE);
            }

        });
    }

    @Override
    public int getItemCount() {
        return idd.size();}}

我的课程:

public class dates {
    String Subject,Title,Detail,type;
     int id;

    public dates(String subject, String title, String detail, String type, int id) {
        this.Subject = subject;
        this.Title = title;
        this.Detail = detail;
        this.type = type;
        this.id = id;
    }

    dates()
    {

    }


    public String getSubject() {
        return Subject;
    }

    public  void setSubject(String subject) {
        Subject= subject;
    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String title) {
        Title = title;
    }

    public String getDetail() {
        return Detail;
    }

    public void setDetail(String detail) {
        Detail = detail;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getId() {
        return id;
    }

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

1 个答案:

答案 0 :(得分:0)

此处并未显示所有必需的代码,但我假设来自ArrayList DBHandler.getAs()的数组列表作为id参数传递给Adapter构造函数

public Adapter (Context context, ArrayList<dates> id)

应该有关于&#34;未经检查或不安全的操作的编译器警告&#34;。

getAs返回的实际上是一个包含String个对象的列表,现在这被解释为包含dates个对象的列表,只要其中一个对象导致类转换异常访问元素。

可以从中吸取两个教训:

  1. 不要忽略编译器警告
  2. 不要使用原始收集类