“This is my fifth day of the November Gwen Challenge. A textView and a switch are defined in the listView, but the state of the same switch is constantly changing when using the ListView.

The solution

Method one:

Layer a RelativeLayout over the ListView and set the ListView height to match_parent.

Method 2:

Set the ListView height to 0DP and the weight to 1

Obviously setting height to match_parent is not appropriate for dialog

I nested the Switch in the ListView. Click on the ListView item to Switch the state of the Switch, and use the List control, so add it to getView ()

The discovery is then made in the AdapterSix entries were refreshed 42 times

Three entries were refreshed 21 times

So getView() gets called seven times

So the question is, which one of these calls do you want to add?

One by one analysis found that 6 sets of data 1 groups: 0 2 groups: 25 = (7-3) * 6 + 1 to 3 groups: 26 (7-3) * 6 + 2 = 4 groups: 27 = (7-3) x 6 + 3 to 5 groups: 28 = (7-3) x 6 + 4 groups: 29 = (7-3) x 6 + 5

So: 7 minus 3=4 is the fourth time.

Method 3:

int List_Count = getCount();
            int aa = List_Count * 4 + 1;
            
            int_list_switch = new int[List_Count];

        
            for (int i = 0; i < List_Count; i++) {

                int_list_switch[i] = aa;
                aa++;
            }


Copy the code

GetView ()

     // Optimize the Switch control
            if (Optimize_getView == 0) {
                list_switch.add(holder.sw_Clock);
            }
            if (Optimize_getView == int_list_switch[Optimize_getView_Assist]) {
                list_switch.add(holder.sw_Clock);
                Log.d("To add the switch"."" + Optimize_getView);
                if (list_switch.size() < getCount())
                    Optimize_getView_Assist++;
            }

Copy the code

Do not put the map shall be treated as water paste

If you think there’s a better way, keep an eye out in the comments below!

All the code

    class TalkAdapter extends BaseAdapter implements OnClickListener {

        private Context mContext;
        private List<Course>  mData;
        private List<Switch> list_switch;


        private int[] int_list_switch;
        private int Optimize_getView = 0;
        private int Optimize_getView_Assist =0;
        public HashMap<Integer, Boolean> Switch_isSelected= new HashMap<Integer, Boolean>();


        public TalkAdapter(Context mContext, List
       
         mData)
        {
            this.mData = mData;
            this.mContext = mContext;


            list_switch = new ArrayList<>();
            list_switch.clear();

            int List_Count = getCount();
            int aa = List_Count * 4 + 1;
            int bb = 1;
            int_list_switch = new int[List_Count];

        
            for (int i = 0; i < List_Count; i++) {

                int_list_switch[i] = aa;
                aa++;
            }

            / / int_list_switch = new int [],17,18,19,28,29 {0}.


            if (MMkvListDate.isDataCapacity(SwitchStatus_KEY)) {              
                Switch_isSelected.clear();
                Switch_isSelected = MMkvListDate.getIBHashMap(SwitchStatus_KEY);             
            } else {
                for (int i = 0; i < getCount(); i++) {
                    Switch_isSelected.put(i, false); }}}public Switch getaSwitch(int position) {
            // toast.maketext (mContext,list_switch.size()+ "position: "+position, toast.length_short).show();
            return list_switch.get(position);
        }

        @Override
        public int getCount(a) {          
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

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

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

            ViewHolder holder = null;

            if (convertView == null) {

                convertView = LayoutInflater.from(mContext).inflate(R.layout.course_alarmclock_tips_list_item, parent, false);
                holder = new ViewHolder();

                holder.tv_className = (TextView) convertView.findViewById(R.id.coursealarmclocktipslistitemTextView1);
                holder.tv_classTime = (TextView) convertView.findViewById(R.id.coursealarmclocktipslistitemTextView2);

                holder.sw_Clock = (Switch)convertView.findViewById(R.id.coursealarmclocktipslistitemSwitch1);

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

            String CourseName =mData.get(position).getName();
            String jc = LitepalCourseUtil.getJc(mData.get(position).getTime());
            holder.tv_className.setText(CourseName);
            holder.tv_classTime.setText(jc);

            System.out.println("list_getView()" + position);

            // Optimize the Switch control
            if (Optimize_getView == 0) {
                list_switch.add(holder.sw_Clock);
            }
            if (Optimize_getView == int_list_switch[Optimize_getView_Assist]) {
                list_switch.add(holder.sw_Clock);
                Log.d("To add the switch"."" + Optimize_getView);
                if (list_switch.size() < getCount())
                    Optimize_getView_Assist++;
            }



            Log.d("Log_test"."_____" + Optimize_getView);

            Optimize_getView++;
            
            holder.sw_Clock.setChecked(Switch_isSelected.get(position));
           // holder.sw_Clock.setChecked(Switch_isSelected.get(position)==null? false:true);
            
            
            

            return convertView;
        }

        @Override
        public void onClick(View p1) {}private class ViewHolder { TextView tv_className; TextView tv_classTime; Switch sw_Clock; }}Copy the code
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<? > adapterView, View view,int position, long l) {
                    Switch aSwitch=item_Adapter.getaSwitch(position);
                    if (aSwitch.isChecked()) {
                        aSwitch.setChecked(false);
                        // Perform service processing
                    } else {
                        aSwitch.setChecked(true);
                        // Perform service processing
                    }          
                    Toast.makeText(getContext(), aSwitch.isChecked() + "The first"+ position, Toast.LENGTH_SHORT).show(); }}); }Copy the code