当前位置: 首页 > 知识库问答 >
问题:

我如何通过单击日历视图的日期来控制回收视图?

山森
2023-03-14

我是Android系统的新手,也是英语新手,对不起,我的英语不好...我在学院学过Android课程。我的句子可能是错的,因为它是通过翻译写的。我希望你能以一颗宽厚的心来理解。

// CalendarView myCalenderView;

// TextView schedule1, schedule11;

String a, b, c;

static String data1;
long Now;
Date date;
java.text.SimpleDateFormat mFormat = new SimpleDateFormat("YYYY_MM_dd");

TextView datetext;

Context mcontext;

private String getTime() {
    Now = System.currentTimeMillis();
    date = new Date(Now);
    return mFormat.format(date);
}


private ArrayList<calenderArrayList> mArrayList = new ArrayList<>();

private ArrayList<calenderArrayList> bArrayList = new ArrayList<>();


SharedPreferences preferences;

SharedPreferences.Editor editor;


private static String TAG = "recyclerview_example";

//private ArrayList<calenderArrayList> mArrayList;

//ArrayList 선언

calendarAdapter mAdapter = new calendarAdapter(this, mArrayList);
//mAdapter 선언

//  calendarAdapter bAdapter = new calendarAdapter(this, bArrayList);
private RecyclerView mRecyclerView;


private LinearLayoutManager mLinearLayoutManager;

private int count = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_calender);

    Log.d("스케쥴선택액티비티", "OnCreate()실행");


    SharedPreferences calender_load = getSharedPreferences("calender", MODE_PRIVATE);

    calender_load.getInt("save_size", 0);

    int calender_size = calender_load.getInt("save_size", 0);
    Log.d("시작시b사이즈1", "" + calender_size);
    Log.d("시작시b사이즈1", "" + bArrayList.size());
    Log.d("시작시m사이즈1", "" + mArrayList.size());
    if (calender_size != 0) {

        for (int i = 0; i < calender_size; i++) {


            calenderArrayList calender = new calenderArrayList(calender_load.getString("save_date" + i, ""), calender_load.getString("save_work" + i, ""), calender_load.getString("save_place" + i, ""), calender_load.getBoolean("save_box" + i, false));

            if (calender.number_exam.equals(data1)) {
                Log.d("불러오기값", "" + calender_load.getString("save_date" + i, ""));

                bArrayList.add(calender);
                mArrayList.add(calender);
            }
            mAdapter = new calendarAdapter(this, mArrayList);

            mAdapter.notifyDataSetChanged();
        }
    } else if (calender_size == 0) {

        mAdapter = new calendarAdapter(this, mArrayList);
        mAdapter.notifyDataSetChanged();
    }


    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_exam);
    //recycler_view라는 id를 가진 recycler_view를 mRecyclerView로 지정해준다.

    mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(), 1));


    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));


    //RecyclerView 내에 Item view들의 크기를 측정하고 위치를 지정
    //언제 item view를 재사용해야하는지에 대한 정책을 결정하고 결정.

    //mArrayList = new ArrayList<>();


    mAdapter = new calendarAdapter(this, mArrayList);
    //RecyclerView 내에 보여지는 view 들에 date set을 binding 시켜주는 역할.
    //binding? 데이터 끼리 묵어준다?

    mRecyclerView.setAdapter(mAdapter);
    //mRecyclerView의 Adapter를 mAdapter로 set한다.
    //set? 지정한다. 놓다. 위치하다.


    // myCalenderView = (CalendarView) findViewById(R.id.calendar);


}


@Override
protected void onStart() {
    // TODO Auto-generated method stub
    super.onStart();
    //   Toast.makeText(getApplicationContext(), "onStart()", Toast.LENGTH_SHORT).show();


    Log.d("스케쥴선택액티비티", "OnStart()실행");


    Log.d("스케쥴선택액티비티", "OnResume()실행");


}


