2017-05-04 33 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小時後,我的頭和手指開始做垃圾。 –