2017-05-04 63 views
0

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

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()获得一个新List。并使用它们的值在“antwortencode”列表中填入一个新字符串。

如果我回到最后的位置,我已经产生了字符串,但复选框不再被检查。但他们从旧的位置有弦。这意味着除了复选框的状态以外,所有内容都会保存。我不能设置一个cBox.setChecked(isChecked)或buttonView.setChecked(isChecked)或buttonView.setChecked(buttonView.isChecked())或在语法上几乎相同的东西。

我不知道我可以做什么,除了在xml文件中声明10个复选框以逐个跟他们交谈,并且如果auswahlList.get(position).isEmpty()设置为VISIBLE.false。

重要提示:我的XML是一个可滚动活动,因为内容的大小过度扩展屏幕。这就是为什么我没有和不能使用ListView。所以我需要一个使用LinearLayout的解决方案

回答

0

从来就独自管理解决我的问题,现在我想分享它,即使它isn't编程最好的例子。

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

Ty为答案和我的问题的更正。 Nostramärus

0

事实是,你应该实际使用一个ListView。只要您多次重复使用版式 - 请执行此操作。

有2个选项:

另一件事是如何维持复选框的状态 - 使用模型类。使用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); 
    } 
} 
} 
+0

好的,但我不能使用ListView导致它扰乱了我的ScrollableActivity并被颠倒。在一项活动中使用它们是不可能的(?)。至于我读过关于这个问题的计算器的主题。但我设法单独解决我的问题。编程8小时后,我的头和手指开始做垃圾。 –