当我们选择android中的select all复选框时,选择所有listview复选框

时间:2013-06-24 10:36:11

标签: android

我正在使用片段,其中我有ListView和全选复选框。

Fragment class ..

public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

fb_custom_listview = (ListView) selectfriend_login
                .findViewById(R.id.friend_listview);

        select_all=(CheckBox) selectfriend_login.findViewById(R.id.select_all_checkbox);

        select_all.setOnCheckedChangeListener(this);

}

现在拥有custom adapter class extends Baseadapter 我在ImageView中有一个TextView,一个checkbox和一个layout,并在baseadapter类的getview方法中给布局充气。显示图片,这一切都没问题,显示文本并显示复选框。现在,当我单击select_all(fragment class)复选框时,应检查自定义适配器类的所有复选框,并取消选中所有未选中的复选框。这是类 用于自定义适配器

public class CustomListAdapter extends BaseAdapter
{
    private Context contxt;
    private ArrayList<String> user_friend_name;
    private ArrayList<String> user_friend_pic_url;
    private LayoutInflater inflater;
    private int friend_counter=0;
    private ChangeUIListner mlistner;
    public static ArrayList<Integer> selected_friend_pos;
    public static ArrayList<String> selected_friend_uid;
    public static ArrayList<String> selected_friend_name;
    private List<Model> list;
    private ViewHolder viewholder=null;



    public CustomListAdapter(Context context,ArrayList<String> user_friend_picurl, List<Model> model) {
        // TODO Auto-generated constructor stub
        this.contxt=context;
        this.list=model;
        user_friend_pic_url=user_friend_picurl;

        selected_friend_uid=new ArrayList<String>();
        selected_friend_name=new ArrayList<String>();
        selected_friend_pos=new ArrayList<Integer>();
        user_friend_name=GetFriendDetails.user_fb_friend_name;
        inflater=(LayoutInflater) contxt.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


        System.gc();


    }

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

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

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

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



        if(convertView==null)
        {
            viewholder=new ViewHolder();

            convertView=inflater.inflate(R.layout.custom_listview_friendlist, null);

            viewholder.fb_friend_name=(TextView) convertView.findViewById(R.id.friend_nametextview);

            viewholder.fb_friend_pic=(ImageView) convertView.findViewById(R.id.friend_picview);

            viewholder.fb_checkbox=(CheckBox) convertView.findViewById(R.id.friendselectcheckBox);

            viewholder.fb_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    int getposition=(Integer) buttonView.getTag();
                    Log.e("ITEM POSITION", "."+getposition);

                    list.get(getposition).setSelected(buttonView.isChecked());

                    if(isChecked==true)
                    {
                        if(selected_friend_pos.contains(getposition))
                        {
                            //nothing
                        }
                        else
                        {
                            friend_counter++;
                            mlistner.UpdateUi(friend_counter);
                            selected_friend_pos.add(getposition);
                            selected_friend_uid.add(GetFriendDetails.user_fb_friend_id.get(getposition));
                            selected_friend_name.add(GetFriendDetails.user_fb_friend_name.get(getposition));
                            Log.e("ARRAY SIZE", "."+selected_friend_uid.size());
                        }
                    }
                    else
                    {
                        if(selected_friend_pos.contains(getposition)){
                            friend_counter--;
                            mlistner.UpdateUi(friend_counter);
                            int del_posi=selected_friend_pos.indexOf(getposition);
                            selected_friend_pos.remove(del_posi);
                            selected_friend_name.remove(del_posi);
                            selected_friend_uid.remove(del_posi);
                            Log.e("ARRAYLIST", "REM"+del_posi);
                            Log.e("ARRAY SIZE", "."+selected_friend_uid.size());

                        }

                    }

                }
            });

            convertView.setTag(viewholder);

        }
        else
        {
            viewholder=(ViewHolder) convertView.getTag();
            new AsyncDownloadImage(viewholder).execute(user_friend_pic_url.get(position));
        }


        viewholder.fb_checkbox.setTag(position);

        viewholder.fb_checkbox.setChecked(list.get(position).isSelected());

        viewholder.fb_friend_name.setText(user_friend_name.get(position));

        viewholder.fb_friend_name.setTag(user_friend_name.get(position));

        viewholder.fb_friend_pic.setTag(user_friend_pic_url.get(position));

        //new AsyncDownloadImage(viewholder).execute(user_friend_pic_url.get(position));


        return convertView;
    }


    class ViewHolder
    {
        int id;
        ImageView fb_friend_pic;
        TextView fb_friend_name;
        CheckBox fb_checkbox;
    }

    public class AsyncDownloadImage extends AsyncTask<String, Object, Bitmap>
    {


        private HttpURLConnection connection;
        private InputStream is;
        private Bitmap bitmap;
        private URL url;
        ViewHolder holder;


        public AsyncDownloadImage(ViewHolder viewholder1) {
            // TODO Auto-generated constructor stub
            holder=viewholder1;
        }



        @Override
        protected Bitmap doInBackground(String... params) {
            // TODO Auto-generated method stub

            try {
                url=new URL(params[0]);

                try {
                    connection=(HttpURLConnection) url.openConnection();
                    connection.setDoInput(true);
                    connection.connect();
                    is=connection.getInputStream();
                    bitmap=BitmapFactory.decodeStream(is);
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                finally
                {
                    if(is!=null)
                    {
                        try {
                            is.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }if(connection!=null)
                    {
                        connection.disconnect();
                    }
                }

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            // TODO Auto-generated method stub
                if(result!=null)
                {
                    holder.fb_friend_pic.setImageBitmap(result);
                }

        }
    }   

    public void registerUIupdateListener(ChangeUIListner lisnter)
    {
        mlistner=lisnter;
    }
}

你可以看到我已经使用了listview复选框的单选项,所以它的工作正常并添加到arraylist中以便进一步操作。所以我们如何在单击或选择复选框时实现select all。谢谢

1 个答案:

答案 0 :(得分:1)

当你点击selectAll check按钮然后调用selectAllFromList(value)函数。 喜欢

     selectAllButton.setOnClickListener(new OnClickListener() {      
  @Override
      public void onClick(View v) {
            //is chkIos checked?
    if (((CheckBox) v).isChecked()) 
    {
        selectAllFromList(true);
    }else 
    {
        selectAllFromList(false);
    }

  }

以下功能将从列表中选中所有复选框:

  private void selectAllFromList(boolean b) {
            int cList = list.getCount();
            for(int i=0;i<cList;i++){   
                View sView = yourMainList.getChildAt(i);
            if(sView != null){
                CheckBox childCheckBox=(CheckBox)sView.findViewById(R.id.urCheckButton);
                    childCheckBox.setChecked(b);
                    childCheckBox.setOnClickListener(new OnClickListener() {

                      @Override
                      public void onClick(View v) {
                           if (((CheckBox) v).isChecked()) 
                            {
                                selectAllButton.setChecked(false);
                            }
                        }
                      });
                }   
            }   
        }
        });

希望这个可以帮助你