Reputation: 163
I have successfully passed arraylist of objects by using parcelable. But when i am trying to pass a object from one activity to another its not working.
In first Activity (ShowActivity), i have a gridView and for onclick i want to send a object to another activity and show the result of 1 one object's value :
ShowActivity ::
gridView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View v,int position, long id)
{
aStudent = students.get(position);
Toast.makeText(ShowActivity.this, String.valueOf(aStudent.getstudentdID()), 2000).show();
/*Intent myIntent = new Intent(ShowActivity.this,ViewStudentInfoActivity.class);
myIntent.putExtra("studentObj", aStudent);
//myIntent.putExtra("studentObj", aStudent.getstudentName());
startActivity(myIntent);*/
Bundle b = new Bundle();
b.putParcelable("studentObj", aStudent);
Intent myIntent2 = new Intent(ShowActivity.this,ViewStudentInfoActivity.class);
startActivity(myIntent2.putExtras(b));
}
});
And in onCreate() of ViewStudentInfoActivity ::
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewstudent_info);
getPassedVal();
}
private void getPassedVal()
{
Intent intent = new Intent();
/* if(intent!=null)
{
Student aaStudent = (Student) intent.getParcelableExtra("studentObj");
Log.d("vv", aaStudent.getstudentName());
} */
Bundle b = getIntent().getExtras();
if(b != null) {
Student aaStudent = (Student)(b.getParcelable("studentObj"));
String str = aaStudent.getstudentName().toString();
}
}
Though the toast is showing the result, which means the object is ok. but cant retrive it from the second activity? Its weird. Am i doing something wrong?
Upvotes: 1
Views: 602
Reputation: 11948
try use following code:
b.putExtra("studentObj", aStudent); // instead of b.putParcelable
and for getting that:
Student aaStudent = b.getParcelable("studentObj");
if this not helped you please post Student
class
Upvotes: 0
Reputation: 3357
Take a look at these lines:
b.putParcelable("studentObj", aStudent);
Student aaStudent = (Student)(b.getParcelable("aStudent"));
You use different keys. "studentObj" vs "aStudent".
Upvotes: 1