【问题标题】:Get the value from selected rows of recyclerView (java)从recyclerView(java)的选定行中获取值
【发布时间】:2016-06-21 12:32:32
【问题描述】:

好吧,我有一个 RecyclerView,我正在从中选择多个值,并希望将选定行的值附加到一个数组中,但我不知道它在返回 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 ");


        }




    }
}}

另一个类:

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 上一样,现在我在我的视图中放了一个复选框并尝试获取checkBox的checkBox的值,如果它被选中然后显示数据但得到null作为回报,如果有人在这里知道错了,请指出它对我很有帮助并受到我的赞赏,谢谢

【问题讨论】:

    标签: java android android-recyclerview multipleselection


    【解决方案1】:

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

    public List<YoutItem> getSelectedItens(){
    
     return selectedItems;
    }
    

    it's 地图示例

    【讨论】:

    • @llya 非常感谢您的回复,基本上是一名 iOS 开发人员,对 android 并没有太多了解,所以仍然在搞清楚所有这些方面,所以请您深入解释一下吗?请因为我没有从你的问题中得到确切的观点
    • 嘿@llya 感谢您的链接,所以在看到您的示例后,我需要的是onBindViewHolder,对吗?
    • 嘿@llya 你能看看我在这里得到一个空异常`public void onClick(View view) { if (!selected.contains(current.channelName)) { holder.mSolvedCheckBox.setChecked(真的); selected.add(current.channelName); map.put(current.channelName, true); } else { holder.mSolvedCheckBox.setChecked(false); map.remove(当前); } }` 你能告诉我这里有什么问题吗?
    • 嘿,谢谢你提供的例子,它现在工作正常
    【解决方案2】:

    嗯,我没有看到你的初始化

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

    【讨论】:

    • 嘿@mithun 非常感谢你的回答,我真的很感激这个,但是在代码中我还没有将它们添加到任何数组中,我只是遍历所有数据并检查视图是否被选中` for (int i = 0; i
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多