How to update activity content automatically when an item is selected in bottom sheet

54 Views Asked by At

I am trying to make an unit convertor when i change unit the result is not gating updated as showen in image

enter image description hereenter image description hereenter image description here

When i select unit any unit from menu the conversion must be visable on textview

here is code:-

 
public class UnitMenu extends BottomSheetDialogFragment {
    ListView unitList;
    String set;
    String[] units;
    ExtendedFloatingActionButton button;
    UnitMenu(String[] units, String set, ExtendedFloatingActionButton button) {
        this.units = units;
        this.set = set;
        this.button = button;
    }

    @NonNull
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.area_select_unit, container, true);
        unitList = view.findViewById(R.id.unitList);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, units);
        unitList.setAdapter(adapter);
        unitList.setOnItemClickListener((parent, v, position, id) -> {
            if (set.equals("Select Unit From"))
                ConverterActivity.setSelectedFromUnit(units[position]);
            else ConverterActivity.setSelectedToUnit(units[position]);

            button.setText(units[position]);
            dismiss();
        });

        return view;
    }
}

1

There are 1 best solutions below

0
Hoa.Tran On

Please add your code when call show your dialog to get better answer. normally i would use callback like that:

class UnitDialog : BottomSheetDialogFragment(){
  private lateinit var onChangeUnit: (String) -> Unit
  companion object {
    fun newInstance(onChangeUnit: (String) -> Unit){
      val dialog = UnitDialog()
      dialog.onChangeUnit = onChangeUnit
      return dialog
    }    
  }
  
  fun onItemClick(unit: String){
    onChangeUnit.invock(unit)
  }  
} 

class MainActivity: AppCompatActivity(){
  fun changeUnit(){
  UnitDialog.newInstance(){ newUnit->
    // call logic convert number in here
  }.show(supportFragmentManager,"TAG_UNIT_DIALOG")
}