为什么EditText调用notifyDataSetChanged()方法?

时间:2018-08-31 20:32:45

标签: java android

我正在处理一个奇怪的情况。 我实现了一个扩展ArrayAdapter的类。 有两个视图类型,第一个视图是“经典”视图,第二个视图具有EditText。只有一个项目可以同时具有第二种视图。 当“编辑”文本为焦点且键盘处于打开状态时,我的适配器会不断重新加载,并在一段时间后停止(不修复)。 我不认为我重写的notifyDataSetChanged()方法是不好的用法:

@Override
public void notifyDataSetChanged() {
    Log.i("ADAPTER","reload");
    super.notifyDataSetChanged();
}

我没有得到这个日志,但是在:

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 
    Log.i("ADAPTER","new view");
    //
    // some stuff
    //
    return convertView;
}

我得到了这个日志。 显示问题的视频: enter image description here

public class LabelAdapter extends ArrayAdapter<Label> {

    private ConfigInterface ci;
    private DatabaseInterface di;
    private int itemSelected;

    public static int NORMAL = 0;
    public static int SELECTED = 1;

    public LabelAdapter(@NonNull Context context, int resource, DatabaseInterface databaseInterface, ConfigInterface configInterface) {
        super(context, resource, configInterface.getLabels());
        this.di = databaseInterface;
        this.ci = configInterface;
        itemSelected = -1;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        final Label label = this.getItem(position);
        final int pos = position;
        Log.i("Test","tous");
        if (getItemViewType(position) == SELECTED){
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.layout_lv_label_edit, null);
            EditText et = convertView.findViewById(R.id.et_label_name); // it's this edit text
            et.setText(label.getName());
            et.requestFocus();
            Log.i("Test","select");
            ImageView check = convertView.findViewById(R.id.check);
            check.setImageResource(R.drawable.ic_check);
            ImageView del = convertView.findViewById(R.id.delete);
            del.setImageResource(R.drawable.ic_delete);
            return convertView;
        }
        else {
            Log.i("Test","no sel");
            convertView =    LayoutInflater.from(getContext()).inflate(R.layout.layout_lv_label_item, null);
            TextView tv = convertView.findViewById(R.id.l_name);
            tv.setText(label.getName());
            ImageView iv = convertView.findViewById(R.id.ic_label);
            iv.setImageResource(R.drawable.ic_label);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("Listen","label");
                }
            });
            try {
                int color = Color.parseColor(label.getColor());
                iv.setColorFilter(color);
            } catch (Exception e) {
            }
            TextView n = convertView.findViewById(R.id.number);
            int n_ci = di.getCountDatabaseByLabel(label.getName());
            if (n_ci != 0) n.setText("" + n_ci);
            ImageView edit = convertView.findViewById(R.id.edit);
            edit.setImageResource(R.drawable.ic_edit);
            edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setSelectedItem(pos);
                    notifyDataSetChanged();
                }
            });
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("Listen","item");
                }
            });
            return convertView;
        }
    }

    public void setSelectedItem(int posistion){
        itemSelected = posistion;
    }

    @Override
    public int getItemViewType(int position) {
       if (position == itemSelected) return 1;
       else return 0;
    }

    @Override
    public void notifyDataSetChanged() {
        Log.i("Test","reload");
        super.notifyDataSetChanged();
    }
}

0 个答案:

没有答案