从 recyclerView (java) 的选定行中获取值

Get the value from selected rows of recyclerView (java)

好吧,我有一个 RecyclerView,我正在从中选择多个值,并想将所选行的值附加到一个数组中,但我不知道它的 returning null 做错了什么:我的adpater

import com.bignerdranch.android.multiselector.MultiSelector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by MACBOOK on 07/03/16.
 */


public class ChannelsAdapter extends RecyclerView.Adapter<ChannelsAdapter.ChannelsViewHolder> implements Filterable  {

    private LayoutInflater inflater;
    private Context context;

    List<ChannelsInformation> data = Collections.emptyList();

    private final List<ChannelsInformation> filteredChannelsList;

    private final MultiSelector mMultiSelector = new MultiSelector();

      ArrayList <String> selected;



    public ChannelsAdapter(Context context,  List<ChannelsInformation> data){

        inflater =  LayoutInflater.from(context);


        this.context = context;


        this.data = data;


        filteredChannelsList = data;
    }

    public void remove(int position){
        data.remove(position);
        notifyItemRemoved(position);
    }


    @Override
    public ChannelsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View rowView = inflater.inflate(R.layout.custom_channel_row, parent, false);

        ChannelsViewHolder holder = new ChannelsViewHolder(rowView);

        return holder;
    }

    @Override
    public void onBindViewHolder(final ChannelsViewHolder holder, final int position) {

        ChannelsInformation current = data.get(position);

        holder.CHANNELNAME.setText(current.channelName);
        holder.ID.setText(current.id);
        holder.mSolvedCheckBox.setChecked(current.isSelected);






    }

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

    @Override
    public Filter getFilter() {
        return new UserFilter(this ,filteredChannelsList);
    }

private static class UserFilter extends Filter {

    private final ChannelsAdapter adapter;

    private final List<ChannelsInformation> originalList;

    private final List<ChannelsInformation> filteredList;

    private UserFilter(ChannelsAdapter adapter, List<ChannelsInformation> originalList) {
        super();
        this.adapter = adapter;
        this.originalList = new ArrayList<>(originalList);
        this.filteredList = new ArrayList<>();
    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        filteredList.clear();
        final FilterResults results = new FilterResults();

        if (constraint == null || constraint.length() == 0) {
            filteredList.addAll(originalList);



        } else {
            final String filterPattern = constraint.toString().toLowerCase().trim();


            for (final ChannelsInformation channel : originalList) {






            }
        }
        results.values = filteredList;
        results.count = filteredList.size();
        return results;
    }


    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        adapter.filteredChannelsList.clear();
        if  ((ArrayList<ChannelsInformation>) results.values != null ) {

            adapter.filteredChannelsList.addAll((ArrayList<ChannelsInformation>) results.values);
        }
        adapter.notifyDataSetChanged();

    } }






class ChannelsViewHolder extends SwappingHolder implements View.OnClickListener {

    TextView CHANNELNAME,ID;
    CheckBox mSolvedCheckBox;




    public ChannelsViewHolder(View itemView) {


        super(itemView , mMultiSelector);

        mMultiSelector.setSelectable(true);

        mSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.selectedChannelCheckBox);

        itemView.setOnClickListener(this);

        CHANNELNAME = (TextView) itemView.findViewById(R.id.ChannelNameTxtView);
        ID = (TextView) itemView.findViewById(R.id.ChannelRowIdTextView);





    }

    @Override
    public void onClick(View v) {

        if (!mMultiSelector.tapSelection(this)) {
            Toast.makeText(context,CHANNELNAME.getText(),Toast.LENGTH_LONG).show();

            mSolvedCheckBox.toggle();


          selected.add(ID.toString());


            Log.d(""+selected , " : value of selected ");


        }




    }
}}

另一个 class :

public class ChannelsInformation {
    String id,channelName;
    boolean isSelected;
}

在 myFragmentCLass 上:

Fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        String data = "";
        List<ChannelsInformation> stList = ((ChannelsAdapter) channelsAdapter).data;

        for (int i = 0; i < stList.size(); i++) {
            ChannelsInformation singleStudent = stList.get(i);
            if (singleStudent.isSelected) {

                data = data + "\n" + singleStudent.channelName;

            }

        }

        Toast.makeText(getActivity(),
            "Selected Students: \n" + data, Toast.LENGTH_LONG)
            .show();




    }
});

好吧,我在过去 4 个小时里尝试过这个,你可以看到我什至尝试 multiselector 它的一个库来创建一个 selectMode 与我们在 listView 上的一样,现在我放了一个复选框在我的视图中并尝试获取复选框的复选框的值,如果它被选中然后显示数据但是我在 return 中得到 null ,如果有人在这里知道错了那么请指出它我会这样对我有帮助并受到我的赞赏,谢谢

您可以尝试创建List<YourItem> selectedItems 甚至添加选定的项目。不要忘记从此列表中删除未选择的项目。在此适配器

中创建 getter
public List<YoutItem> getSelectedItens(){

 return selectedItems;
}

it's 地图示例

好吧,我没有看到你对

的初始化
ArrayList<String> selected;

这样做

public ChannelsAdapter(Context context, List<ChannelsInformation> data) {

    inflater = LayoutInflater.from(context);

    this.context = context;

    this.data = data;

    filteredChannelsList = data;

    // like this
    selected = new ArrayList<>();
}