【问题标题】:how can to use Persian Calander in RecyclerViewAdapter如何在 RecyclerView 适配器中使用波斯日历
【发布时间】:2018-03-21 09:36:42
【问题描述】:

我使用特定的日期库(https://github.com/mohamad-amin/PersianMaterialDateTimePicker)

现在我想在 Recycler View 的按钮中使用它。

现在我的问题是我必须在库代码中定义两个选项:

1 - 我应该参考活动。

2 - 我需要使用 getSupport FragmentManager()。

但是这个页面已经被 Recycler View.Adapter 扩展了,我不知道如何在这个页面上调用这两个选项。

RecyclerViewAdapter 页面:

                        PersianCalendar persianCalendar = new PersianCalendar();

                    DatePickerDialog datePickerDialog = DatePickerDialog.newInstance( StudentList.this ,
                            persianCalendar.getPersianYear() ,
                            persianCalendar.getPersianMonth() ,
                            persianCalendar.getPersianDay());




                    datePickerDialog.show( getSupportFragmentManager() , "datePickerDialog" );

在这些图片中你可以很容易地理解我:

enter image description here

enter image description here

enter image description here

【问题讨论】:

  • 请将代码添加为文本,而不是图像/屏幕截图。
  • 现在可以看到代码
  • 您应该从您的适配器中移出该代码,这在 SOLID 原则中是错误的。使用委托模式解决您的问题。

标签: android android-studio android-fragments android-recyclerview android-datepicker


【解决方案1】:

你的activity必须实现TimePickerDialog.OnTimeSetListener并实现abstact方法onTimeSet

    public class StudentList extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {


 @Override
    public void onTimeSet(TimePickerDialog view, int hourOfDay, int minute, int second) {

    }
}

onClick内部

  DatePickerDialog datePickerDialog = DatePickerDialog.newInstance((((StudentList)view.getContext()),  persianCalendar.getPersianYear(),
                        persianCalendar.getPersianMonth(),
                        persianCalendar.getPersianDay());

或者:

DatePickerDialog datePickerDialog =DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

            }
        },  persianCalendar.getPersianYear(),
            persianCalendar.getPersianMonth(),
            persianCalendar.getPersianDay());

【讨论】:

  • 你说的方式没有问题,但我的第一个问题是我不能调用StudentList Activity @B.M
  • ((StudentList)view.getContext())替换StudentList.this
  • 你说的方法没问题,但是我的日期库可以用:DatePickerDialog.newInstance @B.M
  • 你使用的是哪个库?
  • 这种方式是对的 但是我现在想在 RecyclerView 的 AlertDialog 的文本视图中显示日期 我该如何在它们之间进行通信(StudentList)?
【解决方案2】:

您可以尝试在创建适配器实例时传递 Activity 的上下文:

//Code in your Activity
MyAdapter myAdapter = new MyAdapter(this, ...);

您的适配器:

private Context context;

    public MyAdapter(Context context, ...){
    this.context = context;
    //Rest of the constructor...
    }

通过这样做,您可以添加这样做:

datePickerDialog.show(context.getSupportFragmentManager(), "datePickerDialog");

【讨论】:

  • 我按照你说的做了,但是问题还没有解决@XabinCoffee
【解决方案3】:

您只需要在构造函数中传递一个活动上下文。请务必从活动中调用 new Adapter(this,...) 并从 Fragment 中调用 new Adapter(getActivity(),...)。

private Context context;

@Override
public void onClick(View v) {
FragmentManager manager = ((Activity) context).getFragmentManager();
FragmentManager manager = ((AppCompatActivity)context).getSupportFragmentManager();
}

试试这个

【讨论】:

  • 你说的方式没有问题,但我的第一个问题是我不能调用StudentList Activity @Android Geek
  • 能否分享完整的代码学生名单活动
猜你喜欢
  • 2022-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多