我正在尝试创建一个应用程序,我想在其中预约,我想将我的数据从活动发送到片段然后进入我的自定义适配器我尝试了所有方法将数据从活动发送到片段之类使用Bundle(SetArguments和getArguments),我试图在我的界面中创建一个接口 主要活动
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);
}
}
在我的主要活动我的片段初始化
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");
Date decr = addDays(today, 1);
yesterday = dateFormat1.format(decr.getTime());
viewPagerAdapter.addFragment(new FragmentTommorrow(), yesterday+"\nTommorrow");
viewPagerAdapter.addFragment(new FragmentSelect(),"select");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
txtdate.setText(todate);
}
请帮帮我们。
答案 0 :(得分:0)
在FragmentToday上添加:
private String place;
private String date;
public passData(String place,String date)
{
this.place=place;
this.date=date;
}
并在您的活动中定义:
private String date,place;
将这个添加到片段onCreate:
的顶部private FragmentToday fragmenttoday;
然后将您的片段添加到寻呼机,如:
private FragmentToday fragmenttoday;
FragmentToday fragmenttoday = new FragmentToday();
viewPagerAdapter.addFragment(fragmenttoday , todate+"\nToday");
现在在sppiner中,当你想要传递数据时添加它:
fragmenttoday.passData(place,date);
现在您可以访问FragmentToday中的地点和日期