1

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();
        }
    }));

}
  • create your own `interface` here. register it inside your `MainActivity` or `Fragment` where is your recycler view. set it inside adapter of recycler view and on clicking of any item of recycler view call that interface method with position of item. – Radhey Mar 23 '18 at 08:03
  • 1
    use `position` AS `et_kodepos.setText(data.get(position).getPosKode());` – ADM Mar 23 '18 at 08:03
  • THANK YOU so much ! its solved my problem! @ADM – Lisda Wijayanti Mar 23 '18 at 08:10
  • @ADM sir, can i ask you some question. How to make the dialog closed after I clicked the item ? – Lisda Wijayanti Mar 26 '18 at 03:54
  • To dismiss Dialog See https://stackoverflow.com/a/49349852/4168607 . – ADM Mar 26 '18 at 04:04
  • thanks! it works @ADM – Lisda Wijayanti Mar 26 '18 at 04:49
  • Sir, once again i want to ask you how i get position after i search an item then when i click it , it will set in edit text ? @ADM – Lisda Wijayanti Mar 26 '18 at 08:01
  • @ADM or may i have your email? – Lisda Wijayanti Mar 26 '18 at 08:05
  • These little problems you are facing because your code is not structurized and you did not get understand the concept. Read documentation and working of elements you are using and Look for tutorials for each step and combine them. `position` is an parameter of `onItemClick` which you can use . And to get Item from `position` get it from `filteredList` not from `completeList`. It will be clear if you make a separate class for `Dialog` and use `interface` for callback See [How to Define Callbacks in Android](https://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android). – ADM Mar 26 '18 at 08:07

1 Answers1

0
 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.

Jithu S
  • 76
  • 3