androidandroid-recyclerviewonitemclick

Recycle view get item value base on item clicked


I just create a dialog with a recycle view on it. When i click the item it supposed to set the edit text base on what i click. But it doesn't, it shown the first value in the recycle view. could someone help me to get the value base on what i click. I just create a dialog with a recycle view on it. When i click the item it supposed to set the edit text base on what i click. But it doesn't, it shown the first value in the recycle view. could someone help me to get the value base on what i click

this is my list item

@SuppressLint("ResourceType")
@OnClick(R.id.button_choose)
void chooseLOV() {

    AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.lov_kodepos, null);
    dialogBuilder.setView(dialogView);

    final RecyclerView recyclerView = (RecyclerView) dialogView.findViewById(R.id.rv_lov_kodepos);
    final EditText search  = (EditText) dialogView.findViewById(R.id.editText_lov_search);
    search.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) {
            ArrayList<KodePos> kodePos = new ArrayList<>();
            if (tempData!=null){
                for (KodePos kodePosSatu: data ){
                    String dat = s.toString().toLowerCase();
                    if (kodePosSatu.getPosCamat().toLowerCase().contains(dat)) {
                        kodePos.add(kodePosSatu);
                    }
                }
                tempData = kodePos;
                recyclerView.setAdapter(new KodePosAdapter(tempData));
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });


    API.getKodePos().enqueue(new Callback<ArrayList<KodePos>>() {
        @Override
        public void onResponse(Call<ArrayList<KodePos>> call, Response<ArrayList<KodePos>> response) {
            if (response.code()== 200){
                Log.i("bella", "onResponse: "+response);

                data = response.body();
                tempData = data;
                recyclerView.setHasFixedSize(true);
                recyclerView.addItemDecoration(new DividerItemDecoration(AddCustomerActivity.this, DividerItemDecoration.VERTICAL));
                recyclerView.setLayoutManager(new LinearLayoutManager(AddCustomerActivity.this));
                recyclerView.setAdapter(new KodePosAdapter(data));
            }
        }

        @Override
        public void onFailure(Call<ArrayList<KodePos>> call, Throwable t) {
            Toast.makeText(AddCustomerActivity.this, "Failed", Toast.LENGTH_SHORT).show();

        }
    });

    final AlertDialog alertDialog = dialogBuilder.create();
    alertDialog.show();

    recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(AddCustomerActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            et_kodepos.setText(data.get(position).getPosKode());
            et_kota.setText(data.get(position).getPosKota());
            et_provinsi.setText(data.get(position).getPosProp());
            et_kecamatan.setText(data.get(position).getPosCamat());
            et_kelurahan.setText(data.get(position).getPosLurah());

            alertDialog.dismiss();
        }
    }));

}

Solution

  •  recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(AddCustomerActivity.this, new RecyclerItemClickListener.OnItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
    
    
            et_kodepos.setText(data.get(position).getPosKode());
            et_kota.setText(data.get(position).getPosKota());
            et_provinsi.setText(data.get(position).getPosProp());
            et_kecamatan.setText(data.get(position).getPosCamat());
            et_kelurahan.setText(data.get(position).getPosLurah());
    
        }
    
    }));
    

    In your code, every time, you are assigning the 0th (first item) element to edit text.