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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…