Android:GridView中的Datepicker

时间:2017-01-16 22:51:36

标签: android gridview datepicker

我正在构建一个Android应用程序,我遇到DatePicker的问题。 我的实现(我希望它的方式)如下: GridView(带有图片)和选中的项目会启动新的意图。 在特定项目上,我首先要启动DatePickerDatePicker选项,然后启动新意图。问题是,DatePicker或者onCreate()将在以后启动,或者它根本不会启动。 如果有人可以提供帮助,我会感激(关于代码,或者如果他能为我提供另一个更合适的实现解决方案)。

所以我在gridview.setAdapter(myAdapter); calendarDate = Calendar.getInstance(); caseCalendar = Calendar.getInstance(); gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { if(position == 7){ onCustomDatePicker(); if(caseCalendar.get(Calendar.DAY_OF_YEAR) < calendarDate.get(Calendar.DAY_OF_YEAR)){ Toast.makeText(getBaseContext(), "Πρέπει να επιλέξεις μεταγενέστερη ημερομηνία", Toast.LENGTH_SHORT).show(); onCustomDatePicker(); } } else{ caseCalendar = calendarDate; } Intent intent = new Intent(getBaseContext(), Customize.class); ViewHolder holder = (ViewHolder) v.getTag(); DateWithImage temp = (DateWithImage) holder.image.getTag(); intent.putExtra("image",temp.getImageID()); intent.putExtra("option",position); intent.putExtra("info",temp.getInfo()); intent.putExtra("calendar", calendarDate); intent.putExtra("caseCalendar", caseCalendar); startActivity(intent); Toast.makeText(getBaseContext(), "" + position, Toast.LENGTH_SHORT).show(); } });

中有这个代码
DatePicker

以及private void onCustomDatePicker(){ new DatePickerDialog(this, d, caseCalendar.get(Calendar.YEAR),caseCalendar.get(Calendar.MONTH),caseCalendar.get(Calendar.DAY_OF_MONTH)).show(); } DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { caseCalendar.set(Calendar.YEAR, year); caseCalendar.set(Calendar.MONTH, monthOfYear); caseCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); } }; 的代码:

caseCalendar

我正在尝试将新约会设置为calendarDate并将其与Name3(包含当前日期)进行比较。

2 个答案:

答案 0 :(得分:0)

每次要点击图片时,您都在呼叫startActivity(intent);。对于特定项目,您需要这样写:

if(position == 7){
                    onCustomDatePicker();
                    if(caseCalendar.get(Calendar.DAY_OF_YEAR) < calendarDate.get(Calendar.DAY_OF_YEAR)){
                        Toast.makeText(getBaseContext(), "Πρέπει να επιλέξεις μεταγενέστερη ημερομηνία", Toast.LENGTH_SHORT).show();
                        onCustomDatePicker();
                    }
                }
                else{
                    caseCalendar = calendarDate;
Intent intent = new Intent(getBaseContext(), Customize.class);
                ViewHolder holder = (ViewHolder) v.getTag();
                DateWithImage temp = (DateWithImage) holder.image.getTag();
                intent.putExtra("image",temp.getImageID());
                intent.putExtra("option",position);
                intent.putExtra("info",temp.getInfo());
                intent.putExtra("calendar", calendarDate);
                intent.putExtra("caseCalendar", caseCalendar);
                startActivity(intent);
                Toast.makeText(getBaseContext(), "" + position,      Toast.LENGTH_SHORT).show();
                }

首先,您只需要调用datepicker对话框,然后在日期设置中启动新活动。现在,您只需点击图片即可开始活动。

答案 1 :(得分:0)

好的,所以我终于找到了解决方案,以防有人遇到这个问题(datePicker,在gridview.SetOnClickListener中实现) -

说实话,虽然它有效但如果有人能告诉我,如果这是一个好的编程实践,我将不胜感激....:

我使用了两个日历(一个用于当天,一个用于所选日历 - 显然如果我用getInstance()初始化caseCalendar,它与calendarDate ?????获得相同的值):

calendarDate = Calendar.getInstance();
        caseCalendar = new GregorianCalendar(calendarDate.get(Calendar.YEAR), calendarDate.get(Calendar.MONTH) ,calendarDate.get(Calendar.DAY_OF_MONTH)-1);

在onCreate()中:

gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                final ViewHolder holder = (ViewHolder) v.getTag();
                final int mposition = position;
                if(mposition == 7){//in my case the desired position to trigger the dayPicker
                    if(caseCalendar.compareTo(calendarDate)< 0){
                        Toast.makeText(getBaseContext(), "", Toast.LENGTH_LONG).show();
                        onCustomDatePicker(holder,mposition);
                     }
                }
                else{
                    caseCalendar = calendarDate;
                    startNewActivity(holder,mposition);
                }
            }
        });

在单个特定选项上,我想在新活动之前获取datePicker,但在其他选项上,我想立即启动新活动。所以我使用了这两种方法:

private void onCustomDatePicker(final ViewHolder holder, final int mposition){
        new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                caseCalendar.set(Calendar.YEAR, year);
                caseCalendar.set(Calendar.MONTH, monthOfYear);
                caseCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                startNewActivity(holder, mposition);
            }
        }, caseCalendar.get(Calendar.YEAR),caseCalendar.get(Calendar.MONTH),caseCalendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    private void startNewActivity(final ViewHolder holder, final int mposition){
        Intent intent = new Intent(getBaseContext(), Customize.class);
        DateWithImage temp = (DateWithImage) holder.image.getTag();
        intent.putExtra("image",temp.getImageID());
        intent.putExtra("option",mposition);
        intent.putExtra("info",temp.getInfo());
        intent.putExtra("calendar", calendarDate);
        intent.putExtra("caseCalendar", caseCalendar);
        startActivity(intent);
        Toast.makeText(getBaseContext(), "" + mposition, Toast.LENGTH_SHORT).show();
    }