Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
486 views
in Technique[技术] by (71.8m points)

android - How to update recyclerview from another activity (button) JAVA

Im building a notes app and got stuck on the stage where I want to update my StaggeredRecycleView from another activity after pressing a button. The different options to update the recycleviewadapter is not available to me for some reason. thank you for your time!

Here is 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));


            }
        });


    }

Here is the 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 ();


            }

            

        });

Here is the activity where I want to update from after I press the button "right function"



    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);

question from:https://stackoverflow.com/questions/65546377/how-to-update-recyclerview-from-another-activity-button-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have some options (based on your design model):

  1. When you run the second activity, the first activity is paused, when you close the second activity and back to first activity, then onResume function is called, then you can update the RecyclerView on the onResume function.
  2. If you start the second activity using startActivityForResults, then after closing the second activity, you will be directed to onActivityResult function inside the first activity. You can even pass some data back from second activity to first activity and then update the RecyclerView.
  3. If you want the RecyclerView to be updated while both screens are visible, I would suggest using fragments. A fragment containing the RecyclerView and another fragment containing the second view. Then you can create an interface in the first fragment and implement an interface on the second fragment. Using this way, you can update the RecyclerView in realtime.
  4. There are some other ideas like using Firebase for updating and retrieving data. Using Firebase, you can update data on the second screen, and these data will be automatically fetched by the first screen.

Wish these ideas are helpful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...