使用所选复选框的自定义适配器从列表视图中获取值到活动

时间:2014-12-31 06:55:28

标签: android android-listview

我在尝试获取活动中所选复选框listview值的值时遇到困难。我不知道该怎么做,任何人都可以建议我解决这个问题吗?这是我的代码:

public class CandidateAdapter extends BaseAdapter implements OnCheckedChangeListener {  
    Context ctxt;
    private LayoutInflater mInflater;
    private ArrayList<CandidateModel> candidateList = new ArrayList<CandidateModel>();
    private SparseBooleanArray mCheckStates;
    public CandidateAdapter(Context context,ArrayList<CandidateModel> candidateList) {      
        mInflater = LayoutInflater.from(context);
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.candidateList = candidateList;
        mCheckStates = new SparseBooleanArray(candidateList.size());
    }
    @Override
    public int getCount() {
        return candidateList.size();
    }

    @Override
    public CandidateModel getItem(int position) {
        return candidateList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view = convertView;
        final ViewHolder holder;
        if(view == null){   
            view = mInflater.inflate(R.layout.table_row_candidate,null);            
            holder = new ViewHolder();
            holder.candidateName = (TextView)view.findViewById(R.id.candidatenametxt);              
            holder.contactNumber = (TextView)view.findViewById(R.id.contactnumbertxt);
            holder.email = (TextView)view.findViewById(R.id.emailtxt);
            holder.contactCheckBox = (CheckBox)view.findViewById(R.id.contactchk);
            view.setTag(holder);

        }
        else{           
            holder = (ViewHolder)view.getTag();
        }
        final CandidateModel cm = candidateList.get(position);              
        String contactNumber = cm.getContactNumber();
        String candidateName = cm.getCandidateName();
        String email = cm.getEmail();
        if(!candidateName.equals("null") && candidateName!=null)
        {
            holder.candidateName.setText(cm.getCandidateName());            
        }
        else{
            holder.candidateName.setText("confidential");
        }           
        if(!contactNumber.equals("null") && contactNumber!="" && contactNumber!=null && !contactNumber.equals("NA"))
        {
            holder.contactNumber.setText(cm.getContactNumber());            
        }
        else{
            holder.contactNumber.setVisibility(View.GONE);          
        }
        if(!email.equals("null") && email!="" && email!=null && !email.equals("NA"))
        {           
            holder.email.setText(cm.getEmail());
        }
        else{           
            holder.email.setVisibility(View.GONE);
        }               
        holder.contactCheckBox.setTag(position);
        holder.contactCheckBox.setChecked(mCheckStates.get(position, false));
        holder.contactCheckBox.setOnCheckedChangeListener(this);        
        return view;
    }
    public boolean isChecked(int position) {
        return mCheckStates.get(position, false);
    }

    public void setChecked(int position, boolean isChecked) {
        mCheckStates.put(position, isChecked);

    }

    public void toggle(int position) {
        setChecked(position, !isChecked(position));
    }
   @Override
   public void onCheckedChanged(CompoundButton buttonView,
           boolean isChecked) {
        mCheckStates.put((Integer) buttonView.getTag(), isChecked);    
   }
   private class ViewHolder{
       public TextView candidateName;      
       public TextView contactNumber;
       public TextView email;
       public CheckBox contactCheckBox;
    }
}

///我的活动中的代码。在这里,我实际上是将数据添加到适配器。 我想从列表视图中选择所选复选框的数据,我将如何实现它?。

dataarray = datajsonobject.getJSONArray("candidateList");                   
                    CandidateAdapter ca = null;
                    final ArrayList<CandidateModel> candidateList = new ArrayList<CandidateModel>();                    
                    for (int i = 0; i < dataarray.length(); i++) {
                        JSONObject dataobject = new JSONObject();
                        dataobject = dataarray.getJSONObject(i);                        
                        String candidate_name = dataobject.getString("fullName");
                        String url = dataobject.getString("url");
                        String email = dataobject.getString("email");
                        String contactNumber = dataobject.getString("contactNumber");
                        String candidateId = dataobject.getString("candidateId");
                        //CandidateModel cm = new CandidateModel(candidate_name,score);
                        CandidateModel cm = new CandidateModel();
                        cm.setJobId(job_id);
                        cm.setCandidateName(candidate_name);
                        cm.setResumeURL(url);
                        cm.setContactNumber(contactNumber);
                        cm.setEmail(email);
                        cm.setSelected(false);
                        cm.setCandidateId(candidateId);
                        candidateList.add(cm);                      
                    }                                       
                    ca = new CandidateAdapter(Candidate.this,candidateList);
                    clist.setAdapter(ca);
                    clist.setOnItemClickListener(new OnItemClickListener() {
                          public void onItemClick(AdapterView<?> parent, View view,int position, long id) {                           
                              CandidateModel selItem = candidateList.get(position);//get the selected RowItem from the list                           
                              Intent downloadResumeIntent = new Intent(
                                        getApplicationContext(),
                                        DownloadResume.class);                              
                                final String url = selItem.getResumeURL();
                                downloadResumeIntent.putExtra("url",url);
                                downloadResumeIntent.putExtra("jobId",job_id);
                                //downloadResumeIntent.putExtra("resumeUniqueID", uniqueResumeId);                              
                                startActivity(downloadResumeIntent);
                              Toast.makeText(getApplicationContext(), "CLICKED", Toast.LENGTH_SHORT).show();   
                              }
                            });

4 个答案:

答案 0 :(得分:0)

在适配器中全局维护arraylist维护arraylist中的复选框状态现在你可以通过调用adaptet.arraylist来获取活动中的arraylist;

答案 1 :(得分:0)

如果candidateList订单始终保持不变,则可以将position用作Tag值。 尝试将此方法放入适配器并从活动中调用它。

public ArrayList<CandidateModel> GetCheckedItems(){

    ArrayList<CandidateModel> checkedItems = new ArrayList<CandidateModel>();

    for(int position = 0; position < candidateList.size(); position++){
        if(mCheckStates.get(position))
            checkedItems.add(candidateList.get(position));
    }

    return checkedItems;
}

也许我误解了你的问题:)。最好的问候。

答案 2 :(得分:0)

尝试在适配器项模块类CandidateModel中添加一个布尔字段:

public class CandidateModel {

    private boolean isSelected;

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean isSelected) {
        this.isSelected = isSelected;
    }
}

直接使用isSelected字段而不是SparseBooleanArray或者不需要SparseBooleanArray:

holder.contactCheckBox.setChecked(cm.isSelected());
holder.contactCheckBox..setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
       cm.setSelected(isChecked);         
    }
});

检查所有项目:

public ArrayList<CandidateModel> getCheckAllItem(){
    ArrayList<CandidateModel> candidateModelArrayList = new ArrayList<CandidateModel>();
    for(CandidateModel model :candidateList){
        if(model.isSelected()){
           candidateModelArrayList.add(model);
        }
    }
    return candidateModelArrayList
}

检查位置处的项目:

public boolean isItemChecked (int index){
   return candidateList.get(index).isSelected();
}

答案 3 :(得分:0)

使用CandidateModel类中的getter方法检索所需的所有数据。例如:selItem.getResumeURL();

如果您知道列表中已被点击的位置,为什么还需要知道复选框的状态?无论答案如何,您都可以使用现有方法adapter.isChecked(position)来查看是否选中了复选框。