if you want to pass custom object from one activity to another activity you need to implement Serializable interface.
This is student class having some property in that case I want to transfer student object and list of student object between Activity. if you want to know about Serialization you can refer this link.
Student.java
package com.test.google.customobject.intent; import java.io.Serializable; /** * Created by admin on 11-Sep-15. */ public class Student implements Serializable { private String name; private int age; private String address; private String city; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
MainActivity.java
package com.test.google.customobject.intent; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import java.util.ArrayList; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Student mStudent; private ArrayList<Student> mStudentList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); updateStudentData(); } private void updateStudentData() { mStudentList= new ArrayList<Student>(); for (int count = 0; count < 4; count++) { mStudent= new Student(); mStudent.setAddress("Delhi"); mStudent.setAge(30); mStudent.setName("Test User"); mStudent.setCity("Faridabad"); mStudentList.add(mStudent); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btnClick: Bundle bundle= new Bundle(); bundle.putSerializable("studentData", mStudent); startActivity( new Intent(this,StudentActivity.class).putExtras(bundle)); break; case R.id.btnListObject: Intent intent= new Intent(this,StudentActivity.class); intent.putExtra("studentList",mStudentList ); intent.putExtra("isListObject", true); startActivity(intent); break; } } }
StudentActivity.java
package com.test.google.customobject.intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.LinearLayout; import android.widget.TextView; import java.util.List; public class StudentActivity extends AppCompatActivity { private LinearLayout mRelativeLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_student); mRelativeLayout= (LinearLayout) findViewById(R.id.container); showData(); } private void showData() { boolean isListObject= getIntent().getBooleanExtra("isListObject", false); if(isListObject){ List<Student> student= (List<Student>) getIntent().getSerializableExtra("studentList"); int studentSize=student.size(); for (int studentCount = 0; studentCount <studentSize ; studentCount++) { getStudentView(student.get(studentCount)); } }else{ Student student= (Student) getIntent().getSerializableExtra("studentData"); getStudentView(student); } } private void getStudentView(Student student){ View studentItemView= getLayoutInflater().inflate(R.layout.student_row, null); TextView nameTextView= (TextView) studentItemView.findViewById(R.id.tvName); TextView addressTextView= (TextView) studentItemView.findViewById(R.id.tvAddress); TextView ageTextView= (TextView) studentItemView.findViewById(R.id.tvAge); TextView cityTextView= (TextView) studentItemView.findViewById(R.id.tvCity); nameTextView.setText(student.getName()); addressTextView.setText(student.getAddress()); ageTextView.setText(String.valueOf(student.getAge())); cityTextView.setText(student.getCity()); mRelativeLayout.addView(studentItemView); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_student, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="fill_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:onClick="onClick" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:id="@+id/btnClick" android:text="@string/action_button" android:layout_height="wrap_content"/> <Button android:layout_width="wrap_content" android:onClick="onClick" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_below="@id/btnClick" android:id="@+id/btnListObject" android:text="@string/list_action_string" android:layout_height="wrap_content"/> </RelativeLayout>
activity_student.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" android:id="@+id/container" android:paddingBottom="@dimen/activity_vertical_margin" tools:context="map.test.google.ajitdubey.intent.StudentActivity"> </LinearLayout> </ScrollView>
student_row.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/tvAge" android:layout_width="wrap_content" android:layout_below="@+id/tvName" android:layout_height="wrap_content"/> <TextView android:id="@+id/tvAddress" android:layout_width="wrap_content" android:layout_below="@+id/tvAge" android:layout_height="wrap_content"/> <TextView android:id="@+id/tvCity" android:layout_below="@+id/tvAddress" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_width="match_parent" android:background="@android:color/darker_gray" android:layout_marginTop="5dp" android:layout_height="1dp"/> </LinearLayout>
No comments:
Post a Comment