@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    //  Toast.makeText(getApplicationContext(), "onResume()", Toast.LENGTH_SHORT).show();

    final CalendarView calendar = (CalendarView) findViewById(R.id.calendar);
    calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {

            data1 = year + "/" + (month + 1) + "/" + dayOfMonth;

            String a = data1 + dayOfMonth;
            Log.d("날짜", "" + a);
            ArrayList<calenderArrayList> dayofMonth = new ArrayList<>();

            Log.d("어떤 이름으로?", "" + dayofMonth);
            if (dayofMonth.size() != 0) {

                SharedPreferences load = getSharedPreferences("" + data1, MODE_PRIVATE);


            }

           /* if(mArrayList.size()!=0) {
                for (int i=0; i<mArrayList.size() ; i++) {
                    SharedPreferences save11 = getSharedPreferences("save", MODE_PRIVATE );
                    SharedPreferences.Editor save_editor = save11.edit();

                    save_editor.putBoolean("save_box"+i+mArrayList.get(i).number_exam, mArrayList.get(i).selected );
                    save_editor.putString("save_date"+i+mArrayList.get(i).number_exam, mArrayList.get(i).number_exam);

                    save_editor.putString("save_work"+i+mArrayList.get(i).number_exam, mArrayList.get(i).content_exam);

                    save_editor.putString("save_place"+i+mArrayList.get(i).number_exam, mArrayList.get(i).content_exam2);

                    save_editor.putInt("save_size"+mArrayList.get(i).number_exam, mArrayList.size());

                    save_editor.commit();


                }
            }
            mArrayList.clear();

            if(mArrayList.size()!=0)
            {
                for (int i =0; i<mArrayList.size(); i++){

                    if(mArrayList.get(i).number_exam.equals(data1)){


                    }

                }
            }*/


            //int a = dayOfMonth;
            Toast.makeText(Calender.this, year + "/" + (month + 1) + "/" + dayOfMonth, Toast.LENGTH_SHORT).show();

            Log.d("리사이클러뷰 실행 전", "실행 전");
            mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view_exam);
            Log.d("리사이클러뷰 실행 후", "실행 후");


            final EditText number_exam = (EditText) view.findViewById(R.id.calender_date);
            final EditText content_exam = (EditText) view.findViewById(R.id.calender_place);
            final EditText content_exam2 = (EditText) view.findViewById(R.id.calender_content);

            //TextView ee = findViewById(R.id.number_exam);

            Log.d("사이즈 측정  실행 전", "실행 전");


            ArrayList<calenderArrayList> calenderArrayLists = new ArrayList<>();
            Log.d("실행11111", mAdapter.getItemCount() + "");

            Log.d("뭐가 들어있나?", "" + mArrayList);
            Log.d("뭐가 들어있나?", "" + mArrayList.size());
            // Log.d("뭐가 들어았나?", ""+mArrayList.get(1).number_exam.toString());
            //  Log.d("뭐가 들어았나?", ""+mArrayList.get(2).number_exam.toString());
            // Log.d("뭐가 들어았나?", ""+mArrayList.get(3).number_exam.toString());

            if (mArrayList.size() != 0) {
                Log.d("if구문 실행됨?", "" + mArrayList.size());

                //1. 일단 뷰 자체를 초기화 해주어야함.
                //2. 초기화 된 뷰에 다시 mArrayList에서 선정 된 정보를 다시 나타내주어야한다.


            /*  for(int i = 0; i<mArrayList.size(); i++){

                    Log.d("얼마?", ""+i);
                    Log.d("for 구문 작동?실행", "여기까진 접속?");
                    if(mArrayList.get(i).number_exam.toString().contains(a)){

                        Log.d("뭐가 들어있나? 실행여부", ""+mArrayList.get(i).number_exam.toString());





                        a = mArrayList.get(i).number_exam;

                        b = mArrayList.get(i).content_exam;

                        c = mArrayList.get(i).content_exam2;


                        //mArrayList.add(mArrayList.get(i));



                    }


                }*/


            }

        }
    });


    Button buttonInsert_exam = (Button) findViewById(R.id.exam_button);
    //button 클릭시 발생하는 이벤트를 나타낸다. 여기서는 입력하기 버튼 클릭시 발생하는 상황.

    buttonInsert_exam.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    AlertDialog.Builder builder = new AlertDialog.Builder(Calender.this);
                    View view = LayoutInflater.from(Calender.this)
                            .inflate(R.layout.activity_calender_edit_box, null, false);
                    builder.setView(view);
                    final Button ButtonSubmit_exam = (Button) view.findViewById(R.id.button_dialog_submit_exam);
                    final EditText number_exam = (EditText) view.findViewById(R.id.calender_date);
                    final EditText content_exam = (EditText) view.findViewById(R.id.calender_place);
                    final EditText content_exam2 = (EditText) view.findViewById(R.id.calender_content);
                    // final EditText editTextKorean = (EditText) view.findViewById(R.id.edittext_dialog_korean);

                    ButtonSubmit_exam.setText("입력하기");

                    number_exam.setText(data1);
                    final AlertDialog dialog = builder.create();

                    dialog.show();

                    //dialog에 나타나는 입력하기 버튼을 눌렀을 때 발생하는 상황
                    ButtonSubmit_exam.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {


                            String strID = number_exam.getText().toString();
                            String strID2 = content_exam.getText().toString();
                            String strID3 = content_exam2.getText().toString();
                            //number와 content view에 입력한 문자열을 strID, strID2에 담는다.

                            //String strKorean = editTextKorean.getText().toString();

                            calenderArrayList dict = new calenderArrayList(strID, strID2, strID3);

                            bArrayList.add(0, dict);
                            mArrayList.add(0, dict); //첫 줄에 삽입
                            //mArrayList.add(dict); //마지막 줄에 삽입
                            mAdapter.notifyDataSetChanged(); //변경된 데이터를 화면에 반영

                            Log.d("b사이즈", "" + bArrayList.size());
                            Log.d("m사이즈", "" + mArrayList.size());

                            dialog.dismiss();
                            //dialog를 종료 시켜준다.
                        }
                    });


                }


            });
}


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    //  Toast.makeText(getApplicationContext(), "onPause()", Toast.LENGTH_SHORT).show();

    SharedPreferences calendersave = getSharedPreferences("calender", MODE_PRIVATE);

    SharedPreferences.Editor calendersaveeditor = calendersave.edit();

    //calenderArrayList calenderArrayList = new calenderArrayList();

    if (bArrayList.size() != 0) {

        for (int i = 0; i < bArrayList.size(); i++) {


            calendersaveeditor.putBoolean("save_box" + i, bArrayList.get(i).selected);

            calendersaveeditor.putString("save_date" + i, bArrayList.get(i).number_exam);

            calendersaveeditor.putString("save_work" + i, bArrayList.get(i).content_exam);

            calendersaveeditor.putString("save_place" + i, bArrayList.get(i).content_exam2);

            calendersaveeditor.putInt("save_size", bArrayList.size());

            calendersaveeditor.commit();


            Log.d("종료시b사이즈", "" + bArrayList.size());
            Log.d("종료시m사이즈", "" + mArrayList.size());

        }

    } else if (bArrayList.size() == 0) {

        calendersaveeditor.clear();
        calendersaveeditor.commit();
    }

    Log.d("스케쥴선택액티비티", "OnPause()실행");
}


@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    //  Toast.makeText(getApplicationContext(), "onStop()", Toast.LENGTH_SHORT).show();





    /*
    * if(mArrayList.size()!=0){

        for(int i = 0; i< mArrayList.size(); i++){



            calendersaveeditor.putBoolean("save_box"+i, mArrayList.get(i).selected);

            calendersaveeditor.putString("save_date"+i, mArrayList.get(i).number_exam);

            calendersaveeditor.putString("save_work"+i, mArrayList.get(i).content_exam);

            calendersaveeditor.putString("save_place"+i, mArrayList.get(i).content_exam2);

            calendersaveeditor.putInt("save_size", mArrayList.size());

            calendersaveeditor.commit();


        }

    }

    else if(mArrayList.size()==0){

        calendersaveeditor.clear();
        calendersaveeditor.commit();
    }
    Log.d("스케쥴선택액티비티", "OnStop()실행");
}

@Override
protected void onRestart() {
    // TODO Auto-generated method stub
    super.onRestart();
    // Toast.makeText(getApplicationContext(), "onRestart()", Toast.LENGTH_SHORT).show();

    Log.d("스케쥴선택액티비티", "OnRestart()실행");
}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    // Toast.makeText(getApplicationContext(), "onDestroy()", Toast.LENGTH_SHORT).show();

    Log.d("스케쥴선택액티비티", "OnDestroy()실행");
}

}

共有1个答案

郑狐若
2023-03-14

首先,您需要在现有的mArrayList中添加新条目,正如我可以从代码中看到的,您正在再次添加所有现有条目,但不是新条目。

之后,您需要使用新的mArrayList再次设置适配器,只有这样,您才能在回收器视图中看到新的数据

 类似资料:
  • 请帮助如何使其工作

  • 我需要在我的Android应用程序中添加日历视图。我需要用户能够通过点击传统日历中的某一天来选择日期,该日历一次显示一个月,并且用户可以通过一些简单的方式在月份之间导航,例如点击按钮或滑动。我对造型不挑剔。此应用程序在智能手机格式的设备上运行。 当我在堆栈溢出中搜索这个问题时,首先会出现这个问题,它说除非您想编写自己的或使用第三方开放源代码,否则没有这样的东西。但这是4年前的事了。我最近看到过更多

  • 一个多星期以来,我一直在stackoverflow和许多其他网站上搜索,想找到一种方法,在包含事件的日历视图中为某些日期着色,但运气不佳。文档中描述的唯一颜色变化是聚焦日期和聚焦周的颜色变化。 我得到了一个带有setOnDateChangeListener的CalendarView,它打开了一个对话框片段,用户可以在其中添加信息并将其保存到数据库中。我想从数据库中提取带有信息的日期,并将这些日期显

  • 我用的是完整的日历。这里我的问题是在点击prev按钮或next按钮点击功能时,如何在月视图、周视图或日视图中找到fullcalendar is的格式。这里正在为next和prev按钮调用自定义代码。因为我想用它激发一些自定义数据。基于fullcalendar视图。 在这里我想找到完整日历的视图,以月、周或日为单位 在这里我想找到完整日历的视图,以月、周或日为单位

  • 如何更改日历视图特定日期的颜色?我正在创建一个简单的应用程序,可以在日历上添加事件,我想突出显示有事件的日期。有人能告诉我怎么做吗?如果不可能,请给我一些其他的建议。

  • 我需要一个Android日历视图像附上的照片。我已经尝试使用这个库https://github.com/alamkanak/android-week-view作为日历视图。这是一个固定的静态事件在日历添加仅。无法在此库日历视图中添加动态事件。有没有人可以推荐任何一个自定义库在Android。 日历视图库代码示例Android-Week-View: 我在这个图书馆所尝试的: 注: 我需要在此日历视图