无法将值从recycleler视图传递到片段

时间:2018-04-01 09:39:20

标签: android android-fragments android-adapter recycler-adapter

我是一个回收器适配器,我在其中添加了一个接口,用于处理适配器到片段之间的数据传输。我想通过positionstring 适配器代码:

    public FiltersAdapter(Context mContext, ClickEvent clickEvent) {
        }

        public interface ClickEvent {
            void clickItemEvent(int position, String str);
        }

        ClickEvent clickEvent;

        public void setClickEvent (ClickEvent clickEvent){
            this.clickEvent = clickEvent;
        }
    @Override
    public void onBindViewHolder(final FiltersAdapter.MyViewHolder holder, final int position) {
        holder.mOrganizer.setText(filtersList.get(position));
        holder.mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (holder.mOrganizerCheck.isChecked()) {
                    holder.mOrganizerCheck.setChecked(false);
                } else {
                    holder.mOrganizerCheck.setChecked(true);
                    int pos = holder.getAdapterPosition();
                    //filtersList.get(pos);
                    clickEvent.clickItemEvent(position, filtersList.get(pos));
                }
            }
        });

        holder.mOrganizerCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    holder.mLayout.setBackgroundResource(R.drawable.selected_list_item);
                } else {
                    holder.mLayout.setBackgroundResource(R.drawable.unselected_list_item);
                }
//                if(Utility.isTablet(mContext))
//                    ((FragmentTabMyLibraryFilterBy) mContext).addChoosedFiles(mChoosebleFilesList.get(position).trim(), isChecked);
//                else
//                    ((FragmentMyLibraryFilterBy) mContext).addChoosedFiles(mChoosebleFilesList.get(position).trim(), isChecked);
            }
        });
    }

这里是片段代码来处理从适配器接收的数据。

    public class EventFilterFragment extends Fragment {

    private Context mContext;

    // Member variables
    public static final String TAG = EventFilterFragment.class.getSimpleName();
    private View mFilterView = null;
    private List<String> mOrganizer = new ArrayList<>();
    private RecyclerView mrvFilterBySender;
    private FiltersAdapter mFiltersAdapter;
    private ArrayList<String> mListOrganizer;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (null != getArguments()) {
            mListOrganizer = getArguments().getStringArrayList("OrgList");
        }
        mFilterView = inflater.inflate(R.layout.fragment_event_filter, container, false);
        initView();

        mContext = getContext();

        return mFilterView;
    }

    private void initView() {
        mrvFilterBySender = (RecyclerView) mFilterView.findViewById(R.id.rvFilterBySender);
        mFiltersAdapter = new FiltersAdapter(getActivity(), mListOrganizer);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mrvFilterBySender.setLayoutManager(mLayoutManager);

        mrvFilterBySender.setAdapter(mFiltersAdapter);
        mFiltersAdapter.notifyDataSetChanged();

        // Ajay's code
        mrvFilterBySender.setAdapter(mFiltersAdapter);
        //mFiltersAdapter.setClickEvent((FiltersAdapter.ClickEvent) this);
        FiltersAdapter filtersAdapter = new FiltersAdapter(mContext, new FiltersAdapter.ClickEvent() {
            @Override
            public void clickItemEvent(int position, String str) {
                Toast.makeText(mContext, "Position is" +position, Toast.LENGTH_SHORT).show();
            }
        });
        /*mrvFilterBySender.setRecyclerListener(new RecyclerView.RecyclerListener() {
            @Override
            public void onViewRecycled(RecyclerView.ViewHolder holder) {
                holder.getAdapterPosition();
            }
        });*/
    }

}

当我尝试运行该应用时,出现此错误:

