簡體   English   中英

LinearLayout中Checkbox的最佳解決方案

[英]Best solution for Checkbox in LinearLayout

在我的LinearLayout中,有可變數量的CheckBoxes。 在一個月前的一個問題中,有人說,最好動態添加復選框,而不是使其不可見。

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個復選框以與它們逐一對話並設置aviswahlList.get(position).isEmpty()設置VISIBLE.false外,我不知道該怎么辦。

重要說明:我的XML是可滾動活動,因為內容的大小超出了屏幕的范圍。 那就是為什么我沒有也不能使用Listview的原因。 所以我需要一個使用LinearLayout的解決方案

事實是,您實際上應該使用ListView 只要您多次重復使用布局-便要這樣做。

有2個選項:

另一件事是如何維護Checkboxes的狀態-使用模型類。 使用ListView非常容易,因為它迫使您使用Adapter ,該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);
    }
}
}

我設法獨自解決了我的問題,現在我想分享它,即使它不是編程的最佳示例。

  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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM