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

时间:2019-03-18 16:16:42

标签: android arraylist android-recyclerview onclicklistener calendarview

我是Android新手,也是英语新手,抱歉我的英语不好... 我已经在该学院学习了android课程。 我的句子可能是错误的,因为它是通过翻译写的。希望您能宽容地理解。

这就是我想要的:

如果我在日历视图中单击15天, 希望Recyclerview中仅显示第15天的信息。如果我在calenderview中单击另一个日期(例如20天),希望第15个项目消失并且仅显示第20个项目视图。

这就是我想要做的。 当我单击日历视图中的日期时,我想查看与该日期相对应的项目视图。

  • 打包com.example.myapplication;

    import android.app.Activity; 导入android.content.Context; 导入android.content.Intent; 导入android.content.SharedPreferences; 导入android.icu.util.Calendar; 导入android.support.v7.app.AlertDialog; 导入android.support.v7.app.AppCompatActivity; 导入android.os.Bundle; 导入android.support.v7.widget.DividerItemDecoration; 导入android.support.v7.widget.LinearLayoutManager; 导入android.support.v7.widget.RecyclerView; 导入android.util.Log; 导入android.view.LayoutInflater; 导入android.view.View; 导入android.widget.Button; 导入android.widget.CalendarView; 导入android.widget.EditText; 导入android.widget.TextView; 导入android.widget.Toast;

    import java.text.SimpleDateFormat; 导入java.util.ArrayList; 导入java.util.Date;

    公共类Calender扩展了AppCompatActivity {

    // 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 个答案:

答案 0 :(得分:0)

首先,您需要在现有mArrayList中添加新条目,正如我从您的代码中看到的那样,您是在重新添加所有现有条目,而不是新条目。

在那之后,您只需要再次使用新的mArrayList设置适配器,便可以在回收器视图中看到新数据。