Unable to start activity ComponentInfo{com.emc.orgly/com.emc.orgly.dialer.activity.EventFilterActivity}: java.lang.ClassCastException: com.emc.orgly.dialer.fragment.EventFilterFragment cannot be cast to com.emc.orgly.dialer.adapter.FiltersAdapter$ClickEvent
                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emc.orgly/com.emc.orgly.dialer.activity.EventFilterActivity}: java.lang.ClassCastException: com.emc.orgly.dialer.fragment.EventFilterFragment cannot be cast to com.emc.orgly.dialer.adapter.FiltersAdapter$ClickEvent
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                         at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                         at android.os.Looper.loop(Looper.java:154)
                                                         at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                      Caused by: java.lang.ClassCastException: com.emc.orgly.dialer.fragment.EventFilterFragment cannot be cast to com.emc.orgly.dialer.adapter.FiltersAdapter$ClickEvent
                                                         at com.emc.orgly.dialer.fragment.EventFilterFragment.initView(EventFilterFragment.java:73)
                                                         at com.emc.orgly.dialer.fragment.EventFilterFragment.onCreateView(EventFilterFragment.java:55)
                                                         at android.support.v4.app.Fragment.performCreateView(Fragment.java:2192)
                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299)
                                                         at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528)
                                                         at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595)
                                                         at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758)
                                                         at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363)
                                                         at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149)
                                                         at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
                                                         at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
                                                         at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                         at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
                                                         at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
                                                         at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)
                                                         at android.app.Activity.performStart(Activity.java:6696)
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2628)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                         at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                         at android.os.Looper.loop(Looper.java:154) 
                                                         at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

如何正确投射?

2 个答案:

答案 0 :(得分:0)

卸下:

//Adapter
    public void setClickEvent (ClickEvent clickEvent){
        this.clickEvent = clickEvent;
    }
//Fragmnet
    mFiltersAdapter.setClickEvent((FiltersAdapter.ClickEvent) this);

根据你的代码应该做的伎俩

答案 1 :(得分:0)

您尚未在此处实现导致问题的片段接口。 您需要将片段的签名更改为。

 public class EventFilterFragment extends Fragment implements ClickEvent{

//override method

}

也不要忘记打电话给片段。

 mFiltersAdapter.setClickEvent( this);

您的代码应如下所示

public class EventFilterFragment extends Fragment implements ClickEvent{

    private Context mContext;

    // Member variables
    public static final String TAG = EventFilterFragment.class.getSimpleName();
    private View mFilterView = null;
    private List<String> mOrganizer = new ArrayList<>();
    private RecyclerView mrvFilterBySender;
    private FiltersAdapter mFiltersAdapter;
    private ArrayList<String> mListOrganizer;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (null != getArguments()) {
            mListOrganizer = getArguments().getStringArrayList("OrgList");
        }
        mFilterView = inflater.inflate(R.layout.fragment_event_filter, container, false);
        initView();

        mContext = getContext();

        return mFilterView;
    }

    private void initView() {
        mrvFilterBySender = (RecyclerView) mFilterView.findViewById(R.id.rvFilterBySender);
        mFiltersAdapter = new FiltersAdapter(getActivity(), this);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mrvFilterBySender.setLayoutManager(mLayoutManager);

        mrvFilterBySender.setAdapter(mFiltersAdapter);
        mFiltersAdapter.notifyDataSetChanged();

        // Ajay's code
        mrvFilterBySender.setAdapter(mFiltersAdapter);

        /*mrvFilterBySender.setRecyclerListener(new RecyclerView.RecyclerListener() {
            @Override
            public void onViewRecycled(RecyclerView.ViewHolder holder) {
                holder.getAdapterPosition();
            }
        });*/
    }
 @Override
 public void clickItemEvent(int position, String str){
      //perform actions
 }
}

<强>通知 我想我已经得到了你的问题

mFiltersAdapter = new FiltersAdapter(getActivity(), mListOrganizer);

此行不正确请参阅其构造函数接受上下文和ClickEvent对象的FiltersAdapter,但我们正在传递mListOrganizer。正确的行是

mFiltersAdapter = new FiltersAdapter(getActivity(), this);