0

I'm trying to create a button which will select/un-select all items in ListView. So far I've manager that creates checkboxes for all of the items, but now i have to create a button that will select all the items. I have used Metadata class in the listener for the checkboxes.

Here's my adapter:

 public class ResultsAdapter extends DataBufferAdapter<Metadata>{


        Context context;
         public ResultsAdapter(Context context) {
            super(context, R.layout.list_item);
    this.context = context;

        }



        private class ViewHolder {
            TextView txtView;
            CheckBox checkBox;
            CheckBox selectAllChk;


        }

        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {

            ViewHolder holder;


            if (convertView == null) {
                LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.list_item, null);

              holder = new ViewHolder();

                holder.txtView = (TextView) convertView.findViewById(R.id.textView1);
                holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
                holder.selectAllChk = (CheckBox) convertView.findViewById(R.id.selectAllButton);
                convertView.setTag(holder);

                final ViewHolder finalHolder = holder;


                holder.checkBox.setOnClickListener(new View.OnClickListener() {
                   /* private Context applicationContext;

                    public Context getApplicationContext() {
                        return applicationContext;
                    }*/

                    @Override
                    public void onClick(View v) {
    Metadata metadata = getItem(position);
                        finalHolder.txtView.setText(metadata.getTitle());
          }
                });
            } else {
                holder = (ViewHolder)convertView.getTag();

            }

            return convertView;
     }
    }
Abdul Aleem
  • 679
  • 8
  • 31
Tomi
  • 45
  • 6
  • try [this](http://stackoverflow.com/a/5239889/2900127) – 7geeky Jul 19 '16 at 08:17
  • Can you be more specific? I mean, did i have to put that for loop in my checkbox button, or...? – Tomi Jul 19 '16 at 08:36
  • `i have to create a button that will check all of the items.`. But not only the visible items i think. There are no checkboxes yet for the items which come in view after scrolling. Please rephrase your problem. – greenapps Jul 19 '16 at 08:46
  • For the rest you say that you have already checkboxes. But what happens if you check a file item, scroll it out of view and then scroll back. Is it still checked? Don't think so. – greenapps Jul 19 '16 at 08:48
  • With this CheckBox selectAllChk i want to do that (to select/deselect) all of the items from the list. – Tomi Jul 19 '16 at 08:49
  • Dont repeat what you want.. I already know what you want. Better answer my remarks/questions. – greenapps Jul 19 '16 at 08:50
  • Yes it is. The checkboxes for the items are working perfectly well. – Tomi Jul 19 '16 at 08:50
  • Then show the code where you are doing that. Where you keep the state of the checkboxes during scrolling. It looks to me that you omitted a lot of code from getView(). – greenapps Jul 19 '16 at 08:54
  • No. I have past it all of the code from the adapter. Everything what i'm doing for the checkboxes in at the checkbox.setOnClickListener. – Tomi Jul 19 '16 at 09:00
  • Strange getView code. You are not even setting a text in that TextView there. I suppose it should show the filename. So where are you giving it contents? You are setting it in onClick of the checkbox. But that is too late. And you will loose that text while scrolling. – greenapps Jul 19 '16 at 09:05

0 Answers0