I am currently working on an application which read some database, display it on my application (text and image) and I want it to be able to send email containing a certain quantity of items provided by my database.
For the moment, the layout is pretty much finished, I can read data from the database and display it on my application. What I struggle with is the following : I have a RecyclerView in which are several EditText (the number of these EditText depends of the number of the items in the databse). I can enter number in my EditText but when I scroll down these numbers disappear.
After some research, I came across TextWatcher. I set up one watching the EditText but its functions are not entered when I type something in my EditText.
MainActivity.java :
//inside MainActivity class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//mylist is the xml file in which the EditText with id Quantite is
//defined
setContentView(R.layout.mylist);
final EditText quantiteText = (EditText) findViewById(R.id.Quantite);
quantiteText.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) {
System.out.println("Entering onTextChanged" + s.toString());
int position = (int) quantiteText.getTag();
//Produit is a class containing a field quantite and a
//function setQuantite
Produit produit = produitList.get(position);
produit.setQuantite(s.toString());
mAdapter.update(position, produit);
}
@Override
public void afterTextChanged(Editable s) {
}
});
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/Apparence"
android:layout_width="70dp"
android:layout_height="70dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/Titre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Titre"
android:textStyle="bold"
android:textSize="12sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4d4d4d" />
<TextView
android:id="@+id/Code_article"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Code_article"
android:textSize="12sp"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/Designation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Designation"
android:textSize="12sp"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/Quantite_par_carton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantite par Carton"
android:textSize="12sp"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<!-- Just here so that the next linearlayout is far on the right -->
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginRight="10dp"
android:descendantFocusability="beforeDescendants">
<TextView
android:id="@+id/QuantiteText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="12sp"
android:text="Quantité"/>
<EditText
android:id="@+id/Quantite"
android:inputType="text"
android:textSize="12sp"
android:saveEnabled="false"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
RecyclerViewAdapter.java :
package com.example.myapplication;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {
private List<Produit> produitList;
private static List<String> mEditTextValues = new ArrayList<>();
public RecyclerViewAdapter(List<Produit> produitList) {
this.produitList = produitList;
for(int i = 0; i < produitList.size(); i++)
{
mEditTextValues.add("I'm editText number" + i);
}
notifyDataSetChanged();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.mylist, parent, false);
ViewHolder mViewHolder = new ViewHolder(itemView);
return mViewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//System.out.println("Entering onBindViewHolder");
Produit produit = produitList.get(position);
holder.titre.setText(produit.getTitre());
holder.code_article.setText(produit.getCode_Article().toString());
holder.designation.setText(produit.getDesignation());
holder.quantite_par_carton.setText(produit.getQuantite_par_carton());
holder.quantite.setText(produit.getQuantite());
Picasso.get().load(produit.getApparence()).into(holder.apparence);
}
@Override
public int getItemCount() {
return produitList.size();
}
//
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public void initialise(List<Produit> produitList) {
System.out.println("Entering update");
this.produitList.clear();
this.produitList.addAll(produitList);
notifyDataSetChanged();
}
public void update(int Position, Produit produit) {
this.produitList.remove(Position);
this.produitList.add(Position, produit);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private EditText mEditText;
public MyViewHolder(View itemView) {
super(itemView);
mEditText = (EditText) itemView.findViewById(R.id.Quantite);
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(mEditText.getTag()!=null){
mEditTextValues.set((int)mEditText.getTag(),charSequence.toString());
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
}
}
There is no output error but no System.out.println() message are shown, and values entered in the EditText aren't kept.