2016-06-10 1 views
1

Я новичок в android и работаю над спискомView, который имеет флажки в элементах, Когда я проверяю какой-либо элемент, другой элемент также автоматизирован, может ли кто-нибудь помочь мне, как его решить, мой адаптер как показано ниже,Проблема с выбором флажка в спискеView в android

public class ServiceAdapter extends BaseAdapter { 
    public ArrayList<HashMap<String, String>> contArray; 
    ArrayList<Boolean> checked; 


    private Context mContext; 
    String resnID, reson; 
    Intent i; 

    public ServiceAdapter(Context paramContext, ArrayList<HashMap<String, String>> contList) { 
     this.mContext = paramContext; 
     this.contArray = contList; 
     checked = new ArrayList<>(); 

    } 

    public int getCount() { 
     return this.contArray.size(); 
    } 

    public Object getItem(int paramInt) { 
     return Integer.valueOf(paramInt); 
    } 

    public long getItemId(int paramInt) { 
     return paramInt; 
    } 

    public View getView(final int paramInt, View paramView, 
      ViewGroup paramViewGroup) { 
     LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext 
       .getSystemService("layout_inflater"); 
     Viewholder localViewholder = null; 
     Const.selectedIDs.clear(); 
     if (paramView == null) { 
      paramView = localLayoutInflater.inflate(
        R.layout.raw_single_contact, paramViewGroup, false); 
      localViewholder = new Viewholder(); 

      localViewholder.tv_name = ((TextView) paramView 
        .findViewById(R.id.tv_name)); 
      localViewholder.chk_id = ((CheckBox) paramView 
        .findViewById(R.id.chk_id)); 

      paramView.setTag(localViewholder); 
      paramView.setTag(R.id.chk_id, localViewholder.chk_id); 

     } else { 

      localViewholder = (Viewholder) paramView.getTag(); 
     } 
     localViewholder.tv_name.setText(contArray.get(paramInt).get("serviceText")); 

     localViewholder.chk_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       try { 


        if (isChecked) { 
         Const.selectedIDs.add(contArray.get(paramInt)); 
         Const.serviceArrayList.get(paramInt).put("flag", "1"); 
        } else { 
         Const.selectedIDs.remove(contArray.get(paramInt)); 
         Const.serviceArrayList.get(paramInt).put("flag", "0"); 
        } 
       }catch (Exception e){ 
        e.printStackTrace(); 
       } 

      } 
     }); 


    return paramView; 

    } 

    static class Viewholder { 


    TextView tv_name; 
    CheckBox chk_id; 


    } 



} 
+0

См этот ответ: http://stackoverflow.com/questions/36807029/using-switch-in-recyclerview-srcoll/36807323#36807323 –

ответ

0

Проверьте этот код, это поможет вам,

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

    ViewHolder holder = null; 
    Log.v("ConvertView", String.valueOf(position)); 

    if (convertView == null) { 
    LayoutInflater vi = (LayoutInflater)getSystemService(
    Context.LAYOUT_INFLATER_SERVICE); 
    convertView = vi.inflate(R.layout.country_info, null); 

    holder = new ViewHolder(); 
    holder.code = (TextView) convertView.findViewById(R.id.code); 
    holder.name = (CheckBox) convertView.findViewById(R.id.checkBox1); 
    convertView.setTag(holder); 

    holder.name.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
    CheckBox cb = (CheckBox) v ; 
    Country country = (Country) cb.getTag(); 
    Toast.makeText(getApplicationContext(), 
    "Clicked on Checkbox: " + cb.getText() + 
    " is " + cb.isChecked(), 
    Toast.LENGTH_LONG).show(); 
    country.setSelected(cb.isChecked()); 
} 
}); 
} 
    else { 
    holder = (ViewHolder) convertView.getTag(); 
} 

    Country country = countryList.get(position); 
    holder.code.setText(" (" + country.getCode() + ")"); 
    holder.name.setText(country.getName()); 
    holder.name.setChecked(country.isSelected()); 
    holder.name.setTag(country); 

    return convertView; 

    } 

} 


private void checkButtonClick() { 


Button myButton = (Button) findViewById(R.id.findSelected); 
myButton.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 

StringBuffer responseText = new StringBuffer(); 
responseText.append("The following were selected...\n"); 

ArrayList<Country> countryList = dataAdapter.countryList; 
for(int i=0;i<countryList.size();i++){ 
Country country = countryList.get(i); 
if(country.isSelected()){ 
    responseText.append("\n" + country.getName()); 
} 
} 

Toast.makeText(getApplicationContext(), 
    responseText, Toast.LENGTH_LONG).show(); 

} 
}); 

} 
0

Здесь вы почитайте комментарии, которые я добавил.

public class ServiceAdapter extends BaseAdapter { 
     public ArrayList<HashMap<String, String>> contArray; 
     ArrayList<Boolean> checked; 

     private Context mContext; 
     String resnID, reson; 
     Intent i; 

     public ServiceAdapter(Context paramContext, ArrayList<HashMap<String, String>> contList) { 
      this.mContext = paramContext; 
      this.contArray = contList; 
      //update code there make list of same size as contList 
      checked = new ArrayList<>(contList.size()); 
      //On start all are unchecked 
      Collections.fill(checked, Boolean.FALSE); 
     } 

     public int getCount() { 
      return this.contArray.size(); 
     } 

     public Object getItem(int paramInt) { 
      return Integer.valueOf(paramInt); 
     } 

     public long getItemId(int paramInt) { 
      return paramInt; 
     } 

     public View getView(final int paramInt, View paramView, 
          ViewGroup paramViewGroup) { 
      LayoutInflater localLayoutInflater = (LayoutInflater) this.mContext 
        .getSystemService("layout_inflater"); 
      Viewholder localViewholder = null; 
      Const.selectedIDs.clear(); 
      if (paramView == null) { 
       paramView = localLayoutInflater.inflate(
         R.layout.raw_single_contact, paramViewGroup, false); 
       localViewholder = new Viewholder(); 

       localViewholder.tv_name = ((TextView) paramView 
         .findViewById(R.id.tv_name)); 
       localViewholder.chk_id = ((CheckBox) paramView 
         .findViewById(R.id.chk_id)); 

       paramView.setTag(localViewholder); 
       // dont need below line try without below line if it works comment it otherwise let it stay there. 
       paramView.setTag(R.id.chk_id, localViewholder.chk_id); 

      } else { 
       localViewholder = (Viewholder) paramView.getTag(); 
      } 

      localViewholder.tv_name.setText(contArray.get(paramInt).get("serviceText")); 
      localViewholder.chk_id.setChecked(checked.get(paramInt)); 
      localViewholder.chk_id.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        try { 
         if (isChecked) { 
          checked.set(paramInt, true); 
          Const.selectedIDs.add(contArray.get(paramInt)); 
          Const.serviceArrayList.get(paramInt).put("flag", "1"); 
         } else { 
          checked.set(paramInt, false); 
          Const.selectedIDs.remove(contArray.get(paramInt)); 
          Const.serviceArrayList.get(paramInt).put("flag", "0"); 
         } 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
      return paramView; 
     } 

     public class Viewholder { 
      TextView tv_name; 
      CheckBox chk_id; 
     } 
    } 
+0

Hi..Ii я проверить это, братан –

+0

@sulphuricAcid надеюсь, что это исправит вашу проблему. –

+0

Я получаю arrayIndexoutofbounds exception..in вашего кода. –