CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Plagiarized on 2018-09-08
by Anupa Dayaratne

Original Post

Original - Posted on 2016-12-30
by Martin Sing



            
Present in both answers; Present only in the new answer; Present only in the old answer;

First make note object serialized. To do that modify your Note.java file. as below
import java.io.Serializable;
public class Note implements Serializable { private String title; private String description; private int priority;
public Note() { //empty constructor needed }
public Note(String title, String description, int priority) { this.title = title; this.description = description; this.priority = priority; }
public String getTitle() { return title; }
public String getDescription() { return description; }
public int getPriority() { return priority; }}

In MainActivity, update the `adapter.setOnItemClickListener` as below.
adapter.setOnItemClickListener(new NoteAdapter.OnItemClickListener() { @Override public void onItemClick(DocumentSnapshot documentSnapshot, int position) { Note note = documentSnapshot.toObject(Note.class); String id = documentSnapshot.getId(); String path = documentSnapshot.getReference().getPath(); Toast.makeText(MainActivity.this, "Position: " + position + " ID: " + id, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, ActivityDetail.class); Bundle bundle = new Bundle(); bundle.putSerializable("NOTE_ITEM", note); intent.putExtras(bundle);
startActivity(intent); } });
In ActivityDetails.java file, update `onCreate()` as below
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail);
Intent intent = this.getIntent(); Bundle bundle = intent.getExtras();
Note note = (Note) bundle.getSerializable("NOTE_ITEM"); }
Now you can access note object in your ActivityDetails.java file.
Current responses are great but a more comprehensive answer is needed for beginners. There are 3 different ways to start a new activity in Android, and they all use the `Intent` class; [Intent | Android Developers][1].
1. Using the `onClick` attribute of the Button. (Beginner) 2. Assigning an `OnClickListener()` via an anonymous class. (Intermediate) 3. Activity wide interface method using the `switch` statement. (Pro)
Here's the link to my example if you want to follow along: [https://github.com/martinsing/ToNewActivityButtons][2]

1. Using the `onClick` attribute of the Button. (Beginner) ---------- Buttons have an `onClick` attribute that is found within the .xml file:
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="goToAnActivity" android:text="to an activity" />
<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="goToAnotherActivity" android:text="to another activity" />
In Java class:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); }
public void goToAnActivity(View view) { Intent intent = new Intent(this, AnActivity.class); startActivity(intent); }
public void goToAnotherActivity(View view) { Intent intent = new Intent(this, AnotherActivity.class); startActivity(intent); } **Advantage**: Easy to make on the fly, modular, and can easily set multiple `onClick`s to the same intent.
**Disadvantage**: Difficult readability when reviewing.

2. Assigning an `OnClickListener()` via an anonymous class. (Intermediate) ----------

This is when you set a separate `setOnClickListener()` to each `button` and override each `onClick()` with its own intent.
In Java class:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(view.getContext(), AnActivity.class); view.getContext().startActivity(intent);} }); button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(view.getContext(), AnotherActivity.class); view.getContext().startActivity(intent);} });
**Advantage**: Easy to make on the fly.
**Disadvantage**: There will be a lot of anonymous classes which will make readability difficult when reviewing.
3. Activity wide interface method using the `switch` statement. (Pro) ---------- This is when you use a `switch` statement for your buttons within the `onClick()` method to manage all the Activity's buttons.
In Java class:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button1.setOnClickListener(this); button2.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.button1: Intent intent1 = new Intent(this, AnActivity.class); startActivity(intent1); break; case R.id.button2: Intent intent2 = new Intent(this, AnotherActivity.class); startActivity(intent2); break; default: break; }
**Advantage**: Easy button management because all button intents are registered in a single `onClick()` method
----------

For the second part of the question, passing data, please see [https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android][3]

[1]: https://developer.android.com/reference/android/content/Intent.html [2]: https://github.com/martinsing/ToNewActivityButtons [3]: https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android

        
Present in both answers; Present only in the new answer; Present only in the old answer;