简体   繁体   English

如何从所有 TextViews RecyclerView 中获取文本

[英]How to get text from all TextViews RecyclerView

First of all sorry for the language.首先对语言感到抱歉。 I have a RecyclerView with items.我有一个带有项目的RecyclerView I have a checkbox in each item.我在每个项目中都有一个checkbox I have onCheckedChangeListener inside RecyclerAdapter .我在RecyclerAdapter中有onCheckedChangeListener

When I check 4 checkboxes I need to disable the remaining.当我选中 4 个checkboxes时,我需要禁用剩余的复选框。 So how can I get an access for them?那么我怎样才能获得他们的访问权限呢? I can do it when on create Recycler by checking how many items are selected.我可以在创建 Recycler 时通过检查选择了多少项目来做到这一点。 But can't find how to get access for every checkbox in onCheckedChange method.但是找不到如何访问onCheckedChange方法中的每个checkbox Screenshot example截图示例

public class EditAtributesAdapter extends RecyclerView.Adapter<EditAtributesAdapter.EditAtributesViewHolder> {

    private ArrayList<AtributeEditItem> mEditAtributeList;
    DBHelper dbHelper;


    public static class EditAtributesViewHolder extends RecyclerView.ViewHolder {

        public ImageView mImageView;
        public CheckBox mCheckBox;

        public EditAtributesViewHolder(View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.edit_atribute_icon);
            mCheckBox = itemView.findViewById(R.id.enabledAtribute);
        }
    }


    public EditAtributesAdapter(ArrayList<AtributeEditItem> editAtributeList){
        mEditAtributeList = editAtributeList;
    }

    @Override
    public EditAtributesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.atribute_edit_item, parent, false);
        final Context forClickCTX = parent.getContext();
        final EditAtributesViewHolder eavh = new EditAtributesViewHolder(v);


        eavh.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int pos = eavh.getAdapterPosition();
                // System.out.println("Hello, you clicked: " + mEditAtributeList.get(pos).getName());
                dbHelper = new DBHelper(forClickCTX);
                try {
                    dbHelper.createDataBase();}
                catch (IOException ioe) {
                    throw new Error("Не удалось создать базу данных");}
                try {
                    dbHelper.openDataBase();}
                catch (SQLException sqle) {
                    throw sqle;}


                int countSelected = 0;
                if (countSelected == 0){
                    Cursor cursor = null;
                    try {
                        SQLiteDatabase db = dbHelper.getReadableDatabase();
                        cursor = db.query(DBHelper.TABLE_TASKS, null, "id = " + mEditAtributeList.get(pos).getTask_id(), null, null, null, null);
                        while (cursor != null && cursor.moveToNext()) {
                            String ids = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.TASK_ATRIBUTE_ID));
                            String[] parts = ids.split(",");
                            int[] ints = new int[parts.length];
                            for (int i = 0; i < parts.length; i++) {
                                ints[i] = Integer.parseInt(parts[i]);
                            }
                            countSelected = ints.length;
                        }
                    } finally {
                        if (cursor != null)
                            cursor.close();
                    }
                    if(countSelected>=5){
                        if(!eavh.mCheckBox.isChecked())
                            eavh.mCheckBox.setEnabled(false);
                        else
                            eavh.mCheckBox.setEnabled(true);
                    }
                    else
                        eavh.mCheckBox.setEnabled(true);

            }
     //here is saving new status to db ( a lot of code)
    });
        return eavh;
    }
    @Override
    public void onBindViewHolder(EditAtributesViewHolder holder, int position) {
        AtributeEditItem currentItem = mEditAtributeList.get(position);

        holder.mImageView.setImageResource(currentItem.getImage());
        holder.mCheckBox.setText(currentItem.getName());
        holder.mCheckBox.setChecked(currentItem.isSelected());

        int countSelected = 0;
        for (int i = 0 ; i < mEditAtributeList.size() ; i++){
            if(mEditAtributeList.get(i).isSelected())
                countSelected++;
        }
        if(countSelected>=5){
            if(!holder.mCheckBox.isChecked())
                holder.mCheckBox.setEnabled(false);
            else
                holder.mCheckBox.setEnabled(true);
        }
        else
            holder.mCheckBox.setEnabled(true);
    }

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

Your adapter and viewholder are actually doing too much, but for a small list you can probably get away with it.您的适配器和查看器实际上做的太多了,但是对于一个小列表,您可能可以侥幸逃脱。 Calling the sqldatabse like this onclick can be a bit awkward, i am not sure, but i think you are blocking the main thread with this which means you may notice that onclick slightly freezes your app.像这样调用 sqldatabse onclick 可能有点尴尬,我不确定,但我认为你用这个阻塞了主线程,这意味着你可能会注意到 onclick 会稍微冻结你的应用程序。

But the simplest way disable the items in your recyclerview is to update the items in your但是禁用回收站视图中的项目的最简单方法是更新您的项目中的项目

private ArrayList<AtributeEditItem> mEditAtributeList

and call并打电话

notifyDataSetChanged()

on the adapter, as you can then derive the state based on the data在适配器上,因为您可以根据数据得出 state

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM