2

我创建了自己的适配器,它扩展了 ArrayAdapter ...问题是,我在列表中有 3 种可能的行,并且通过快速滚动某些行是不正确的,我认为是因为带有 android 视图的“回收器机制”.. . 看了很多StackOverflow上的教程,还是有这个问题。我的代码有问题吗?(代码没有优化,我总是在功能正确时优化它)。你能帮帮我吗?谢谢!

我的 ArrayAdapter 类:

package com.example.birthdayReminder;

import java.util.List;

import android.app.Activity;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class InteractiveArrayAdapter extends ArrayAdapter<Human> {

        private final List<Human> list;
        private final Activity context;
        private int TYPE = 0;
        ViewHolder holder;

        public InteractiveArrayAdapter(Activity context, List<Human> list) {
                super(context, R.layout.programlistview_row_layout, list);
                this.context = context;
                this.list = list;
        }

        static class ViewHolder {
                protected TextView text;
                protected TextView birthDate;
                protected TextView daysToBirthDate;
                protected TextView daysToBirthDateValue;
                protected CheckBox checkbox;
        }

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

                //View view = null;

                Human h = list.get(position);

                holder = null;

                TYPE = getItemViewType(position);
                //System.out.println("getView " + position + " " + convertView + " type = " + type);

                if (convertView == null) {

                        //LayoutInflater inflator = context.getLayoutInflater();
                        LayoutInflater inflator = LayoutInflater.from(getContext());
                        holder = new ViewHolder();

                        switch(TYPE){
                                case 0:
                                        convertView = inflator.inflate(R.layout.programlistview_row_layout_birthday, parent, false);
                                        break;
                                case 1:
                                        convertView = inflator.inflate(R.layout.programlistview_row_layout_text_sentence, parent, false);
                                        break;
                                case 2:
                                        convertView = inflator.inflate(R.layout.programlistview_row_layout, parent, false);
                                        break;
                        }

                        convertView.setTag(holder);

                } else {
                        holder = (ViewHolder) convertView.getTag();
                        ((ViewHolder) convertView.getTag()).checkbox.setTag(list.get(position));
                }

                holder.text = (TextView) convertView.findViewById(R.id.label);
                holder.birthDate = (TextView) convertView.findViewById(R.id.birthDate);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.check);

                holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                Human element = (Human) holder.checkbox.getTag();
                                element.setSelected(buttonView.isChecked());
                        }
                });

                convertView.setTag(holder);
                holder.checkbox.setTag(h);

                holder.text.setText(h.getName() + " " + h.getLastName());
                holder.text.setTypeface(null, Typeface.BOLD);
                holder.birthDate.setText("    Birthday: " + String.valueOf(h.getBirthDate()));

                switch(TYPE){
                        case 0:
                                holder.daysToBirthDate = (TextView) convertView.findViewById(R.id.daysTobirthDate);

                                if(h.getAge() == 1)
                                        holder.daysToBirthDate.setText("    ... is " + (int)(h.getAge()) + " year old today!");
                                else
                                        holder.daysToBirthDate.setText("    ... is " + (int)(h.getAge()) + " years old today!");
                                break;
                        case 1:
                                holder.daysToBirthDate = (TextView) convertView.findViewById(R.id.daysTobirthDate);                            

                                if(h.getAge() == 0){
                                        holder.daysToBirthDate.setText("    ... was born today!");
                                }
                                else if(h.getAge() < 0)
                                        holder.daysToBirthDate.setText("    ... was not born yet!");                   
                                break;
                        case 2:
                                holder.daysToBirthDateValue = (TextView) convertView.findViewById(R.id.daysTobirthDate_value);
                                holder.daysToBirthDate = (TextView) convertView.findViewById(R.id.daysTobirthDate);
                                holder.daysToBirthDate.setText("    Days to birthday: ");
                                holder.daysToBirthDateValue.setText(String.valueOf(h.getDaysToBirthDate()));
                                break;
                }

                holder.checkbox.setChecked(h.isSelected());

                return convertView;

        }

        @Override
        public int getViewTypeCount(){
                return 3;

        }

        @Override
        public int getItemViewType(int position){
                Human h = list.get(position);
                if (h.getDaysToBirthDate() == 0 && h.getAge() > 0){
                        TYPE = 0;
                }
                else if((h.getDaysToBirthDate() == 0 && h.getAge() == 0) || (h.getDaysToBirthDate() == 0 && h.getAge() < 0) || (h.getDaysToBirthDate() != 0 && h.getAge() < 0)){
                        TYPE = 1;
                }
                else if(h.getDaysToBirthDate() != 0 && h.getAge() >= 1){
                        TYPE = 2;
                }
                return TYPE;
        }

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

        @Override
        public Human getItem(int position) {
            return list.get(position);
        }

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


        /*
    //private class AnimalAdapter extends BaseAdapter {
        final List<com.example.birthdayReminder.row.Row> rows;

        InteractiveArrayAdapter(Activity context, List<Human> humans) {
                super(context, R.layout.programlistview_row_layout);
                //super(context, R.layout.programlistview_row_layout, list);
            rows = new ArrayList<com.example.birthdayReminder.row.Row>();//member variable

            for (Human h : humans) {
                if (h.getDaysToBirthDate() == 0 && h.getAge() > 0){
                    rows.add((com.example.birthdayReminder.row.Row) new BirthdayRow(LayoutInflater.from(context), h));
                } else if((h.getDaysToBirthDate() == 0 && h.getAge() == 0) || (h.getDaysToBirthDate() == 0 && h.getAge() < 0) || (h.getDaysToBirthDate() != 0 && h.getAge() < 0)){
                    rows.add((com.example.birthdayReminder.row.Row) new TodayFutureRow(LayoutInflater.from(context), h));
                } else if(h.getDaysToBirthDate() != 0 && h.getAge() >= 1){
                        rows.add((com.example.birthdayReminder.row.Row) new NonBirthdayRow(LayoutInflater.from(context), h));
                }
            }
        }

        @Override
        public int getViewTypeCount() {
            return com.example.birthdayReminder.row.RowType.values().length;
        }

        @Override
        public int getItemViewType(int position) {
            return rows.get(position).getViewType();
        }

        public int getCount() {
            return rows.size();
        }

        //public Object getItem(int position) {
        //    return position;
        //}

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

        public View getView(int position, View convertView, ViewGroup parent) {
            return rows.get(position).getView(convertView);
        }
    //}
        */

}
4

1 回答 1

0

删除持有人并使用您自己的逻辑来检查项目。因为如果您放置持有人,您的 *视图将在列表视图中重用,因此您的数据将被折叠。使用持有人很好(内存观点)。但对于您的要求你不能使用支架。*

public class InteractiveArrayAdapter extends ArrayAdapter<Human> {


     private final List<Human> list;
        private final Activity context;
        private int TYPE = 0;
        private LayoutInflater l_Inflater;

        public ItemListBaseAdapter(Context context,
                List<Human> list) {

              this.context = context;
                         this.list = list;

        }

        public int getCount() {
            return list.size();

        }

        public Object getItem(int position) {
            return list.get(position);
        }

        public long getItemId(int position) {


            return position;
        }

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


                Human h = list.get(position);

                TYPE = getItemViewType(position);
                //System.out.println("getView " + position + " " + convertView + " type = " + type);

                if (convertView == null) {

                        //LayoutInflater inflator = context.getLayoutInflater();
                        LayoutInflater inflator = LayoutInflater.from(getContext());

                        switch(TYPE){
                                case 0:
                                        convertView = inflator.inflate(R.layout.programlistview_row_layout_birthday, parent, false);
                                        break;
                                case 1:
                                        convertView = inflator.inflate(R.layout.programlistview_row_layout_text_sentence, parent, false);
                                        break;
                                case 2:
                                        convertView = inflator.inflate(R.layout.programlistview_row_layout, parent, false);
                                        break;
                        }



                } 



              text = (TextView) convertView.findViewById(R.id.label);
               birthDate = (TextView) convertView.findViewById(R.id.birthDate);
               checkbox = (CheckBox) convertView.findViewById(R.id.check);

                checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                        }
                });

                checkbox.setTag(h);

               text.setText(h.getName() + " " + h.getLastName());
               text.setTypeface(null, Typeface.BOLD);
               birthDate.setText("    Birthday: " + String.valueOf(h.getBirthDate()));

                switch(TYPE){
                        case 0:
                       daysToBirthDate = (TextView) convertView.findViewById(R.id.daysTobirthDate);

                                if(h.getAge() == 1)
                                     daysToBirthDate.setText("    ... is " + (int)(h.getAge()) + " year old today!");
                                else
                                        holder.daysToBirthDate.setText("    ... is " + (int)(h.getAge()) + " years old today!");
                                break;
                        case 1:
                                holder.daysToBirthDate = (TextView) convertView.findViewById(R.id.daysTobirthDate);                            

                                if(h.getAge() == 0){
                                        holder.daysToBirthDate.setText("    ... was born today!");
                                }
                                else if(h.getAge() < 0)
                                        holder.daysToBirthDate.setText("    ... was not born yet!");                   
                                break;
                        case 2:
                               daysToBirthDateValue = (TextView) convertView.findViewById(R.id.daysTobirthDate_value);
                                daysToBirthDate = (TextView) convertView.findViewById(R.id.daysTobirthDate);
                               daysToBirthDate.setText("    Days to birthday: ");
                                holder.daysToBirthDateValue.setText(String.valueOf(h.getDaysToBirthDate()));
                                break;
                }

                holder.checkbox.setChecked(h.isSelected());

                return convertView;

        }

        @Override
        public int getViewTypeCount(){
                return 3;

        }

        @Override
        public int getItemViewType(int position){
                Human h = list.get(position);
                if (h.getDaysToBirthDate() == 0 && h.getAge() > 0){
                        TYPE = 0;
                }
                else if((h.getDaysToBirthDate() == 0 && h.getAge() == 0) || (h.getDaysToBirthDate() == 0 && h.getAge() < 0) || (h.getDaysToBirthDate() != 0 && h.getAge() < 0)){
                        TYPE = 1;
                }
                else if(h.getDaysToBirthDate() != 0 && h.getAge() >= 1){
                        TYPE = 2;
                }
                return TYPE;
        }



    }
于 2013-07-30T14:10:43.827 回答