0

this is my first question here so please ignore my mistake. my question is about bottom sheet fragment dialog.

I am trying to show bottom sheet dialog on button click when bottom sheet dialog appear its show list of different companies name. I have added the name myself. but I want the user to add another name if they want on the button click. everything is working fine.

I am getting the name of the company from edit text from dialog box. but when I dismiss the bottom sheet dialog fragment I lost the added name from the user through alert dialog box.

I don't understand what is happening and how can I sort the issue. please follow my code and let me know what is the best approach I can follow. thanks very much

public class Item
 {
 public String companyName;

 public Item(String companyName) {
this.companyName = companyName;
 }

public String getCompanyName() {
return companyName;
}

public void setCompanyName(String companyName) {
this.companyName = companyName;}}

this is my adapter class

 public class CustomAdapter_1 extends RecyclerView.Adapter  <CustomAdapter_1.myViewHolder>


{
   List<Item> customAdapters;
   Context context;

public CustomAdapter_1(Context context, List<Item> customAdapters) {
this.customAdapters = customAdapters;
this.context = context;

}


@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from ( parent.getContext () ).inflate (   R.layout.custom_adapter_1, parent, false );
myViewHolder myViewHolder = new myViewHolder ( v );
return myViewHolder;
}

@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
holder.radioButton.setText ( customAdapters.get ( position ).getCompanyName () );

}

@Override
public int getItemCount() {
return customAdapters.size ();
 }


 public class myViewHolder extends RecyclerView.ViewHolder {
 RadioButton radioButton;
 TextView textView;

 public myViewHolder(@NonNull View itemView) {
    super ( itemView );
    radioButton = (RadioButton) itemView.findViewById ( R.id.rdbtn1 );


   }
   }

   public void AddItem(Item item, int position) {
   customAdapters.add ( position, item );

   }}

and below is my fragment class

public class CustomFragment_1 extends BottomSheetDialogFragment {

private RecyclerView recyclerView;
private CustomAdapter_1 mAdapter;
ArrayList<Item> listOfItem = new ArrayList<> ();


String cName;
Button btn;
FlatDialog flatDialog;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

{
    // Inflate the layout for this fragment
    View view = inflater.inflate ( R.layout.fragment_custom_1, container, false );


    recyclerView = (RecyclerView) view.findViewById ( R.id.customRV );
    btn = (Button) view.findViewById ( R.id.button1 );

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager ( getContext ()     );
    recyclerView.setLayoutManager ( linearLayoutManager );


    mAdapter = new CustomAdapter_1 ( getContext (), listOfItem );
    recyclerView.setAdapter ( mAdapter );

    listOfItem.add ( new Item ( "company 1" ) );
    listOfItem.add ( new Item ( "company 2" ) );
    listOfItem.add ( new Item ( "company 3" ) );


    btn.setOnClickListener ( new View.OnClickListener () {
        @Override
        public void onClick(View view)
        {
            getList ();


        }
    } );
    return view;
}
public void getList()
{
    flatDialog = new FlatDialog ( getContext () );
    flatDialog.setTitle ( "Company Name" )
            .setSubtitle ( "Please add the name of the company" )
            .setFirstTextFieldHint ( "Company Name" )
            .setFirstButtonText ( "ADD" )
            .setSecondButtonText ( "CANCEL" )
            .withFirstButtonListner ( new View.OnClickListener () {
                @Override
                public void onClick(View view) {
                    
                    listOfItem.add ( new Item ( flatDialog.getFirstTextField ()) ) ;
                    mAdapter.notifyDataSetChanged ();
                    flatDialog.dismiss ();

                }
            } )
            .withSecondButtonListner ( new View.OnClickListener () {
                @Override
                public void onClick(View view) {
                    flatDialog.dismiss ();
                }
            } )
            .show ();

}
}

0 Answers0