1

我已經使用Adapter類中的Adapter類的Customized Listview ROW中的兩個複選框爲此,我需要Select Only一個複選框Checkbox1行/列相同複選框2. 例如,我有一個列表視圖中的5行共10個複選框裏面的列表視圖輸出10只有一個選擇一次所有其他應該取消選擇使用Listview在同一行和列中僅選擇一個複選框/單選按鈕連同兩個複選框/單選按鈕Android

任何想法關於我試過很多但只選擇Checkbox1列 請幫忙

public class ListViewAdapter extends ArrayAdapter<Friend> { 

    private List<Friend> myFriends; 
    private Activity activity; 
    private int selectedPosition = -1; 
    String flag=""; 

    String[]ids=new String[100]; 
    String[]ids1=new String[100]; 

    public ListViewAdapter(Activity context, int resource, List<Friend> objects) { 
     super(context, resource, objects); 
     this.activity = context; 
     this.myFriends = objects; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 
     LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

     // If holder not exist then locate all view from UI file. 
     if (convertView == null) { 
      // inflate UI from XML file 
      convertView = inflater.inflate(R.layout.item_listview, parent, false); 
      // get all UI view 
      holder = new ViewHolder(convertView); 
      // set tag for holder 
      convertView.setTag(holder); 
     } else { 
      // if holder created, get tag from view 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.checkBox.setTag(position); // This line is important. 
     holder.checkBox1.setTag(position + 100); 
     holder.friendName.setText(getItem(position).getName()); 
     holder.rdgrp.setTag(position); 

     ids[position]=holder.checkBox.getTag().toString(); 
     ids1[position]=holder.rdgrp.getTag().toString(); 
     // System.out.println("praveen"+ids[position]); 
     if (position == selectedPosition) { 
      holder.rdgrp.setActivated(true); 
     } else holder.rdgrp.setActivated(false); 

     if(holder.checkBox.getTag().equles(position)){ 
      holder.checkBox.setOnClickListener(onStateChangedListener(holder.checkBox, position)); 
     }else{ 
      holder.checkBox1.setOnClickListener(onStateChangedListener(holder.checkBox1, position)); 
     } 

     return convertView; 
    } 

    public View.OnClickListener onStateChangedListener(final RadioGroup gry, final int position){ 
     return new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (gry.isClickable()) { 
        selectedPosition = position; 
       } else { 
        selectedPosition = -1; 
       } 
       notifyDataSetChanged(); 
      } 
     }; 
    } 

    private static class ViewHolder { 
     private TextView friendName; 
     private CheckBox checkBox,checkBox1; 

     public ViewHolder(View v) { 
      checkBox = (CheckBox) v.findViewById(R.id.check); 
      checkBox1 = (CheckBox) v.findViewById(R.id.check1); 
      friendName = (TextView) v.findViewById(R.id.name); 
     } 
    } 
} 
+0

你試過了什麼? –

+0

你可以發佈你的代碼嗎? –

+0

是的,我發佈我的代碼在剛纔的問題 – kumar

回答

0

你可以使用組有兩個複選框,然後列表視圖單行的複選框適用以下的代碼

"listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE)" 
+0

這也是我試過 – kumar

+0

我有一個疑問如何調用這個方法複選框1和複選框2.其工作只有複選框1 公衆View.OnClickListener onStateChangedListener(最終複選框RY,最終詮釋位置){ 返回新View.OnClickListener(){ @覆蓋 公共無效的onClick(視圖v){ 如果(gry.isClickable()){ selectedPosition =位置; } else { selectedPosition = -1; } notifyDataSetChanged(); } }; – kumar

0

嘗試用下面的代碼行:

public class CustomAdapter extends BaseAdapter{ 
    int selectedPosition = -1; // row position 
    int selectedCheckbox = -1; // 1 for 1st, 2 for 2nd on each row 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if(position == selectedPosition){ 
      if(selectedCheckbox == 1){ 
       checkbox1.setChecked(true); 
       checkbox2.setChecked(false); 
      } else if(selectedCheckbox == 2){ 
       chkbox1.setChecked(false); 
       chkbox2.setChecked(true); 
      } 
     } else{ 
      checkbox1.setChecked(false); 
      checkbox2.setChecked(false); 
     } 

     checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked){ 
        selectedPosition = position; 
        selectedCheckbox = 1; 
       } else{ 
        selectedPosition = -1; 
        selectedCheckbox = -1; 
       } 
       notifyDataSetChanged(); 
      } 
     }); 

     checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if(isChecked){ 
        selectedPosition = position; 
        selectedCheckbox = 2; 
       } else{ 
        selectedPosition = -1; 
        selectedCheckbox = -1; 
       } 
       notifyDataSetChanged(); 
      } 
     }); 

     return convertView; 
    } 
} 

希望,它會幫你!