ViewHolder在getView方法中返回错误的位置

时间:2017-11-30 15:31:00

标签: java android adapter getview

我有卡片视图,我需要更改其中一些。问题是,当我有多个cardview时,我点击一个,它会更改我拥有的最后一个cardview的数据,而不是我点击的那个

    @RequiresApi(api = Build.VERSION_CODES.N)
public class ReagendarAdapter extends BaseAdapter {

    private static final String TAG = "vania";

    private List<Visita> visitaList;
    private Context context;
    private Calendar dateTime = Calendar.getInstance();
    private String dataAntiga;
    private Date novaDataInicio =  new Date();
    private Date novaDataFinal  =  new Date();

    private ViewHolder holder;

    public ReagendarAdapter(List<Visita> visitaList, Context context) {
        this.visitaList = visitaList;
        this.context = context;
    }

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

    @Override
    public Visita getItem(int position) {
        return visitaList.get(position);
    }

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

    class ViewHolder {

        @BindView(R.id.txtCliente)
        TextView txtCliente;
        @BindView(R.id.txtAtividade)
        TextView txtAtividade;
        @BindView(R.id.txtData)
        TextView txtData;
        @BindView(R.id.txtDuracao)
        TextView txtDuracao;
        @BindView(R.id.txtNovaData)
        TextView txtNovaData;

        @BindView(R.id.llData)
        LinearLayout llData;
        @BindView(R.id.llMudarDuracao)
        LinearLayout llMudarDuracao;

        @BindView(R.id.btnReagendr)
        Button btnReagendr;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    @SuppressLint({"SimpleDateFormat", "SetTextI18n"})
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;

        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            assert inflater != null;
            row = inflater.inflate(R.layout.reagendar_row, parent, false);
            holder = new ViewHolder(row);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        final Visita visita = visitaList.get(position);

        assert visita != null;
        Date dataInicio = new Date(visita.getDtinicio() + (3 * 3600 * 1000));

        if (visita.getNome() != null)
            holder.txtCliente.setText(visita.getNome());
        if (visita.getDtinicio() != null) {
            dataAntiga = " " + new SimpleDateFormat("dd/MM 'às' HH:mm").format(dataInicio);
            holder.txtData.setText(dataAntiga);
        }
        if (visita.getTipoevento() != null) {
            String atividade = getPrefString(visita.getTipoevento().toString(), context);
            holder.txtAtividade.setText(atividade);
        }

        holder.txtDuracao.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!dataAntiga.equalsIgnoreCase(String.valueOf(holder.txtData))){
                    holder.btnReagendr.setBackgroundColor(context.getResources().getColor(R.color.BrightBlue));
                    holder.btnReagendr.setEnabled(true);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        holder.llData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateDate();
            }
        });


        holder.llMudarDuracao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateDate();
            }
        });

        holder.btnReagendr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                visita.setStatusVisita("reagendada");
                visita.setDataInicio(novaDataInicio);
                visita.setDataFim(novaDataFinal);
                updateAtvidade(context, visita);
            }
        });
        return row;
    }

    private void updateDate() {
        new DatePickerDialog(context, R.style.Picker, d, dateTime.get(Calendar.YEAR), dateTime.get(Calendar.MONTH), dateTime.get(Calendar.DAY_OF_MONTH)).show();

    }

    private void updateTime() {
        new TimePickerDialog(context, R.style.Picker, t, dateTime.get(Calendar.HOUR_OF_DAY), dateTime.get(Calendar.MINUTE), true).show();
    }

    private void updateHour() {
        NumberPicker numberPicker = new NumberPicker(context);
        numberPicker.setMinValue(0);
        numberPicker.setMaxValue(10);
        numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                if (newVal != 1)
                    holder.txtDuracao.setText(" " + newVal + " horas");
                else
                    holder.txtDuracao.setText(" " + newVal + " hora");
                holder.txtNovaData.setText("Nova Data: ");
                holder.llMudarDuracao.setVisibility(View.VISIBLE);
                holder.txtDuracao.setTextColor(ColorStateList.valueOf(context.getResources().getColor(R.color.BrightBlue)));
                novaDataFinal = new Date(novaDataInicio.getTime() + ( newVal* 3600 * 1000));
            }
        });

        AlertDialog.Builder builder = new AlertDialog.Builder(context).setView(numberPicker);
        builder
                .setTitle("Duração")
                .setIcon(R.drawable.ic_action_timer)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .show();
    }

    private DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener()

    {

        @Override
        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
            dateTime.set(Calendar.YEAR, year);
            dateTime.set(Calendar.MONTH, month);
            dateTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateTime();
        }
    };

    private TimePickerDialog.OnTimeSetListener t = new TimePickerDialog.OnTimeSetListener() {
        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            dateTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
            dateTime.set(Calendar.MINUTE, minute);
            novaDataInicio = dateTime.getTime();
            atualizaString(holder.txtData);
            updateHour();
        }
    };

    @SuppressLint("SimpleDateFormat")
    private void atualizaString(TextView textView) {
        textView.setText(String.format(" %s", new SimpleDateFormat("dd/MM 'às' HH:mm").format(dateTime.getTime())));
        textView.setTextColor(ColorStateList.valueOf(context.getResources().getColor(R.color.BrightBlue)));
    }

我只能通过在类中创建viewHolder来访问textview,我不知道该怎么办。 当我在getview中创建访问方法时,只有在再次点击后才能同时更新cardviews

0 个答案:

没有答案