我怎样才能从listview android中选择多个项目

时间:2015-08-07 11:35:11

标签: android listview checkbox

我被困在我的计划中。我必须从列表视图中执行多项选择项并从选择项中获取ID。

我试过但是错误

holder.cbCheckItem.setChecked(mCheckStates.get(position,false));

这里我发布了我的代码自定义适配器:

 public class CustomAdapterCategory extends BaseAdapter implements OnCheckedChangeListener{

public SparseBooleanArray mCheckStates;
List<Category> list_category = new ArrayList<Category>();
LayoutInflater mInflatar;
Context mConext;


 public CustomAdapterCategory(Context context, List<Category> m_list_category) {

    mConext = context;
    mInflatar = (LayoutInflater)mConext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.list_category = m_list_category; 
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list_category.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list_category.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return list_category.indexOf(list_category.get(position));
}

public class ViewHolder{
    TextView tvCatItem;
    CheckBox cbCheckItem;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub




        ViewHolder holder = new ViewHolder();
        if(convertView==null){
            convertView = mInflatar.inflate(R.layout.custom_items, null);
            holder.tvCatItem = (TextView)convertView.findViewById(R.id.tvCutomItem);
            holder.cbCheckItem = (CheckBox)convertView.findViewById(R.id.cbItemSelect);

            convertView.setTag(holder);
            holder.cbCheckItem.setTag(position);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        Category cats = list_category.get(position);
        Log.d("Category Name", cats.getCat_name());
        holder.tvCatItem.setText(cats.getCat_name());
        holder.cbCheckItem.setChecked(mCheckStates.get(position,false));
        holder.cbCheckItem.setOnCheckedChangeListener(this);


    return convertView;
}

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) {
    // TODO Auto-generated method stub
     mCheckStates.put((Integer) buttonView.getTag(), isChecked);  
}

}

thisi完成了我的主要活动:

   public void fun_cat()
    {
        try                                                                  
      LayoutInflater mInflator =                                                                         
     (LayoutInflater)getActivity().
     getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = mInflator.inflate(R.layout.dialog_custom_category, null);
        //final ArrayList<Integer> mMultiSelected = new ArrayList<Integer>();
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("Select category Type");
        builder.setView(view);


        ListView category_list = (ListView)view.findViewById(R.id.lvCategory);

        final Button b = (Button)view.findViewById(R.id.f_btnCat);
        lst_Category = dh.getAllCats();
        m_category = new CustomAdapterCategory(getActivity(), lst_Category);
        category_list.setAdapter(m_category); 
        category_list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub
                m_category.toggle(position);

            }
        });




        builder.setPositiveButton("Select", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

 Toast.LENGTH_LONG).show();

            }
        });

        builder.setNegativeButton("Cancel", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
            }
        });


        builder.show();



    } catch (Exception e) 
    {
        Log.e("LogException", ""+e.getMessage());
    }
}

1 个答案:

答案 0 :(得分:0)

自定义适配器Spars阵列中的

必须初始化列表大小。

public SparseBooleanArray mCheckStates;
List<Category> list_category = new ArrayList<Category>();
LayoutInflater mInflatar;
Context mConext;

 public CustomAdapterCategory(Context context, List<Category> m_list_category) {

    mCheckStates = new SparseBooleanArray(m_list_category.size());
    mConext = context;
    mInflatar = (LayoutInflater)mConext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.list_category = m_list_category; 
}
主Activity中的

必须添加此代码。

builder.setPositiveButton("Select", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                StringBuilder result = new StringBuilder();
                for(int i = 0 ; i < m_category.mCheckStates.size() == true ; i++ ){
                    if(m_category.mCheckStates.get(i)==true){
                        result.append(lst_Category.get(i).getCat_name());
                        result.append("\n");
                    }

                    CheckedCat = result.length() > 0 ? result.substring(0,result.length()-1) :"";
                }
                Toast.makeText(getActivity(), result, Toast.LENGTH_LONG).show();
            }
        });

这可能会有所帮助。