【问题标题】:Best solution for Checkbox in LinearLayoutLinearLayout中复选框的最佳解决方案
【发布时间】:2017-08-30 12:50:17
【问题描述】:

在我的 LinearLayout 中,有可变数量的 CheckBox。在我一个月前的一个问题中,有人说最好动态添加复选框而不是让它们不可见。

Integer[] count = new Integer[]{1,2,3,4,5,6,7,8,9,10};
size = mFrageList.get(position).getAuswahlList().size();
for (int i = 0; i < size; i++) {

    cBox = new CheckBox(this);
    cBox.setText(mFrageList.get(position).getAuswahlList().get(i));
    cBox.setId(count[i]);
    cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

           if(isChecked){
                antwortencode[position] += "" + buttonView.getId();
                frageBeantworten.setText("Antwort :"+antwortencode[position]+" abgeben");

            } else {
                 String id = Integer.toString(buttonView.getId());
                 antwortencode[position] = antwortencode[position].replaceAll(id,"");
                 if(!antwortencode[position].isEmpty() || antwortencode[position]!= "") {
                     frageBeantworten.setText("Antwort :" + antwortencode[position] + " abgeben");
                  } else {
                      frageBeantworten.setText("Keine Checkbox(en) gewählt");
                  }
           }
       }
});

antworten.addView(cBox);

目前,我可以使用选中的复选框保存一个字符串,如果我取消选中一个复选框,它会从字符串中删除它的值。 如果我更新活动,则保存字符串,并且复选框从 mFrageList.get(position)getAuswahlList(); 获取一个新列表;并用它们的值在“antwortencode”列表中填充一个新字符串。

如果我回到最后一个位置,我有生成的字符串,但不再选中复选框。但是他们有旧位置的字符串。这意味着除了复选框的状态之外的所有内容都已保存。我无法设置 cBox.setChecked(isChecked) 或 buttonView.setChecked(isChecked) 或 buttonView.setChecked(buttonView.isChecked()) 或语法几乎相同的东西。

我不知道除了在 xml 文件中声明 10 个复选框以与它们一一对话并在 auswahlList.get(position).isEmpty() 时设置 VISIBLE.false 之外我还能做什么。

重要提示:我的 XML 是一个可滚动的活动,因为内容的大小超出了屏幕。这就是为什么我没有也不能使用 Listview 的原因。所以我需要一个使用 LinearLayout 的解决方案

【问题讨论】:

    标签: java android xml checkbox


    【解决方案1】:

    事实上,您实际上应该使用ListView。只要您多次重复使用一个布局 - 就这样做。

    有两种选择:

    另一件事是如何维护 Checkboxes 的状态 - 使用模型类。使用 ListView 非常简单,因为它强制您使用 Adapter,它提供了遍历所有位置的方法。

    适配器示例:

    public class CheckableItemAdapter extends BaseAdapter {
    
    private List<Pair<Integer, Boolean>> items = new ArrayList<>();
    
    public void setItems(List<Pair<Integer, Boolean>> items) {
        this.items = items;
    }
    
    @Override
    public int getCount() {
        return items.size();
    }
    
    @Override
    public Object getItem(int position) {
        return items.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_checkable, parent, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        Pair<Integer, Boolean> item = items.get(position);
        holder.itemCheck.setChecked(item.second);
        return view;
    }
    
    static class ViewHolder {
    
        CheckBox itemCheck;
    
        public ViewHolder(View itemView) {
            itemCheck = (CheckBox) itemView.findViewById(R.id.check);
        }
    }
    }
    

    【讨论】:

    • 好的,但我不能使用 Listview,因为它会干扰我的 ScrollableActivity 并反转。而且不可能(?)在一项活动中同时使用它们。据我在stackoverflow中阅读有关此问题的主题。但我设法独自解决了我的问题。经过 8 小时的编程后,我的头和手指开始做垃圾了。
    【解决方案2】:

    我已经设法独自解决了我的问题,现在我想分享它,即使它不是最好的编程示例。

      Integer[] count = new Integer[]{1,2,3,4,5,6,7,8,9,10}; //maximum of 10 Checkboxes
            size = mFrageList.get(position).getAuswahlList().size();
            for (int i = 0; i < size; i++) {
                cBox = new CheckBox(this);
                cBox.setText(mFrageList.get(position).getAuswahlList().get(i));
                cBox.setId(count[i]);
    
                try{ //this is where the magic happens
                    if(antwortencode[position] != ""){ //cause i won´t want null in my db i´ve set "" as standard string in my activity for the List<String>
                        String code = antwortencode[position];
                        char[] c = code.toCharArray();
                        for(int j=0;j<=c.length;j++){
                            int x = c[j] -'0'; // 'char 1' - 'char 0' = Integer 1 , lol
                            if(cBox.getId()== x){ //compare them
                                cBox.toggle(); //if it fits, toggle
                            }
                        }
                    }
                } catch (Exception e){
                        e.printStackTrace();
                } //and here it ends
    
                cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    
                        if(isChecked){
                            antwortencode[position] += "" + buttonView.getId();
                            frageBeantworten.setText("Antwort :"+antwortencode[position]+" abgeben");
    
                        } else {
                            String id = Integer.toString(buttonView.getId());
                            antwortencode[position] = antwortencode[position].replaceAll(id,"");
                            if(!antwortencode[position].isEmpty() || antwortencode[position]!= "") {
                                frageBeantworten.setText("Antwort :" + antwortencode[position] + " abgeben");
                            } else {
                                frageBeantworten.setText("Keine Checkbox(en) gewählt");
                            }
                        }
                    }
                });
    
                antworten.addView(cBox);
    

    请回答并纠正我的问题。 Nostramärus

    【讨论】:

      猜你喜欢
      • 2020-04-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多