通过接口发送后无法访问ArrayList项

时间:2013-09-19 13:56:18

标签: java android interface android-fragments

问题是我无法从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

}

1 个答案:

答案 0 :(得分:4)

dataSet中使用通用类型而非raw typeArrayList dataSet相当于ArrayList<Object> dataSet

此外,使用List而不是ArrayList

来创建界面代码
public void onUserTimeChoice(List<Integer> dataSet) {