【问题标题】:Android hide and show checkboxes in custom list view on button clickAndroid在按钮单击时在自定义列表视图中隐藏和显示复选框
【发布时间】:2014-01-06 08:02:20
【问题描述】:

我使用下面给出的适配器。我在图中解释。数字仅供参考。 我在单行中使用 textview 和复选框。 textviews 应该出现并且复选框最初隐藏。在按钮按下复选框后应该出现并且 textview 保持不变。请帮助我。提前谢谢..

 public class Adapter extends ArrayAdapter<SectionsModel>{

    private ArrayList<SectionsModel> list;
    public Adapter(Context context, int resource, ArrayList<SectionsModel> objects) {
        super(context, resource, objects);
        this.list = new ArrayList<SectionsModel>();
        this.list.addAll(objects);
    }

    ViewHolder v = new ViewHolder();
    public void setcheckbox() {

        Log.d("viewholser" + v, "checkbox" + v.selected);

        v.selected.setVisibility(View.VISIBLE);

    }

    public class ViewHolder {
        public TextView setting;
        public CheckBox selected;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {


        ViewHolder holder = null;
        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            convertView = vi.inflate(R.layout.menu_frame_child, null);
            holder = new ViewHolder();

            holder.setting = (TextView) convertView.findViewById(R.id.child_setting_header);
            holder.selected = (CheckBox) convertView.findViewById(R.id.settings_check);

        //  holder.selected.setVisibility(View.INVISIBLE);

            convertView.setTag(holder);

            //holder.selected.setVisibility(View.INVISIBLE);

            holder.selected.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    SectionsModel _state = (SectionsModel) cb.getTag();
                    _state.setSelected(cb.isChecked());
                }
            });
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        SectionsModel section = list.get(position);

        holder.setting.setText(section.getSection());
        holder.selected.setChecked(section.isSelected());

        holder.selected.setTag(section);

        return convertView;
    }

    @Override
    public int getCount() 
    {
        return super.getCount();
    }

    @Override
    public int getPosition(SectionsModel item) {

        return super.getPosition(item);
    }


}

更新: 在 getview 我只是添加以下代码 AppConstants.ischeckboxvisible 是变量,其中 Appconstants 是类,ischeckboxvisible 是该类的公共静态变量。最初这个变量是假的

            if (!AppConstants.ischeckboxvisible)
            {
       holder.selected.setVisibility(View.INVISIBLE);
    }
        if (AppConstants.ischeckboxvisible)
            {
           holder.selected.setVisibility(View.VISIBLE);
        }

【问题讨论】:

  • 你为这个功能做了什么?
  • 最初我在适配器中设置了复选框可见性,之后我在适配器中创建了可见该复选框的方法。从按钮单击我在适配器中调用该方法并在那之后通知数据集更改但它没有工作。
  • @MayurRaval 如何让每个不同的复选框都可见?如我所见,更新后的代码只能使一个复选框可见?
  • @Stallman,在布尔值设置为 true 之后,您是否在适配器上应用了 notifydatasetchanged()?更新的代码对我有用。

标签: android listview button checkbox android-arrayadapter


【解决方案1】:

以防万一有人仍然需要它。

我解决了下一个问题:

在 getView - 我设置的 ArrayAdapter 方法中:

CheckBox checkBox = (CheckBox)listItem.findViewById(R.id.checkBox);
    checkBox.setVisibility(View.GONE);

然后在setOnItemLongClickListener里面:

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {

            for(int i = 0; i != arrayList.size(); i++) {

        mCheckBox = (CheckBox) playlist.getChildAt(i).findViewById(R.id.checkBox);
        mCheckBox.setVisibility(View.VISIBLE);
    }

            return true;
        }
    });

长按任何项目后,所有复选框都会出现。

【讨论】:

    【解决方案2】:

    将另一个属性添加到您的 SectionModel 作为 inEditMode,并在用户单击编辑时将数组中的所有项目设置为 true,并在您的适配器上调用 notifyDataSetChanged。

    在适配器中检查每个项目上的标志并使其可见或不可见。

    【讨论】:

    • 谢谢我终于明白了..谢谢你给我的想法。我没有改变部分模型..但我采用外部类静态变量,我设置了该变量的真假可见性
    • @yahya 如何让每个不同的复选框都可见?\
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    相关资源
    最近更新 更多