3

I have onItemClickListener for the ListView that opens DatePicker dialog when an item is clicked:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        showDatePickerDialog(view);
    }
});

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
}

After date in DatePicker gets set, I have onDateSet method which I want to use to update database and set date for item that was clicked in ListView:

public void onDateSet(DatePicker view, int year, int month, int day) {
    ...
    dataSource.updateDate(adapter.getItemId(position), month + "/" + day + "/" + year);
    ...
} 

How can I pass ListView position of the item that was clicked to onDateSet so that my database helper knows which record to update?


EDIT: Here's the DatePickerFragment class:

public static class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        ...
        dataSource.updateDate(adapter.getItemId(position), month + "/" + day + "/" + year);
        ...
    }
}

Thanks!

4

2 回答 2

1

发送positionDatePickerFragment- 我想这是你自己的课程。通过参数发送此参数(因为这是在片段重新创建中保护代码的最佳实践):

public class DatePickerFragment extends Fragment {

    private DatePickerListener datePickerListener;
    private int listPosition = -1;
    private static final String POSITION_ARG_KEY = "POSITION_ARG_KEY";

    public static DatePickerFragment newInstance(int position) {
        DatePickerFragment fragment = new DatePickerFragment();
        Bundle args = new Bundle();
        args.putInt(POSITION_ARG_KEY, position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        if (args != null) {
            listPosition = args.getInt(POSITION_ARG_KEY);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof DatePickerListener) {
            datePickerListener = (DatePickerListener) activity;
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        datePickerListener = null;
    }

    public void methodThatCallsActivity() {
        if(datePickerListener) {
            datePickerListener.onDateSet(datePicker, year, month, day, this.listPosition);
        }
    }

    // / the rest of the code

    public static interface DatePickerListener {
        public void onDateSet(DatePicker view, int year, int month, int day, int listPosition);
    }
}

然后在显示片段的活动中,(它必须实现DatePickerListener):

A.发送列表位置:

public void showDatePickerDialog(int listPosition) {
    DialogFragment newFragment = DatePickerFragment.newInstance(listPosition);
    newFragment.show(getFragmentManager(), "datePicker");
}

B.从列表项单击调用此方法:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        showDatePickerDialog(position);
    }
});

C. 您在onDateSet实施中的列表位置:

public void onDateSet(DatePicker view, int year, int month, int day, int listPosition) {
    // your relevant code
}

简而言之:通过参数和参数将列表位置从列表视图发送到片段,然后从那里发送到父活动。

于 2013-09-22T20:56:27.930 回答
0

通过执行以下操作解决了它:

使它DatePickerFragment成为自己的类(不嵌套在MainActivity类中):

public class DatePickerFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), (MainActivity)getActivity(), year, month, day);
    }
}

这就是我的MainActivity班级最终的样子:

public class MainActivity extends ListActivity implements DatePickerDialog.OnDateSetListener {
    private long listId = -1;
    private int listPosition = -1;
    private String date;
    ...

    protected void onCreate(Bundle savedInstanceState) {
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listId = id;
                listPosition = position;
                showDatePickerDialog(view);
    ...

    // Show DatePicker when clicking on contact in ListView
    public void showDatePickerDialog(View view) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }

    // Update database and ListView adapter with selected date
    public void onDateSet(DatePicker view, int year, int month, int day) {
        date = (month + 1) + " " + day + ", " + year;
        ...
        dataSource.updateDate(listId, date);
        adapter.setDate(listPosition, date);
        ...
        adapter.notifyDataSetChanged();
        dataSource.close();
    }

旁注:按下 DatePicker 的取消按钮(和 Android 后退按钮)仍会将默认日期保存到数据库中并更新适配器。这是解决此问题的帖子:https ://stackoverflow.com/a/15289569/1261256

于 2013-09-23T00:41:02.623 回答