简体   繁体   中英

Send Data from Activity to Dialog which in Custom adapter and my custom adapter is in fragment

I am trying to create an application in which I want to make an appointment for that I want to send my data from activity to a fragment and then into my custom adapter i tried all the method to send data from activity to fragment like using Bundle (SetArguments and getArguments) and I tried to create an Interface in my Main Activity

Make Appointment.java

public class MakeAppointment extends AppCompatActivity {

        
    FragmentToday fragmentToday;
    FragmentTommorrow fragmentTommorrow;
    FragmentSelect fragmentSelect;
    ViewPager viewPager;
    TabLayout tabLayout;
    TextView txtdate, txtTitle, txtTime, txtlocation;

public interface FragmentCommunicator {
    public void passData(String place,String date);
    }

    FragmentCommunicator fragmentCommunicator;
            
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_make_appointment);
    
        txtTitle = (TextView) findViewById(R.id.title);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        txtdate = (TextView) findViewById(R.id.txtDate);
        txtlocation = (TextView) findViewById(R.id.txtLocation);

        spinner = (Spinner) findViewById(R.id.spinner);

        // Spinner Drop down elements
                
        List<String> categories = new ArrayList<String>();
        categories.add("Select Location");
        categories.add("Pashan");
        categories.add("Balaji Nagar");
        categories.add("Mumbai");
                
fragmentCommunicator = (FragmentCommunicator) this ; // GOT ERROR ClassCastException
        
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        strPlace =  spinner.getSelectedItem().toString();
        txtlocation.setText(strPlace);

        String place = spinner.getSelectedItem().toString().trim();
                        String date  = txtdate.getText().toString();
                        
                        fragmentCommunicator.passData(place,date);
                
                    }

@Override
public void onNothingSelected(AdapterView<?> parent) {
 }
});

FragmentToday

public class FragmentToday extends Fragment{
            
    TypedArray alltimeslotes;
    Map<String, List<String>> time;
    List<String> slots;
    ExpandableListView expandableListView;
    Custom_Expandable_Listview_Adapter expandableListviewAdapter;

            ArrayList<String> morning = new ArrayList<>();
            ArrayList<String> evening = new ArrayList<>();
            String PLACE,DATE;

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

            View rootView = inflater.inflate(R.layout.fragmenttoday, null);
                    expandableListView = (ExpandableListView) rootView.findViewById(R.id.expandlist);

            filldata();

                    ((MakeAppointment) getActivity()).passVal(new MakeAppointment.FragmentCommunicator() {
                        @Override
                        public void passData(String place, String date) {
                            PLACE = place ;
                            DATE = date ;
                        }
                    });

                    expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                    @Override
                    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                        Toast.makeText(getActivity(), slots.get(groupPosition) + ":" + time.get(slots.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show();
                        String settime = slots.get(groupPosition) + ":" + time.get(slots.get(groupPosition)).get(childPosition);
                        return false;
                    }
                });
                    return rootView;

            }
                public void filldata () {

                    slots = new ArrayList<>();
                    time = new HashMap<>();

                    slots.add("Morning");
                    slots.add("Evening");


                    alltimeslotes = getResources().obtainTypedArray(R.array.time_slotes);
                    for (int i = 0; i < alltimeslotes.length(); i++) {
                        morning.add(alltimeslotes.getString(i));
                        time.put(slots.get(0), morning);
                    }

                        alltimeslotes = getResources().obtainTypedArray(R.array.time_slotes_evening);
                        for (int i = 0; i < alltimeslotes.length(); i++) {
                            evening.add(alltimeslotes.getString(i));
                            time.put(slots.get(1), evening);
                        }
                    expandableListviewAdapter = new Custom_Expandable_Listview_Adapter(getActivity(),getContext(),slots,time,PLACE, DATE);
                    expandableListView.setAdapter(expandableListviewAdapter);

                }
            }

In my Main Activity my Fragment Initialization

         public void setupViewPager() {

                viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
                Date today = new Date();
                Calendar cal = Calendar.getInstance();
                DateFormat dateFormat1 = new SimpleDateFormat("dd/MMM/yyyy", Locale.getDefault());
                todate = dateFormat1.format(cal.getTime());

                viewPagerAdapter.addFragment(new FragmentToday(), todate+"\nToday"); // `new FragmentToday()` should be inside `FragmentPagerAdapter.getItem()`
           
                    Date decr = addDays(today, 1);
                     yesterday = dateFormat1.format(decr.getTime());
                     viewPagerAdapter.addFragment(new FragmentTommorrow(), yesterday+"\nTommorrow"); // `new FragmentTommorrow()` should be inside `FragmentPagerAdapter.getItem()`

                viewPagerAdapter.addFragment(new FragmentSelect(),"select"); // `new FragmentSelect()` should be inside `FragmentPagerAdapter.getItem()`

    
                viewPager.setAdapter(viewPagerAdapter);
                tabLayout.setupWithViewPager(viewPager);
    
               txtdate.setText(todate);
            }

Please Help me guys.

add this on your FragmentToday:

private String place;
private String date;
public passData(String place,String date)
{
  this.place=place;
  this.date=date;
}

and define this on your activity:

private String date,place;

add this on your top of fragment onCreate:

private FragmentToday fragmenttoday;

then add your fragment to pager like thie :

private FragmentToday fragmenttoday;
  FragmentToday fragmenttoday = new FragmentToday(); // `new FragmentToday()` should be inside `FragmentPagerAdapter.getItem()`
   viewPagerAdapter.addFragment(fragmenttoday , todate+"\nToday"); // `new FragmentToday()` should be inside `FragmentPagerAdapter.getItem()`

now in sppiner when you want pass data add it :

fragmenttoday.passData(place,date);

now you access to place and date in FragmentToday

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM