如何从RecyclerView中的编辑文本(EditText)中获取值,安卓。

huangapple go评论78阅读模式
英文:

How get value from Edit Text in a RecyclerView Android

问题

以下是翻译好的内容:

public class AdaptadorVerduras extends FirebaseRecyclerAdapter<Verduras, AdaptadorVerduras.Holder> {

List<String> nombre = new ArrayList<>();
List<String> cantidad = new ArrayList<>();

public AdaptadorVerduras(@NonNull FirebaseRecyclerOptions<Verduras> options) {
   super(options);
}

@Override
protected void onBindViewHolder(@NonNull Holder holder, int i, @NonNull Verduras verduras) {
   holder.tv_nombreVerdura.setText(verduras.getNombre());
   holder.tv_cantidadPromedio.setText(String.valueOf(verduras.getCantidad()));
   nombre.add(holder.tv_nombreVerdura.getText().toString());
   cantidad.add(holder.et_Cantidad.getText().toString());
}

@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
   View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
   return new Holder(view);
}

public static class Holder extends RecyclerView.ViewHolder {
   TextView tv_nombreVerdura, tv_cantidadPromedio;
   ImageView iv_Producto;
   EditText et_Cantidad;

   public Holder(@NonNull View itemView) {
       super(itemView);
       tv_nombreVerdura = (TextView) itemView.findViewById(R.id.tv_nombreVerdura);
       tv_cantidadPromedio = (TextView) itemView.findViewById(R.id.tv_cantidadPromedio);
       iv_Producto = (ImageView) itemView.findViewById(R.id.iv_Producto);
       et_Cantidad = (EditText) itemView.findViewById(R.id.et_Cantidad);
   }
}
}
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_verduras);
    cantidad = findViewById(R.id.et_Cantidad);
    nombre = findViewById(R.id.tv_nombreVerdura);
    bn_Insertar = findViewById(R.id.bn_Insertar);

    recyclerView = findViewById(R.id.recycler);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
   
    FirebaseRecyclerOptions<Verduras> verduras =
            new FirebaseRecyclerOptions.Builder<Verduras>()
                    .setQuery(FirebaseDatabase.getInstance().getReference().child("Productos"), Verduras.class)
                    .build();

    adaptadorVerduras = new AdaptadorVerduras(verduras);
    recyclerView.setAdapter(adaptadorVerduras);

    reference = firebaseDatabase.getInstance().getReference().child("Pedido");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                id = (int) dataSnapshot.getChildrenCount();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    bn_Insertar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          
            pedido.setNombre(String.valueOf(adaptadorVerduras.nombre.toString()));
            pedido.setCantidad(String.valueOf(adaptadorVerduras.cantidad.toString()));
            reference.child(String.valueOf(id + 1)).setValue(pedido);
        }
    });
}

@Override
protected void onStart() {
    super.onStart();
    adaptadorVerduras.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    adaptadorVerduras.stopListening();
}

如果您还有其他问题或需要进一步的帮助,请随时提问。

英文:

I have a RecyclerView with an EditText so that the user can add the desired quantity of a product. I am using the Firebase RecyclerView and I am inserting the values ​​into the Firebase database. I am fairly new to Android.

This is My Adapter, after much searching I ended up saving the name of the product and the quantity in ArrayLists, the name of the product that comes from a TextView it's actually being inserted into the Firebase Database, but the Edit Text quantity is not.

public class AdaptadorVerduras extends FirebaseRecyclerAdapter&lt;Verduras, AdaptadorVerduras.Holder&gt; {
List&lt;String&gt; nombre = new ArrayList&lt;&gt;();
List&lt;String&gt; cantidad = new ArrayList&lt;&gt;();
public AdaptadorVerduras(@NonNull FirebaseRecyclerOptions&lt;Verduras&gt; options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull Holder holder, int i, @NonNull Verduras verduras) {
holder.tv_nombreVerdura.setText(verduras.getNombre());
holder.tv_cantidadPromedio.setText(String.valueOf(verduras.getCantidad()));
nombre.add(holder.tv_nombreVerdura.getText().toString());
cantidad.add(holder.et_Cantidad.getText().toString());
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
return new Holder(view);
}
public static class Holder extends RecyclerView.ViewHolder {
TextView tv_nombreVerdura, tv_cantidadPromedio;
ImageView iv_Producto;
EditText et_Cantidad;
public Holder(@NonNull View itemView) {
super(itemView);
tv_nombreVerdura = (TextView) itemView.findViewById(R.id.tv_nombreVerdura);
tv_cantidadPromedio = (TextView) itemView.findViewById(R.id.tv_cantidadPromedio);
iv_Producto = (ImageView) itemView.findViewById(R.id.iv_Producto);
et_Cantidad = (EditText) itemView.findViewById(R.id.et_Cantidad);
}
}

This is my Activity. According to what I am trying to do is call the quantity list from my onClick but nothing is happening.

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verduras);
cantidad = findViewById(R.id.et_Cantidad);
nombre = findViewById(R.id.tv_nombreVerdura);
bn_Insertar = findViewById(R.id.bn_Insertar);
recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions&lt;Verduras&gt; verduras =
new FirebaseRecyclerOptions.Builder&lt;Verduras&gt;()
.setQuery(FirebaseDatabase.getInstance().getReference().child(&quot;Productos&quot;), Verduras.class)
.build();
adaptadorVerduras = new AdaptadorVerduras(verduras);
recyclerView.setAdapter(adaptadorVerduras);
reference = firebaseDatabase.getInstance().getReference().child(&quot;Pedido&quot;);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
id = (int) dataSnapshot.getChildrenCount();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
bn_Insertar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pedido.setNombre(String.valueOf(adaptadorVerduras.nombre.toString()));
pedido.setCantidad(String.valueOf(adaptadorVerduras.cantidad.toString()));
reference.child(String.valueOf(id + 1)).setValue(pedido);
}
});
}
@Override
protected void onStart() {
super.onStart();
adaptadorVerduras.startListening();
}
@Override
protected void onStop() {
super.onStop();
adaptadorVerduras.stopListening();
}

This is how my DBlooks after inserting the data, as you can see is actually inserting the names but no the quantity.

My Firebase DB: https://i.stack.imgur.com/WM40W.png

My App: https://i.stack.imgur.com/A7l12.png

答案1

得分: 0

创建一个监听器类,并在其中定义一个带有一个字符串参数的函数。
将该监听器添加到你的RecyclerView适配器构造函数中。
现在在你的ViewHolder类中为EditText添加'addTextChangedListener',如下所示:

et_Cantidad.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// 待办事项:在文本改变后执行
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// 待办事项:在文本改变前执行
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
doSomething();
}
});

在'onTextChanged'方法中添加刚刚定义的监听器中的函数。onTextChanged会在你输入文字时从EditText中检索文本,并将该文本传递到监听器函数中。

现在,在myActivity中,你将会在适配器类对象中获取到监听器函数,以及在你输入文字时从EditText中获取字符串,然后可以对该文本进行任何想做的操作。

英文:

Make a listener class and define a function in it with one String param.
Add that listener in your recyclerView adapter constructor.
Now add 'addTextChangedListener' for your edittext in your viewHiolder class like this:

    et_Cantidad.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
doSomething();
} 
});

add that function you just defined in the listener in the 'onTextChanged' method. The onTextChanged will retrieve the text from the edittext when you're typing something and pass that text into the listener function.

Now, in the myActivity you'll get the listener function in the adapter class object and you'll get the string from the edittext as you type and then do whatever you want with that text.

huangapple
  • 本文由 发表于 2020年8月16日 02:51:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63429716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定