从recyclerView(java)

时间:2016-03-07 19:42:07

标签: java android android-recyclerview multipleselection

我有一个RecyclerView,我从中选择多个值,并希望将所选行的值附加到数组中,但我不知道返回null的错误是什么:my 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 ");


        }




    }
}}

另一堂课:

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

on 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上一样,现在我把一个复选框放在我的查看并尝试获取checkBox的checkBox的值,如果已检查然后显示数据,但我得到null作为回报,如果有人在这里知道错误那么请指出它,我就这样对我很有帮助并感谢我,谢谢

2 个答案:

答案 0 :(得分:1)

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

public List<YoutItem> getSelectedItens(){

 return selectedItems;
}

it's地图

示例

答案 1 :(得分:1)

好吧,我没有看到你的初始化

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<>();
}