Home > other >  java.lang.ClassCastException: android.text.SpannableStringBuilder cannot be cast to com.ree.kms.Daka
java.lang.ClassCastException: android.text.SpannableStringBuilder cannot be cast to com.ree.kms.Daka

Time:04-01

I want to add EditText to the listview, but I can't do it smoothly. Please guide. If it is List<String>, it is normal, but DakaHinbanClass is used, How to write code that uses Class

The data is obtained from MySQL, and the content can be displayed normally at present. I want to change the production quantity directly in the listview, but when running, the position of line 195 always reports an error.

ErrorCode:

2022-03-31 15:58:14.780 13403-13403/com.ree.kms E/AndroidRuntime: FATAL EXCEPTION: main Process: com.ree.kms, PID: 13403 java.lang.ClassCastException: android.text.SpannableStringBuilder cannot be cast to com.ree.kms.DakaHinbanClass at com.ree.kms.DakaListAdapterClass$MyTextWatcher.afterTextChanged(DakaListAdapterClass.java:195) at android.widget.TextView.sendAfterTextChanged(TextView.java:8336) at android.widget.TextView.setText(TextView.java:4399) at android.widget.TextView.setText(TextView.java:4247) at android.widget.EditText.setText(EditText.java:90) at android.widget.TextView.setText(TextView.java:4222) at com.ree.kms.DakaListAdapterClass.getView(DakaListAdapterClass.java:101) at android.widget.AbsListView.obtainView(AbsListView.java:2474) at android.widget.ListView.makeAndAddView(ListView.java:1920) at android.widget.ListView.fillDown(ListView.java:717) at android.widget.ListView.fillFromTop(ListView.java:778) at android.widget.ListView.layoutChildren(ListView.java:1701) at android.widget.AbsListView.onLayout(AbsListView.java:2235) at android.view.View.layout(View.java:16953) at android.view.ViewGroup.layout(ViewGroup.java:5579) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1189) at android.view.View.layout(View.java:16953) at android.view.ViewGroup.layout(ViewGroup.java:5579) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396) at android.widget.FrameLayout.onLayout(FrameLayout.java:333) at android.view.View.layout(View.java:16953) at android.view.ViewGroup.layout(ViewGroup.java:5579) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2001) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1844) at android.widget.LinearLayout.onLayout(LinearLayout.java:1753) at android.view.View.layout(View.java:16953) at android.view.ViewGroup.layout(ViewGroup.java:5579) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396) at android.widget.FrameLayout.onLayout(FrameLayout.java:333) at android.view.View.layout(View.java:16953) at android.view.ViewGroup.layout(ViewGroup.java:5579) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2001) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1844) at android.widget.LinearLayout.onLayout(LinearLayout.java:1753) at android.view.View.layout(View.java:16953) at android.view.ViewGroup.layout(ViewGroup.java:5579) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396) at android.widget.FrameLayout.onLayout(FrameLayout.java:333) at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2740) at android.view.View.layout(View.java:16953) at android.view.ViewGroup.layout(ViewGroup.java:5579) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2562) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2265) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1323) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6718) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894) at android.view.Choreographer.doCallbacks(Choreographer.java:696) at android.view.Choreographer.doFrame(Choreographer.java:631) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5765) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

DakaListAdapterClass

package com.ree.kms;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

public class DakaListAdapterClass extends BaseAdapter {

    String contains1,contains2;
    Context context;
    List<DakaHinbanClass> valueList;
    private int mTouchItemPosition=-1;

    public DakaListAdapterClass(List<DakaHinbanClass> listValue,Context context){
        this.context = context;
        this.valueList = listValue;
    }

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

