在ListView中选择多个项目

时间:2014-09-15 08:10:40

标签: android listview android-listview multi-select

我有Listview包含手机通讯录。我如何通过点击项目来选择多个联系人。我经历了this SO question但找不到合适的解决方案。

这是我迄今为止的工作:

    mContactListView = (ListView)findViewById(R.id.
     mArrayAdapter = new CustomAdapter(this,getContacts());
     mContactListView.setAdapter(mArrayAdapter);

我的CustomAdapter类:

class CustomAdapter extends ArrayAdapter<Contacts>
{
     LayoutInflater layoutInflater;
    private List<Contacts> conctactList;
    private SparseBooleanArray mSelectedItemIds;


    public CustomAdapter(Context context, List<Contacts> mList)
    {
        super(context, R.layout.single_contact_layout,mList);
        this.conctactList = mList;
        layoutInflater = LayoutInflater.from(context);
        mSelectedItemIds = new SparseBooleanArray();
    }

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

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

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

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            final Holder holder;
            Bitmap bitmap = null;

            if(convertView == null)
            {
                holder = new Holder();


                convertView = layoutInflater.inflate(R.layout.single_contact_layout, null);
                holder.name = (TextView) convertView.findViewById(R.id.contact_name);
                holder.number = (TextView) convertView.findViewById(R.id.contact_number);

                holder.contact_img = (ImageView)convertView.findViewById(R.id.contact_img);


               convertView.setTag(holder);
                convertView.setTag(R.id.contact_name, holder.name);
            }

            else{
                holder = (Holder) convertView.getTag();
            }

            holder.name.setText(conctactList.get(position).getName());
            holder.number.setText(conctactList.get(position).getNumber());
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), Uri.parse(contactsList.get(position).getImgUri()));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            holder.contact_img.setImageBitmap(ImageHelper.getRoundedCornerBitmap(bitmap, 100));


            return convertView;
        }

        @Override
        public void remove(Contacts object) {
            // TODO Auto-generated method stub
            contactsList.remove(object);
            notifyDataSetChanged();
        }

         public void removeSelection() {
             mSelectedItemIds = new SparseBooleanArray();
             notifyDataSetChanged();
         }

        public void toggleSelection(int position)
        {
            selectView(position, !mSelectedItemIds.get(position));
        }

        public void selectView(int position,boolean value)
        {
            if(value)
            {
                mSelectedItemIds.put(position, value);
            }
            else
            {
                mSelectedItemIds.delete(position);
            }
            notifyDataSetChanged();
        }

        public int getSelectedCount()
        {
            return mSelectedItemIds.size();
        }

        public SparseBooleanArray getSelectedIds()
        {
            return mSelectedItemIds;
        }
}

P.S:我想在不使用Contextual ActionBar的情况下选择项目

0 个答案:

没有答案