如何从另一个 activity(按钮)JAVA 更新 recyclerview

How to update recyclerview from another activity (button) JAVA

我正在构建一个笔记应用程序并卡在了我想在按下按钮后从另一个 activity 更新我的 StaggeredRecycleView 的阶段。由于某种原因,我无法使用更新 recycleviewadapter 的不同选项。感谢您的宝贵时间!

这里是 RecViewAdapter


     public StaggeredRecycleViewAdapter(Context mContext, ArrayList<String> arrTitle, ArrayList<String> arrNote) {
        this.arrTitle = arrTitle;
        this.arrNote = arrNote;
        this.mContext = mContext;



    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_grid_item, parent, false);


        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        Log.d(TAG, "onBindViewHolder: called");

        RequestOptions requestOptions = new RequestOptions().placeholder(R.drawable.ic_launcher_background);

        //GET TEXT HÄÄÄR

        holder.txtTitle.setText(arrTitle.get(position));
        holder.txtNote.setText(arrNote.get(position));

        // DET HÄR FÖR ATT ÖPPNA TEXT FRÅN STAGGERED VIEW

        holder.txtNote.setOnClickListener(new View.OnClickListener() {
            @Override


            public void onClick(View v) {
                Log.d(TAG, "onClick: clicked on note " + arrNote.get(position));


                String rvTitleText;
                String rvNoteText;
                rvTitleText = MainActivity.arrTitle.get(position).toString();
                rvNoteText = MainActivity.arrNote.get(position).toString();


                Intent editIntent = new Intent(mContext, edTxt_activity.class);
                editIntent.putExtra("headerText", rvTitleText.toString());
                editIntent.putExtra("noteText", rvNoteText.toString());
                editIntent.putExtra("arrIndex", position);
                mContext.startActivity(editIntent);



            }
        });


        holder.txtTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "onClick: clicked on title " + arrTitle.get(position));


            }
        });


    }

这是 MainActivity



    public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final int NUM_COLUMNS = 2;

     static ArrayList<String> arrTitle = new ArrayList<>();
     static ArrayList<String> arrNote = new ArrayList<>();

    FloatingActionButton btn_add;



    public void StartEdTxtActivity (){
        Intent editIntent = new Intent(getApplicationContext(), edTxt_activity.class);
        startActivity(editIntent);
    }



private void addSampleText (){
    Log.d(TAG, "onCreate: test test of mainactivity text");


    arrTitle.add("My first note");
    arrNote.add("Lorem ipsum dolor sit amet, consectetur adipisicing elit, " +
            "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " +
            "Ut enim ad minim veniam, quis nostrud");

}

private void initRecyclerView () {
    System.out.println("init staggered recycleview");
    RecyclerView recyclerView = findViewById(R.id.recyclerView);
    StaggeredRecycleViewAdapter staggeredRecycleViewAdapter = new StaggeredRecycleViewAdapter(this, arrTitle, arrNote);
    StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(NUM_COLUMNS, LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(staggeredGridLayoutManager);
    recyclerView.setAdapter(staggeredRecycleViewAdapter);
}

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

         btn_add = findViewById(R.id.btn_add);

        initRecyclerView();
         addSampleText();



        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("Button", "add button is pressed");

                StartEdTxtActivity ();


            }

            

        });

这是 activity 在我按下“右功能”按钮后我想更新的地方



    public void checkFunction (View view) {

        Log.i("this is INDEX INTENT", String.valueOf(indexPosition));
    }

    public void right_function (View view) {
        Log.i("Check", "CHECK ICON PRESSED");
        titleText = edtTxt_header.getText().toString();
        noteText = textInputEditText.getText().toString();


        if (titleText.equals(t1) && noteText.equals(n1)) {

            finish();


        }

        else if (titleText.equals(t1) && !noteText.equals(n1)){
             MainActivity.arrNote.set(indexPosition, noteText);

StaggeredRecycleViewAdapter.notifyDataSetChanged() <------- HERE IS MY PROBLEM,CANT RUN notifydatachanger
            finish();
        }

        else if (noteText.equals(n1) && titleText!= t1) {
            MainActivity.arrTitle.set(indexPosition, titleText);
            
            finish();
        }

        else  {
            MainActivity.arrTitle.add(titleText);
            MainActivity.arrNote.add(noteText);


            finish();
        }



    }


    /**
     * BACK BUTTON
     * @param view
     */
    public void left_function (View view) {

        //Add 'are you sure dialog here`


        finish();
    }

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

        getSupportActionBar().hide();
        leftIcon = findViewById(R.id.left_icon);
        rightIcon = findViewById(R.id.right_icon);
        edtTxt_header = (EditText)findViewById(R.id.edtTxt_header);
        textInputEditText = (TextInputEditText) findViewById(R.id.textInputEditText);
        btn_check = findViewById(R.id.btn_check);



        Intent editIntent = getIntent();


        t1 =  editIntent.getStringExtra("headerText");
        n1  = editIntent.getStringExtra("noteText");
        indexPosition =  editIntent.getIntExtra("arrIndex", 0);

        edtTxt_header.setText(t1);
        textInputEditText.setText(n1);

您有一些选择(基于您的设计模型):

  1. 当您 运行 第二个 activity 时,第一个 activity 暂停, 当您关闭第二个 activity 并返回第一个 activity 时,然后 onResume 函数被调用,然后你可以更新 RecyclerView on onResume 函数。
  2. 如果您使用 startActivityForResults 启动第二个 activity, 然后在关闭第二个 activity 后,您将被引导至 onActivityResult函数里面第一个activity。你甚至可以 将一些数据从第二个 activity 传递回第一个 activity 然后 更新 RecyclerView。
  3. 如果您希望在两个屏幕都打开时更新 RecyclerView 可见,我建议使用片段。一个片段包含 RecyclerView 和另一个包含第二个视图的片段。然后 您可以在第一个片段中创建一个接口并实现一个 第二个片段上的界面。使用这种方式,您可以更新 RecyclerView 实时查看。
  4. 还有一些其他想法,例如使用 Firebase 更新和检索数据。使用Firebase,可以在副屏更新数据,这些数据会被首屏自动抓取。

希望这些想法对您有所帮助。