从listView中选择特定位置

时间:2017-05-29 06:07:58

标签: android listview

我有一个带有复选框和2 textView的ListView,我想在其中显示员工姓名和员工ID号。问题是我想只检查具有smsMobilenumber的位置,并且不应检查该位置是否存在mobilenumber。请帮帮我。

public SmsEmployeeNameListAdapter(Context context, BaseActivity activity, ArrayList<CheckModelClass> employeeName, ArrayList<CheckModelClass> employeeNumber,
                                      ArrayList<CheckModelClass> smsMobileNum, ArrayList<CheckModelClass> employeeId, ArrayList<String> updatedSmsNumber, ArrayList<String> updatedUserId) {
    super(context, R.layout.sms_employeelist,employeeName);
    this.context = context;
    this.activity = activity;
    this.employeeName = employeeName;
    this.employeeNumber = employeeNumber;
    this.smsMobileNum = smsMobileNum;
    this.employeeId = employeeId;
    this.updatedSmsNumber = updatedSmsNumber;
    this.updatedUserId = updatedUserId;

}
private static class ViewHolder {

    CheckBox checkBox;
    TextView EmployeeName, EmployeeNumber;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    if(convertView == null){
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.sms_employeelist, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.EmployeeName = (TextView)convertView.findViewById(R.id.text1);
        viewHolder.EmployeeNumber = (TextView)convertView.findViewById(R.id.text2);
        viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkbox);
        viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();
                if(!smsMobileNum.get(getPosition).getName().isEmpty()){
                    employeeName.get(getPosition).setSelected(buttonView.isChecked());
                    if(isChecked){
                        updatedSmsNumber.add(smsMobileNum.get(getPosition).getName());
                        updatedUserId.add(employeeId.get(getPosition).getName());
                    }else{
                        updatedSmsNumber.remove(smsMobileNum.get(getPosition).getName());
                        updatedUserId.remove(employeeId.get(getPosition).getName());
                    }

                }

            }
        });
        convertView.setTag(viewHolder);
        convertView.setTag(R.id.text1, viewHolder.EmployeeName);
        convertView.setTag(R.id.text2, viewHolder.EmployeeNumber);
        convertView.setTag(R.id.checkbox, viewHolder.checkBox);

    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.checkBox.setTag(position);
    viewHolder.EmployeeName.setText(employeeName.get(position).getName());
    viewHolder.EmployeeNumber.setText(employeeNumber.get(position).getName());
    viewHolder.checkBox.setChecked(employeeName.get(position).isSelected());

    return convertView;
}

1 个答案:

答案 0 :(得分:0)

Class User
{
boolean hasNumber=flase;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    if(convertView == null){
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.sms_employeelist, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.EmployeeName = (TextView)convertView.findViewById(R.id.text1);
        viewHolder.EmployeeNumber = (TextView)convertView.findViewById(R.id.text2);
        viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.checkbox);

此处检查

if(userObj.get(getPosition).gethasNumber()))
{
              viewHolder.checkBox.setVisibility(View.VISIBLE);
            viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    int getPosition = (Integer) buttonView.getTag();
                    if(!smsMobileNum.get(getPosition).getName().isEmpty()){
                        employeeName.get(getPosition).setSelected(buttonView.isChecked());
                        if(isChecked){
                            updatedSmsNumber.add(smsMobileNum.get(getPosition).getName());
                            updatedUserId.add(employeeId.get(getPosition).getName());
                        }else{
                            updatedSmsNumber.remove(smsMobileNum.get(getPosition).getName());
                            updatedUserId.remove(employeeId.get(getPosition).getName());
                        }

                    }

                }
            });
}