1 year ago

#380663

test-img

Riko

Multiple editText in RecyclerView. The values keeps cloning from one to another

I am creating a program for my company, where I need a part where is consignation for money. The money types are in database what I get from an api. So I use RecyclerView with elements. My problem is that when I am changing the value of the first 7 element it'c copying itselfe to the next 7. Does someone have a clue why?

My Activity:

package eu.obsys.staffkeeper.ui.dayclose;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.os.ParcelFormatException;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.util.ArrayList;

import eu.obsys.staffkeeper.FirstPageActivity;
import eu.obsys.staffkeeper.R;
import eu.obsys.staffkeeper.adapter.ConsignLocalRecycleAdapter;
import eu.obsys.staffkeeper.adapter.DeliveryOrderRecycleAdapter;
import eu.obsys.staffkeeper.data.ConsignLocalAsyncResponse;
import eu.obsys.staffkeeper.data.JsonObjectAsyncResponse;
import eu.obsys.staffkeeper.data.Repository;
import eu.obsys.staffkeeper.model.LocalConsignRow;
import eu.obsys.staffkeeper.ui.deliveryorder.DeliveryOrderFragment;

public class LocalConsignActivity extends AppCompatActivity {

    private RecyclerView localConsignRecycleView;
    private ConsignLocalRecycleAdapter consignLocalRecycleAdapter;
    private Button nextToEuroConsign;
    private JSONArray _jsonArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_consign);
        initVariable();
        initLocalConsign();
        initListener();
    }

    public void initVariable() {
        localConsignRecycleView = findViewById(R.id.localConsignRecycleView);
        nextToEuroConsign       = findViewById(R.id.nextToEuroConsign);
        _jsonArray              = new JSONArray();
    }

    public void initLocalConsign() {

        localConsignRecycleView.setHasFixedSize(true);
        localConsignRecycleView.setLayoutManager(new LinearLayoutManager(this));
        ArrayList<LocalConsignRow> localConsigns = new Repository().getLocalConsignRows( this, new ConsignLocalAsyncResponse() {
            @Override
            public void processFinished(ArrayList<LocalConsignRow> localConsignRows) {
                try {
                    if(localConsignRows.size() > 0 ) {
                        consignLocalRecycleAdapter = new ConsignLocalRecycleAdapter(localConsignRows, getApplicationContext());
                        localConsignRecycleView.setAdapter(consignLocalRecycleAdapter);
                        localConsignRecycleView.setVisibility(View.VISIBLE);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void initListener() {
        nextToEuroConsign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                printAllEditTextValues();
                /* int totalLocal   = 0;
                JSONObject jsonObject = new JSONObject();
                int arrayLength = consignLocalRecycleAdapter.getItemCount();
                for (int i = 0; i < arrayLength; i++) {
                    View view = localConsignRecycleView.getChildAt(i);
                    //View view = localConsignRecycleView.getLayoutManager().findViewByPosition(i);
                    try {


                        EditText valueEditText = view.findViewById(R.id.localConsignRowEditText);
                        String valueText = valueEditText.getText().toString();

                        Log.d("SUBTOTAL", valueText);
                        if (valueText.length() > 0 && !valueText.equals("0")) {
                            try {
                                int valueOfEditText = Integer.parseInt(valueText);
                                LocalConsignRow localConsignRow = consignLocalRecycleAdapter.getItem(i);
                                totalLocal += (valueOfEditText * localConsignRow.getValue_local());
                                try {
                                    jsonObject.put(localConsignRow.getId() + "", valueOfEditText);

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            }
                            //Repository
                        }
                    } catch (NullPointerException e) {
                        e.printStackTrace();
                    }
                } */
                //Log.d("TOTAL", totalLocal + "");
            }
        });
    }

    public void printAllEditTextValues() {
        int childCount = localConsignRecycleView.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View view       = localConsignRecycleView.getChildAt(i);
            EditText nameEditText = (EditText) view.findViewById(R.id.localConsignRowEditText);
            String name     = nameEditText.getText().toString();
            Log.d("EditText", name);
        }
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(LocalConsignActivity.this, FirstPageActivity.class);
        startActivity(intent);
        finish();
    }

}

my Adapter:

package eu.obsys.staffkeeper.adapter;

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.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Objects;

import eu.obsys.staffkeeper.R;
import eu.obsys.staffkeeper.model.LocalConsignRow;

public class ConsignLocalRecycleAdapter extends RecyclerView.Adapter<ConsignLocalRecycleAdapter.ViewHolder> {

    private ArrayList<LocalConsignRow> localConsigns;
    private Context context;

    public ConsignLocalRecycleAdapter(ArrayList<LocalConsignRow> localConsigns, Context context) {
        this.localConsigns  = localConsigns;
        this.context        = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.local_consign_row, parent, false);

        return new ViewHolder(view, new LocalConsignEditTextListener());
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        LocalConsignRow localConsignRow = localConsigns.get(position);
        holder.textInputLayout.setText(localConsignRow.getLabel());
        holder.textInputEditText.setHint("0");
    }

    @Override
    public int getItemCount() {
        return Objects.requireNonNull(localConsigns.size());
    }

    public LocalConsignRow getItem(int position) {
        return localConsigns.get(position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public EditText textInputEditText;
        public TextView textInputLayout;
        public LocalConsignEditTextListener localConsignEditTextListener;

        public ViewHolder(@NonNull View itemView, LocalConsignEditTextListener _localConsignEditTextListener) {
            super(itemView);
            textInputEditText                 = itemView.findViewById(R.id.localConsignRowEditText);
            localConsignEditTextListener      = _localConsignEditTextListener;
            textInputEditText.addTextChangedListener(localConsignEditTextListener);
            textInputLayout                   = itemView.findViewById(R.id.localConsignRowLabel);
        }
    }

    private class LocalConsignEditTextListener implements TextWatcher {
        private int position;
        public void updatePosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            // no op
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            int amount = 0;
            try {
                amount = Integer.parseInt(charSequence.toString());
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            localConsigns.get(position).setAmount(amount);
        }

        @Override
        public void afterTextChanged(Editable editable) {
            // no op
        }

    }
}

android

android-recyclerview

android-edittext

0 Answers

Your Answer

Accepted video resources