Just starting to dive into android, in this regard, there are some difficulties. I have a recyclerView with Note objects in the project, when I click on them, I open activity with information about this "note", specifically the title and the main text. I have the podgruzka title and text in the activity, when it is opened, but I do not understand how I can save the edited note and display the updated title and text on the "main" screen.

Code recyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> { Context mContext; List<Note> mData; Dialog mDialog; //NotesFragment notesFragment; int selected; public RecyclerViewAdapter(Context mContext, List<Note> mData) { this.mContext = mContext; this.mData = mData; //this.notesFragment = notesFragment; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View v; v = LayoutInflater.from(mContext).inflate(R.layout.notes_list_item,viewGroup,false); final MyViewHolder viewHolder = new MyViewHolder(v); //---------------------Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ Π΄ΠΈΠ°Π»ΠΎΠ³ΠΎΠ²ΠΎΠ³ΠΎ ΠΎΠΊΠ½Π° ΠΈ Π΅Π³ΠΎ Π²Ρ‹Π·ΠΎΠ² ΠΏΠΎ ΠΊΠ½ΠΎΠΏΠΊΠ΅---------------------------------------------------------- mDialog = new Dialog(mContext); mDialog.setContentView(R.layout.dialog_note); Button yes_button = mDialog.findViewById(R.id.dialog_yes_btn); Button no_button = mDialog.findViewById(R.id.dialog_no_btn); viewHolder.item_note.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Toast.makeText(mContext,"Test Click" + String.valueOf(viewHolder.getAdapterPosition()),Toast.LENGTH_SHORT).show(); mDialog.show(); selected = viewHolder.getAdapterPosition(); return false; } }); yes_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { deleteItem(selected); mDialog.cancel(); notifyDataSetChanged(); // for (int i = 0; i < mData.size(); i++) { // mData[i] // } }catch (ArrayIndexOutOfBoundsException e){ Log.e("Error","\n ArrayIndexOutOfBoundsException length=10 index=-1 \n ΠžΠΏΡΡ‚ΡŒ -1, Π½ΠΎ ΠΊΠ°ΠΊ ???"); } } }); no_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mDialog.cancel(); } }); //------------------------------------------------------------------------------------------------------------------------------------- // Π Π΅Π΄Π°ΠΊΡ‚ΠΎΡ€ Π·Π°ΠΌΠ΅Ρ‚ΠΊΠΈ? viewHolder.item_note.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(mContext,NoteActivity.class); intent.putExtra("Title",viewHolder.tv_Name.getText().toString()); intent.putExtra("Text",viewHolder.tv_Text.getText().toString()); mContext.startActivity(intent); } }); return viewHolder; } @Override public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { holder.tv_Name.setText(mData.get(position).getName()); holder.tv_Date.setText(mData.get(position).getDate()); holder.button.setBackgroundResource(mData.get(position).getFavourite()); holder.tv_Text.setText(mData.get(position).getText()); } @Override public int getItemCount() { return mData.size(); } public static class MyViewHolder extends RecyclerView.ViewHolder{ private LinearLayout item_note; private TextView tv_Name; private TextView tv_Date; private Button button; private TextView tv_Text; public MyViewHolder(@NonNull View itemView) { super(itemView); item_note = itemView.findViewById(R.id.note_item); tv_Name = itemView.findViewById(R.id.name_note); tv_Date = itemView.findViewById(R.id.date_note); button = itemView.findViewById(R.id.favorite_btn_note); tv_Text = itemView.findViewById(R.id.text_note); } } //Π”ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ Π΄Π°Π½Π½Ρ‹Ρ… public void addItem(int position, Note note){ mData.add(position, note); //ΠœΡ‹ ΠΌΠΎΠΆΠ΅ΠΌ Π²Ρ‹Π·Π²Π°Ρ‚ΡŒ super.notifyItemInserted(position); } //Π£Π΄Π°Π»Π΅Π½ΠΈΠ΅ Π΄Π°Π½Π½Ρ‹Ρ… public void deleteItem(int position){ mData.remove(position); //Π’ΠΎ ΠΆΠ΅ самоС с ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠΌ super.notifyItemRemoved(position); notifyItemRemoved(position); notifyItemRangeChanged(position, mData.size()); // notesFragment.decreasePosition(); NotesFragment.decreasePosition(); } 

}

Code NoteActivity.java

public class NoteActivity extends AppCompatActivity {

 private String NoteTitle; private String NoteText; EditText textNote; EditText titleNote; Button saveBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_note); saveBtn = findViewById(R.id.save_btn); // saveBtn.setCompoundDrawablesWithIntrinsicBounds( // AppCompatResources.getDrawable(this, R.drawable.save), null, null, null); --ΠΈΠΊΠΎΠ½ΠΎΡ‡ΠΊΠ° titleNote = findViewById(R.id.title_note); textNote = findViewById(R.id.main_text_note); Intent intent = getIntent(); NoteTitle = intent.getStringExtra("Title"); NoteText = intent.getStringExtra("Text"); titleNote.setText(NoteTitle); textNote.setText(NoteText); saveBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Intent intent = new Intent(getApplicationContext(),MainActivity.class); // startActivity(intent); -- созданиС Π½ΠΎΠ²ΠΎΠΉ ΠΌΠ΅ΠΉΠ½ активности Intent intent = new Intent(getApplicationContext(),MainActivity.class); intent.putExtra("TitleGet",titleNote.getText().toString()); finish(); } }); } 

}

I repeat - you need to somehow get the updated title and text of the note by clicking on the saveBtn button. I apologize in advance if something is not clear. One of the first times on the site. Thanks in advance for the answer!

  • one
    You need to write all the data to the database, for example, and when outputting to the recycler, read from there. There is no need to transfer between activations. that is, in NoteActivity, fill in the fields and when you click the button, write them to the database, when you switch to activations with a list in it, read data from the database and output it to the list. - pavlofff
  • What I can add is that it is worth creating the DiffUtils class in order not to update the entire list, but only what has changed. - Sergey Buvak
  • @pavlofff I apologize for the stupid question, probably, but what is the database and how to write it?)) - brkas
  • DB - a database (a means for storing information and access to it), not to write it, but to it (write data). pure SQLite or ORM, for example, Room, Google knows all these names in detail - pavlofff pm

0