问题是我无法从Integer
检索从ArrayList<Integer>
通过接口发送到FragmentActivity的TimePickerFragment
值。当我尝试获取保存的值时(请参阅下面的FragmentActivity),我得到了&#34;不兼容的类型。必需:java.lang.Integer,Found:java.lang.Object&#34;。
TimePickerFragment:
public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
Listener mListener;
// declaration of the interface
public interface Listener {
public void onUserTimeChoice(ArrayList dataSet);
}
// registration of the listener
public void registerListener(Listener listener) {
mListener = listener;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the user
ArrayList<Integer> mAList = new ArrayList();
mAList.add(hourOfDay);
mAList.add(minute);
mListener.onUserTimeChoice(mAList);
}
}
FragmentActivity(摘录):
@Override
public void onUserTimeChoice(ArrayList dataSet) {
Integer hour = dataSet.get(0); // <--- incompatible types
Integer minute = dataSet.get(1); // <--- incompatible types
}
答案 0 :(得分:4)
在dataSet
中使用通用类型而非raw type。 ArrayList dataSet
相当于ArrayList<Object> dataSet
此外,使用List
而不是ArrayList
public void onUserTimeChoice(List<Integer> dataSet) {