There is an activity with recyclView.

public class NotesActivity extends AppCompatActivity implements adapter.ItemTouchHelper.ItemTouchHelperListener{ public List<Notes> notesList; private NotesHandler notesHandler; private NotesAdapter notesAdapter; private RecyclerView recyclerView; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.notes_activity); notesHandler = new NotesHandler(getApplicationContext()); recyclerView = findViewById(R.id.recycle_view); prepareData(); notesAdapter = new NotesAdapter(getApplicationContext(), notesList); RecyclerView.LayoutManager manager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(manager); recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL)); recyclerView.setAdapter(notesAdapter); 

In which there is a list of elements, by pressing which, I receive data from the element and open a new activity, I use it to edit the data.

 @Override public void onClick(View v) { int pos = getAdapterPosition(); if (pos != RecyclerView.NO_POSITION){ Intent intent = new Intent(context, CreateActivity.class); intent.putExtra("id", pos); intent.putExtra("title", textViewTitle.getText().toString()); intent.putExtra("text", textViewText.getText().toString()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } } 

In another activity I get this data.

 @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.creature_activity); notesHandler = new NotesHandler(this); editSubject = findViewById(R.id.edit_subject); editText = findViewById(R.id.edit_text); int id; String title; String text; id = getIntent().getExtras().getInt("id"); title = getIntent().getExtras().getString("title"); text = getIntent().getExtras().getString("text"); if (title != null && text != null){ editSubject.setText(title); editText.setText(text); } } 

But I still use this activity to create data, where I don’t need to receive it, the above described. How to be? To put

 try { } catch (Exception e){ } 

???

  • Well, you send an ID equal, for example, -1 and do branching - if id = -1 has arrived, then you skip it all here .. above described - pavlofff
  • or if you do not send an intent when creating data, then just check it for null and also do branching - pavlofff
  • The fact is that when creating a activity, it tries to get an id, anyway, if it could not get it, it crashes: id = getIntent (). GetExtras (). GetInt ("id"); - Anton Lyalin
  • Intent i = getIntent(); if (i == null) ...создание... else .. редактирование.. Intent i = getIntent(); if (i == null) ...создание... else .. редактирование.. - pavlofff

1 answer 1

Try this:

 if(getIntent().hasExtra("id")){ id = getIntent().getExtras().getInt("id"); title = getIntent().getExtras().getString("title"); text = getIntent().getExtras().getString("text"); }else{ //что-то с id и другим. } 

Check whether there is an extra with the name "id".