如何在RecyclerView中保存EditText内容

时间:2019-03-06 18:21:56

标签: java android android-recyclerview android-adapter

我有一个listItem和一个textView和一个EditText,就像一个时间表(textView耗时,EditText是供用户编写注意),因此它将是24个项目。

使用recyclerView时,当我滚动并删除我写的文本时它会更新,我尝试在适配器上实现TextWatcher,但我不知道如何使用它。

我已经搜索了如何保存文本视图内容,但是我发现所有使用数组存储值的人,我正在使用List

活动

public class CreateNoteActivity extends AppCompatActivity {

   private String[] eventos;
   private RecyclerView recyclerView;
   TextView data;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_create_note);

       recyclerView = findViewById(R.id.recycler_view);
       LinearLayoutManager layoutManager = new LinearLayoutManager(this);
       layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
       recyclerView.setLayoutManager(layoutManager);


       List<Evento> eventos = new ArrayList<>();
       carregarlistaDoDia(eventos);

       Adapter adapter = new Adapter(eventos);
       recyclerView.setAdapter(adapter);
       adapter.notifyDataSetChanged();
   }

   void carregarlistaDoDia(List<Evento>eventos){
       eventos.add(new Evento("00:00",""));
       eventos.add(new Evento("01:00",""));
       eventos.add(new Evento("02:00",""));
       eventos.add(new Evento("03:00",""));
       eventos.add(new Evento("04:00",""));
       eventos.add(new Evento("05:00",""));
       eventos.add(new Evento("06:00",""));
       eventos.add(new Evento("07:00",""));
       eventos.add(new Evento("08:00",""));
       eventos.add(new Evento("09:00",""));
       eventos.add(new Evento("10:00",""));
       eventos.add(new Evento("11:00",""));
       eventos.add(new Evento("12:00",""));
       eventos.add(new Evento("13:00",""));
       eventos.add(new Evento("14:00",""));
       eventos.add(new Evento("15:00",""));
       eventos.add(new Evento("16:00",""));
       eventos.add(new Evento("17:00",""));
       eventos.add(new Evento("18:00",""));
       eventos.add(new Evento("19:00",""));
       eventos.add(new Evento("20:00",""));
       eventos.add(new Evento("21:00",""));
       eventos.add(new Evento("22:00",""));
       eventos.add(new Evento("23:00",""));
   }

Evento类

public class Evento {

private String hora;
private String evento;

public Evento(String hora, String evento) {
    this.hora = hora;
    this.evento = evento;
}




public String getHora() {
    return hora;
}

public void setHora(String hora) {
    this.hora = hora;
}

public String getEvento() {
    return evento;
}

public void setEvento(String evento) {
    this.evento = evento;
}

适配器

public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> implements TextWatcher{


private List<Evento> eventos;

public Adapter(List<Evento> eventos) {
    this.eventos = eventos;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_listitem,viewGroup,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    String hora = eventos.get(i).getHora();
    String evento = eventos.get(i).getEvento();
    viewHolder.setData(hora, evento);

}

@Override
public int getItemCount() {
    return eventos.size();
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

}

@Override
public void afterTextChanged(Editable s) {

}

public static class ViewHolder extends RecyclerView.ViewHolder {

    private TextView hora;
    private EditText evento;

    public ViewHolder(View itemView) {
        super(itemView);
        hora = this.itemView.findViewById(R.id.hora);
        evento = this.itemView.findViewById(R.id.txt_evento);
    }

    private void setData(String txtHora, String txtEvento){
        hora.setText(txtHora);
        evento.setText(txtEvento);
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将TextWatcher外部的ViewHolder移到该位置。

为此创建一个类:

private class EditTextTextWatcher implements TextWatcher {
        private int posicion;
        private List<Eventos> listaEventos;

        public void updatePosicionYTexto(int posicion, List<Eventos> listaEventos) {
            this.listaEventos = listaEventos;
            this.posicion = posicion;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            listaEventos.get(posicion).setEvento(charSequence.toString());
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    } 

然后,当您要写便笺时,您要做:

viewHolder.editTextTextWatcher.updatePosicionYTexto(viewHolder.getAdapterPosition(),listaEventos);

ViewHolder内创建这两个方法,您必须将new EditTextTextWatcher()作为参数发送到ViewHolder,并在其中创建EditTextTextWatcher的实例,如:< / p>

public EditTextTextWatcher editTextTextWatcher;

然后添加这些方法:

void enableTextWatcher() {
        evento.addTextChangedListener(EditTextTextWatcher);
    }

    void disableTextWatcher() {
        evento.removeTextChangedListener(EditTextTextWatcher);
    }

不要忘记在ViewHolder

中添加这些方法
@Override
public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
    ((ViewHolder) holder).enableTextWatcher();
}

@Override
public void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder) {
    ((ViewHolder) holder).disableTextWatcher();
}

希望很清楚