【问题标题】:Checkbox selection issue in listView in androidandroid中listView中的复选框选择问题
【发布时间】:2016-10-10 22:50:31
【问题描述】:

我是 android 的新手,正在研究一个在项目中有复选框的 listView,当我检查任何项目时,其他项目也会自动检查,任何人都可以帮我解决它,我的适配器如下所示,

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;


    }



}

【问题讨论】:

标签: android listview android-adapter


【解决方案1】:

检查此代码,这将对您有所帮助,

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

}
});

}

【讨论】:

    【解决方案2】:

    你去阅读我添加的 cmets。

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

    【讨论】:

    • 嗨..我正在检查它,兄弟
    • @sulphuricAcid 希望它能解决您的问题。
    • 我在你的代码中得到了 arrayIndexoutofbounds 异常。
    • localViewholder.chk_id.setChecked(checked.get(paramInt));在这一行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    相关资源
    最近更新 更多