    @Override
    public Object getItem(int position) {
        return this.valueList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewItem_Daka viewItem_daka = null;
        if (convertView == null){
            viewItem_daka = new ViewItem_Daka();
            LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.listview_daka,null);
            viewItem_daka.TextViewHinban = convertView.findViewById(R.id.tv_daka_hinban);
            viewItem_daka.TextViewQuery = convertView.findViewById(R.id.tv_daka_query);
            viewItem_daka.TextViewKyakusaki = convertView.findViewById(R.id.tv_daka_kyakusaki);
            viewItem_daka.TextViewDate_End = convertView.findViewById(R.id.tv_daka_date_end);
            viewItem_daka.TextViewRemarks = convertView.findViewById(R.id.tv_daka_remarks);
            viewItem_daka.TextViewCyui = convertView.findViewById(R.id.tv_daka_cyui);
            viewItem_daka.TextViewRakasu = convertView.findViewById(R.id.tv_daka_rakasu);
            viewItem_daka.EditTextQuery_out = convertView.findViewById(R.id.tv_daka_query_out);

            viewItem_daka.EditTextQuery_out.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    mTouchItemPosition = (int) v.getTag();

                    if ((v.getId()==R.id.tv_daka_query_out && canVertivalScroll((EditText) v) )){
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                        if (event.getAction()== MotionEvent.ACTION_UP){
                            v.getParent().requestDisallowInterceptTouchEvent(false);
                        }
                    }
                    return false;
                }
            });

            viewItem_daka.mTextWatcher = new MyTextWatcher();
            viewItem_daka.EditTextQuery_out.addTextChangedListener(viewItem_daka.mTextWatcher);
            viewItem_daka.updatePosition(position);

            convertView.setTag(viewItem_daka);
        }else{
            viewItem_daka = (ViewItem_Daka) convertView.getTag();
            viewItem_daka.updatePosition(position);
        }

        viewItem_daka.TextViewHinban.setText("品番:" valueList.get(position).Hinban);
        viewItem_daka.TextViewQuery.setText("着荷数:" valueList.get(position).Query);
        viewItem_daka.TextViewKyakusaki.setText("取引先:" valueList.get(position).Kyakusaki);
        viewItem_daka.TextViewDate_End.setText("完成予定:" valueList.get(position).Date_end);
        viewItem_daka.TextViewRakasu.setText("落下数:" valueList.get(position).Rakasu);

        viewItem_daka.EditTextQuery_out.setText(valueList.get(position).Query);
        viewItem_daka.TextViewRemarks.setText("備考:" valueList.get(position).Remarks);
        viewItem_daka.TextViewStatus = valueList.get(position).Status;

        if (mTouchItemPosition==position){
            viewItem_daka.EditTextQuery_out.requestFocus();
            viewItem_daka.EditTextQuery_out.setSelection(viewItem_daka.EditTextQuery_out.getText().length());
        }else{
            viewItem_daka.EditTextQuery_out.clearFocus();
        }

        if (TextUtils.isEmpty(valueList.get(position).Cyui)){
            viewItem_daka.TextViewCyui.setText("注意:なし");
        }else {
            viewItem_daka.TextViewCyui.setText("注意:"   valueList.get(position).Cyui);
        }

        contains1= (String) viewItem_daka.TextViewCyui.getText();
        contains2= (String) viewItem_daka.TextViewStatus;
        if(contains1.equals("注意:なし")){
            viewItem_daka.TextViewCyui.setTextColor(Color.parseColor("#808080"));

        }else{
            viewItem_daka.TextViewCyui.setTextColor(Color.parseColor("#FF0000"));
        }

        if (!contains2.contains("生産済")){
            convertView.setBackgroundResource(R.drawable.listview_daka_background);
        }else{
            convertView.setBackgroundResource(R.drawable.listview_chaka_background);
        }
        return convertView;
    }

    private boolean canVertivalScroll(EditText editText){
        int scrollY = editText.getScrollY();
        int scrollRange = editText.getLayout().getHeight();
        int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() - editText.getCompoundPaddingBottom();
        int scrollDifference = scrollRange - scrollExtent;
        if (scrollDifference == 0){
            return false;
        }
        return (scrollY>0) || (scrollY < scrollDifference -1 );
    }
    class ViewItem_Daka {
        TextView TextViewHinban;
        TextView TextViewQuery;
        TextView TextViewDate_End;
        TextView TextViewKyakusaki;
        TextView TextViewCyui;
        TextView TextViewRemarks;
        TextView TextViewFusoku;
        TextView TextViewRakasu;
        EditText EditTextQuery_out;
        TextView TextViewQuery_out;
        String TextViewStatus;
        MyTextWatcher mTextWatcher;
        public void updatePosition(int position){
            mTextWatcher.updatePosition(position);
        }
    }

    class MyTextWatcher implements TextWatcher{
        private int mPosition;
        public void updatePosition(int Position){
            mPosition = Position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            valueList.set(mPosition, (DakaHinbanClass) s);    ←error is here
        }
    }
}

DakaHinbanClass

package com.ree.kms;

public class DakaHinbanClass {
    public String HinbanId;
    public String Hinban;
    public String HangaNumber;
    public String Date_end;
    public String Query;
    public String Weight;
    public String WeightAll;
    public String Cyui;
    public String Query_out;
    public String Remarks;
    public String Kyakusaki;
    public String Status;
    public String Fusoku,Rakasu,FuryoHakuri,FuryoHaiki;
}

@Override public void afterTextChanged(Editable s) { valueList.set(mPosition, s.toString()); ←That's not right }

CodePudding user response:

After the user changes the string inside the EditText, the method afterTextChanged(Editable s) is called, and s represents the new edited String. You want to update the DakaHinbanClass object inside the list, so you can't just conver a String an object (this String is only one class member of this object class, right?)

If you want to update the Query value inside this object, first get the object and then set the class member value to the new String, like this:

  @Override
    public void afterTextChanged(Editable s){

        DakaHinbanClass temp = valueList.get(mPosition);
        temp.Query = s.toString();
    }
  • Related