Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 How to add elements on ArrayList in Android and display ArrayList data into the next activity?




Step1st:-


Create Class and define a property on it's:-


package com.example.additionapplication;

public class Student {
private int rno;
private String sname;
public Student(int rno, String sname)
{
this.rno=rno;
this.sname=sname;
}
public int getRno() {
return rno;
}

public void setRno(int rno) {
this.rno = rno;
}

public String getSname() {
return sname;
}

public void setSname(String sname) {
this.sname = sname;
}
}


Step2nd:-
Create Activity and Design xml file 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity14">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtr1"
android:hint="Enter rno"
></EditText>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtr2"
android:hint="Enter sname"
></EditText>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:id="@+id/btnadd"
></Button>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Final"
android:id="@+id/btnfinal"
></Button>
</LinearLayout>


Step3rd:-
Create .java file and Write code on it to add elements.

package com.example.additionapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity14 extends AppCompatActivity {

EditText txt1,txt2;
Button btn,btn1;
List<Student> stu= new ArrayList<Student>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main14);
txt1 = findViewById(R.id.txtr1);
txt2 = findViewById(R.id.txtr2);
btn1 = findViewById(R.id.btnadd);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

stu.add(new Student(Integer.parseInt(txt1.getText().toString()),txt2.getText().toString()));
}
});
btn = findViewById(R.id.btnfinal);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(Student st:stu)
{
Toast.makeText(getApplicationContext(),"Rno is "+st.getRno() + " Name is "+st.getSname(),Toast.LENGTH_LONG).show();
}
}
});
}
}




ASSIGNMENT 2nd:-


DISPLAY DATA IN SECOND ACTIVITY:-


STEP!ST:-


CREATE POJO CLASS or SERIALIZABLE CLASS:-

1)

package com.example.additionapplication;

import java.io.Serializable;

public class Student implements Serializable {
private int rno;
private String sname;
public Student(int rno, String sname)
{
this.rno=rno;
this.sname=sname;
}


public int getRno() {
return rno;
}

public void setRno(int rno) {
this.rno = rno;
}

public String getSname() {
return sname;
}

public void setSname(String sname) {
this.sname = sname;
}

public String toString()
{
return "rno is "+rno + "name is "+sname;
}
}


2)  Create DESIGn FILE using XML for Source Activity:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity14">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtr1"
android:hint="Enter rno"
></EditText>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtr2"
android:hint="Enter sname"
></EditText>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:id="@+id/btnadd"
></Button>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Final"
android:id="@+id/btnfinal"
></Button>
</LinearLayout>


3)  CREATE JAVA CODE for SOURCE ACTIVITY TO SET DATA ON LIST:-

package com.example.additionapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity14 extends AppCompatActivity {

EditText txt1,txt2;
Button btn,btn1;
ArrayList<Student> stu= new ArrayList<Student>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main14);
txt1 = findViewById(R.id.txtr1);
txt2 = findViewById(R.id.txtr2);
btn1 = findViewById(R.id.btnadd);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

stu.add(new Student(Integer.parseInt(txt1.getText().toString()),txt2.getText().toString()));
}
});
btn = findViewById(R.id.btnfinal);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent i = new Intent(getApplicationContext(),MainActivity15.class);
i.putExtra("data",stu);
startActivity(i);
/* for(Student st:stu)
{
Toast.makeText(getApplicationContext(),"Rno is "+st.getRno() + " Name is "+st.getSname(),Toast.LENGTH_LONG).show();
}*/
}
});
}
}


4)  Create ANOTHER ACTIVITY TO VIEW DATA

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity15">

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lstres">

</ListView>
</LinearLayout>


5)  ACTIVITY JAVA CODE TO DISPLAY DATA FROM LIST

package com.example.additionapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity15 extends AppCompatActivity {

ListView lst;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main15);
ArrayList<Student> arrstu;
arrstu = (ArrayList<Student>) getIntent().getSerializableExtra("data");
lst = findViewById(R.id.lstres);

ArrayAdapter aa = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,arrstu);
lst.setAdapter(aa);

/* for(Student st:arrstu)
{
Toast.makeText(getApplicationContext(),"Rno is "+st.getRno() + " Name is "+st.getSname(),Toast.LENGTH_LONG).show();
}
for(Student s:arrstu)
{
txtrno.setText(s.getRno());
txtsname.setText(s.getSname());
}*/

}
}








Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post