只需一个按钮,即可保存由edittext组成的回收视图的所有数据



我有一个由输入数据的edittext组成的回收视图。对于每个编辑文本;保存";按钮,以便在按下时将相应编辑文本的值保存在数组列表中。然后,我使用适配器的getInput((方法从main活动中读取这个arraylist(在适配器内部定义(,该方法返回带有edittext数据的数组。但问题是,我需要为回收器中的每个编辑文本都有一个按钮,而我想用一个按钮来完成这一切。我找不到方法,因为在适配器中,你不能放一个独立的按钮,但你必须为每个项目放一个按钮。这是代码:

主要活动:

public class MainActivity extends AppCompatActivity {
ArrayList<String> als = new ArrayList();
public RecyclerView recycler;
public Button boton;
public ArrayList<String> input = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boton= findViewById(R.id.boton);
recycler= findViewById(R.id.recycler);
recycler.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
for(int i=0; i<7;i++)
{
als.add("Hola");
}
adapter a= new adapter(als);
recycler.setAdapter(a);
boton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
input=a.getInput();
for(String s: input)
{
Log.i("hola",s);
}
}
});
}
}

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</androidx.recyclerview.widget.RecyclerView>
<Button
android:id="@+id/boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mostrar" />
</LinearLayout>

适配器:

public class adapter extends RecyclerView.Adapter<adapter.ViewHolder>{
public ArrayList<String> listDatos;
public static ArrayList<String> input;
public adapter(ArrayList<String> listDatos) {
this.listDatos = listDatos;
this.input= new ArrayList<>();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.itemlist,null,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.nombre.setText(listDatos.get(position));
}
@Override
public int getItemCount() {
return listDatos.size();
}
public ArrayList<String> getInput()
{
return input;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
EditText nombre;
Button guardar;
public ViewHolder(View itemView) {
super(itemView);
nombre=itemView.findViewById(R.id.nombre);
guardar= itemView.findViewById(R.id.guardar);
guardar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
input.add(nombre.getText().toString());
}
});
}
}
}

itemlist.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<EditText
android:id="@+id/nombre"
android:layout_height="match_parent"
android:layout_width="wrap_content"
/>
<Button
android:id="@+id/guardar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Guardar" />
</LinearLayout>

Bien类:

public class Bien {
private String nombre;
private double valor;
public Bien(String nombre, double valor) {
this.nombre = nombre;
this.valor = valor;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public double getValor() {
return valor;
}
public void setValor(double valor) {
this.valor = valor;
}
}

根据您的描述,我建议您只需在每行的EditText中添加一个文本更改侦听器,这样无论何时更新文本,存储的数组也会立即更新。通过这种方式,您根本不需要任何保存按钮,也不需要单独的数组。

看起来像这样:

public class adapter extends RecyclerView.Adapter<adapter.ViewHolder>{
// Only need one array, it is updated as values are edited
private ArrayList<String> listDatos;
public adapter(ArrayList<String> listDatos) {
this.listDatos = listDatos;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.itemlist,null,false);
ViewHolder holder = new ViewHolder(view);
// add a watcher so any time the value in "nombre" is changed,
// it updates the value stored in the array at the current
// row's position immediately
holder.nombre.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
listDatos.set(getAdapterPosition(), s.toString());
}
});
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.nombre.setText(listDatos.get(position));
}

@Override
public int getItemCount() {
return listDatos.size();
}
// getInput can just return that stored array to get the
// current state at any point (no save buttons needed)
public ArrayList<String> getInput() {
return listDatos;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
EditText nombre;

public ViewHolder(View itemView) {
super(itemView);
nombre = itemView.findViewById(R.id.nombre);
}
}
}

最新更新