2022-10-16

Android-RecyclerView get text and set text in the same row

I am loading editText on click of fab button in recyclerview. I want to setText on TextView in the same row of recyclerview entered in editText. Currently the text is setting from 2nd textview and previous values from edittext are being set. Basically if I type "One" in 1st editText "One" should be set to textview in the same row, "two" entered in 2nd editText "two" in the 2nd TextView.on click of Fab it should add item in the recyclerview and which will have edittext and textview. Any change in edittext should reflect in textview What changes need to be done? Im using model class MainActivity.java

public class MainActivity extends AppCompatActivity {
FloatingActionButton fab;
RecyclerView recyclerView;
ArrayList<Product> productsList;
CustomAdapter adapter;
EditText editText;
String edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.rec);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    productsList = new ArrayList<>();

    adapter = new CustomAdapter(MainActivity.this,productsList);
    recyclerView.setAdapter(adapter);

    fab = findViewById(R.id.fabBtn);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            editText = new EditText(MainActivity.this);
            edit = editText.getText().toString();
            productsList.add(new Product(0, edit));
            adapter.notifyItemInserted(productsList.size()-1);

            }
    });

}

}

CustomAdapter.java

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
public static ArrayList<Product> productsList,filteredList;
private Context context;

public CustomAdapter(Context context,ArrayList<Product> productsList) {
    this.context = context;
    this.productsList = productsList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    // Create a new view, which defines the UI of the list item
    View view = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.layout_rv, viewGroup, false);

    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
    Product product = productsList.get(position);
    viewHolder.idtxt.setText(String.valueOf(product.getId()));
    viewHolder.textView.setText(productsList.get(position).getName());
   // viewHolder.txtsave.setText(product.getName());

}

@Override
public int getItemCount() {
    return productsList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
      TextView idtxt;
      EditText textView;
      TextView txtsave;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        idtxt = itemView.findViewById(R.id.idttxt);
        textView = itemView.findViewById(R.id.textView);
        txtsave = itemView.findViewById(R.id.txtsave);
        textView.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) {
                productsList.get(getAdapterPosition()).setName(textView.getText().toString());

            }

            @Override
            public void afterTextChanged(Editable editable) {
                //viewHolder.textView.getText().toString();

            }
        });


        for (int i = 0; i < productsList.size(); i++){

            txtsave.setText(txtsave.getText() + " " + productsList.get(i).getName() +System.getProperty("line.separator"));


        }

    }

}

}



No comments:

Post a Comment