Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPE419 Mobile Application Development

Similar presentations


Presentation on theme: "CMPE419 Mobile Application Development"— Presentation transcript:

1 CMPE419 Mobile Application Development
Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department CMPE419 AU

2 Passing data between activities in Android

3 Code for passing data between activities in Android :
MainActivity.java String value = "Hello!"; Intent in = new Intent(this,MainActivity2.class); in.putExtra("Key", value); startActivity(in); MainActivity2.java Bundle bundle = getIntent().getExtras(); String valueReceived = bundle .getString("Key");

4

5 Code for passing multiple data or values between activities in Android :
Method 1: Using Intent to pass data and Bundle to extract data between activities in Android MainActivity.java: String value1 = "Hello!"; String value2 = "Hi!"; Intent in = new Intent(this,MainActivity2.class); in.putExtra("Key1", value1); in.putExtra("Key2", value2); startActivity(in); MainActivity2.java: Bundle bundle = getIntent().getExtras(); String valueReceived1 = bundle .getString("Key1"); String valueReceived2 = bundle .getString("Key2");

6

7 Method 2 : Using Bundle to pass and to extract data between activities in Android
MainActivity.java String value1 = "Hello!"; String value2 = "Hi!"; Intent in = new Intent(this,MainActivity2.class); Bundle bundle = new Bundle(); bundle.putString("Key1", value1); bundle.putString("Key2", value2); in.putExtras(bundle); startActivity(in); MainActivity2.java Bundle bundle = getIntent().getExtras(); String valueReceived1 = bundle .getString("Key1"); String valueReceived2 = bundle .getString("Key2");  

8 Code for passing array between activities in Android :
MainActivity.java String[] array = new String[]{"Item1", "Item2", "item3", "Item4", "item5"}; Intent in = new Intent(this,MainActivity2.class); Bundle bundle = new Bundle(); bundle.putStringArray("MyArray", array); in.putExtras(bundle); startActivity(in); MainActivity2.java Bundle bundle = getIntent().getExtras(); String arrayReceived[] = bundle.getStringArray("MyArray");

9 Code for passing ArrayList between activities in Android :
MainActivity.java ArrayList<String> array = new ArrayList<String>(); array.add("Hello"); array.add("Hi"); array.add("Bye"); Intent intent = new Intent(this, MainActivity2.class); intent.putExtra("array_list", array); startActivity(intent); MainActivity2.java Bundle bundle = getIntent().getExtras(); ArrayList<String> array = (ArrayList<String>)bundle.getStringArrayList("array_list");

10 Code for passing ArrayList <object> between activities in Android :
MainActivity.java ArrayList<Student> stulist=new ArrayList<Student>(); //add students to your list Intent i=new Intent(MainActivity.this,NewActivity.class); Bundle bundle=new Bundle(); bundle.putSerializable("students",stulist); i.putExtras(bundle); startActivity(i);

11 NewActivity.java Bundle bundleobje=getIntent().getExtras();
ArrayList stulis=(ArrayList<Student>) bundleobje.getSerializable("students");

12 public class MainActivity extends Activity {
ArrayList<Student> stulist; Button Add,Send; EditText Edtn,Edts,Edtg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); stulist=new ArrayList<Student>(); Init(); } private void Init() { Add=(Button)findViewById(R.id.button); Send=(Button)findViewById(R.id.button2); Edtn=(EditText)findViewById(R.id.editText); Edts=(EditText)findViewById(R.id.editText2); Edtg=(EditText)findViewById(R.id.editText3); Add.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String Name=Edtn.getText().toString(); Student Stu=new Student(Name,Edts.getText().toString(),Integer.parseInt(Edtg.getText().toString())); stulist.add(Stu); Edtg.setText(""); Edtn.setText(""); Edts.setText(""); }); Send.setOnClickListener(new View.OnClickListener() { Intent i=new Intent(MainActivity.this,NewActivity.class); Bundle bundle=new Bundle(); bundle.putSerializable("students",stulist); i.putExtras(bundle); startActivity(i); } }

13 NewActivity.Java; ArrayList<Student> stulis; ArrayAdapter adapter; ListView mylist; String[] liststring; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new); getstudents(); } private void getstudents() { Bundle bundleobje=getIntent().getExtras(); stulis=(ArrayList<Student>) bundleobje.getSerializable("students"); liststring=new String[stulis.size()]; for(int i=0;i<stulis.size();i++) liststring[i]=stulis.get(i).getName()+" "+stulis.get(i).getSurname()+" "+stulis.get(i).getGrade(); mylist=(ListView)findViewById(R.id.listView); adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,liststring); mylist.setAdapter(adapter);

14 import java.io.Serializable;
/** * Created by PC on 09-Apr-18. */ public class Student implements Serializable { private String Name; private String Surname; private int Grade; Student(String Name, String Surname, int Grade){ this.Name=Name; this.Surname=Surname; this.Grade=Grade; } public String getName() { return Name; public int getGrade() { return Grade; public String getSurname() { return Surname; public void setGrade(int grade) { Grade = grade; public void setName(String name) { Name = name; public void setSurname(String surname) { Surname = surname;


Download ppt "CMPE419 Mobile Application Development"

Similar presentations


Ads by Google