Skip to content

Instantly share code, notes, and snippets.

@devrath
Created June 24, 2015 06:02
Show Gist options
  • Select an option

  • Save devrath/24e0ec6ad970109c6866 to your computer and use it in GitHub Desktop.

Select an option

Save devrath/24e0ec6ad970109c6866 to your computer and use it in GitHub Desktop.
pass class object between activities
// IN THE LISTVIEW CLICK of Activity-1 USE
lstViewId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
TextView titleName=(TextView) view.findViewById(R.id.txtTitleId);
//Start a new activity by putting a parcellable data into the intent
Intent intent=new Intent(getActivity().getApplicationContext(), Activity2.class);
passingObj=new MediaSelList(mediaBaseModel.getMedialist().get(0).getMediaItems());
intent.putExtra("data", passingObj);
intent.putExtra("title",titleName.getText());
startActivity(intent);
}
});
//
//
//
// IN THE Activity-2 USE
//
//
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_media_selectionlist);
//Setting up the toolbar
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (getIntent().getExtras() != null) {
if(getIntent().getExtras().getString("title")!=null && getIntent().getExtras().getString("title").length()>0){
//If the is no null value for the title set that value obtained
getSupportActionBar().setTitle(getIntent().getSerializableExtra("title").toString());
}else{
//Else do not set the title, instead just retain the already inflated value in toolbar title.
}
//Get the data from the previous activity and store in a class
MediaSelList dw = (MediaSelList) getIntent().getSerializableExtra("data");
ArrayList<MediaItems> list = dw.getMediaItems();
//Setting the listview
lstViewId.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
lstViewId.setLayoutManager(mLayoutManager);
mAdapter = new AdptMediaSelectionList(ActMediaSelectionList.this,dw.getMediaItems());
lstViewId.setAdapter(mAdapter);
//Setting the listview
}
}
//
//
//
//-MODEL-CLASS
//
//
//
public class MediaSelList implements Serializable {
private ArrayList<MediaItems> MediaItems;
public MediaSelList(ArrayList<MediaItems> data) {
this.MediaItems = data;
}
public ArrayList<MediaItems> getMediaItems() {
return this.MediaItems;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment