part 17 : Data Passing - Model Class - source code

 Java Part One : 

package first.learn.datapassingtwo;

import androidx.appcompat.app.AppCompatActivity;

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

import com.google.gson.Gson;

import first.learn.datapassingtwo.Person.PersonModel;

public class MainActivity extends AppCompatActivity {


public static String SIMPLE_STRING = null;
public static int SIMPLE_INT = -0;

EditText etOne, etTwo ,etThree,
etFour, etSix,
etFive;
Button btnSimpleInt,
btnSimpleString;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

etOne = (EditText) findViewById(R.id.etOne);
etTwo = (EditText) findViewById(R.id.etTwo);

btnSimpleInt = (Button)findViewById(R.id.btnSimpleInt);
btnSimpleString = (Button)findViewById(R.id.btnSimpleString);

etThree = (EditText)findViewById(R.id.etThree);
etFour = (EditText)findViewById(R.id.etFour);

etSix= (EditText)findViewById(R.id.etSix);
etFive= (EditText)findViewById(R.id.etFive);


btnSimpleString.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String name = etOne.getText().toString();

if(TextUtils.isEmpty(name)){
etOne.setError("Empty");
return;
}



String age = etThree.getText().toString();
if(TextUtils.isEmpty(age)){
etThree.setError("Empty");
return;
}

String address = etFive.getText().toString();
if(TextUtils.isEmpty(age)){
etFive.setError("Empty");
return;
}


etOne.setError(null);
etThree.setError(null);
etFive.setError(null);

String mobileNumber = "No mobile Number";

mobileNumber = etSix.getText().toString();

if(TextUtils.isEmpty(mobileNumber)){
mobileNumber = "No mobile Number";
}



PersonModel personModel = new PersonModel();
personModel.setName(name);
personModel.setAge(age);
personModel.setAddress(address);
personModel.setMobileNumber(mobileNumber);


String stringToPass = new Gson().toJson(personModel);

System.out.println("Json String : "+ stringToPass);


Intent intent = new Intent(MainActivity.this, DataGettingActivity.class);
intent.putExtra("OneModelData", stringToPass);
startActivity(intent);


}
});


btnSimpleInt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String s = etTwo.getText().toString();
if(TextUtils.isEmpty(s)){
etTwo.setError("Empty");
return;
}

etTwo.setError(null);

String t = etFour.getText().toString();

int q = -0 ;

try {
q = Integer.parseInt(t);
} catch (NumberFormatException e) {
throw new RuntimeException(e);
}


int p = -0;

try {
p = Integer.parseInt(s);
} catch (NumberFormatException e) {
throw new RuntimeException(e);
}

if(p==-0||q==-0){
Toast.makeText(MainActivity.this, "Please Write Valid Number", Toast.LENGTH_SHORT).show();
return;
}

Intent intent = new Intent(MainActivity.this, DataGettingActivity.class);

Bundle bundle = new Bundle();
bundle.putInt("SimpleIntOne", p);
bundle.putInt("SimpleIntTwo", q);
intent.putExtras(bundle);
startActivity(intent);


}
});

}





}



xml Part One : 


<?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"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="16dp">


<EditText
android:hint="name....."
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etOne"/>

<EditText
android:hint="age....."
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etThree"/>


<EditText
android:hint="address....."
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etFive"/>


<EditText
android:hint="MobileNumber.....(optional)"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etSix"/>

<Button
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSimpleString"
android:text="Passing Model Data"/>


<EditText
android:hint="Simple int ....."
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etTwo"
android:inputType="number"/>


<EditText
android:hint="Simple int ....."
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etFour"
android:inputType="number"/>


<Button
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnSimpleInt"
android:text="Button Simple Static Integer"/>









</LinearLayout>


Java Part Two : 


package first.learn.datapassingtwo;

import static first.learn.datapassingtwo.MainActivity.SIMPLE_INT;
import static first.learn.datapassingtwo.MainActivity.SIMPLE_STRING;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import first.learn.datapassingtwo.Person.PersonModel;

public class DataGettingActivity extends AppCompatActivity {


TextView tvShow;

private String private_simple_string = null;
private int private_int = -0;

@SuppressLint("SetTextI18n")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_getting);

tvShow = (TextView) findViewById(R.id.tvShow);

if(getIntent()!=null){

if(getIntent().getStringExtra("SimpleString")!=null){

String s = getIntent().getStringExtra("SimpleString");

tvShow.setText(s);

}

}



if(getIntent()!=null){

if(getIntent().getIntExtra("SimpleInt", -0)!=-0){

int p = getIntent().getIntExtra("SimpleInt", -25);
tvShow.setText(""+p);

}

}


if(SIMPLE_STRING!=null){

tvShow.setText(SIMPLE_STRING);
private_simple_string = SIMPLE_STRING;
System.out.println("Taken STATIC String first : " + SIMPLE_STRING);


SIMPLE_STRING = null;

tvShow.setText(private_simple_string );

System.out.println("Taken Private String : " + private_simple_string);
System.out.println("Taken Static String : " + SIMPLE_STRING);


Toast.makeText(this, "Found : "+private_simple_string, Toast.LENGTH_SHORT).show();


}


if(SIMPLE_INT!=-0){

private_int = SIMPLE_INT ;
System.out.println("Static Int First : "+ SIMPLE_INT);

SIMPLE_INT = -0 ;
tvShow.setText(""+private_int);
System.out.println("Static Int Second : "+ SIMPLE_INT);
System.out.println(" private Int : "+ private_int);

}


if(getIntent()!=null){

if(getIntent().getExtras()!=null){

Bundle bundle = getIntent().getExtras();

if(bundle!=null) {
String a = bundle.getString("bundleStringOne");
String b = bundle.getString("bundleStringTwo");
System.out.println("a : "+ a +" b : "+ b );

tvShow.setText(a + " -- "+b );

}
}
}

if(getIntent()!=null){

if(getIntent().getExtras()!=null){

Bundle bundle = getIntent().getExtras();

if(bundle!=null) {

int a = bundle.getInt("SimpleIntOne");
int b = bundle.getInt("SimpleIntTwo");

System.out.println("a : "+ a +" b : "+ b );

tvShow.setText(a + " -- "+b );

}
}
}




if(getIntent()!=null){

if(getIntent().getStringExtra("OneModelData")!=null){

String foundString = getIntent().getStringExtra("OneModelData");

System.out.println("Json String get : "+ foundString);


PersonModel personModel = new Gson().fromJson(foundString, PersonModel.class );

String name = personModel.getName();
String age = personModel.getAge();
String address = personModel.getAddress();
String phoneNumber = personModel.getMobileNumber();


tvShow.setText(
"Name : "+name
+ "\n\n" +"Age: "+ age
+ "\n\n"+ "Address: "+ address
+ "\n\n"+"MobileNumber: "+ phoneNumber
);




}
}




}



}


xml Part Two : 

<?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"
tools:context=".DataGettingActivity"
android:orientation="vertical"
android:layout_margin="16dp">


<TextView
android:hint="Data....."
android:textSize="30dp"
android:textColor="@color/purple_700"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvShow"
/>




</LinearLayout>


Java Part 3 : 


package first.learn.datapassingtwo.Person;

public class PersonModel {

String name, age, address, mobileNumber;

public PersonModel() {
}

public PersonModel(String name, String age, String address, String mobileNumber) {
this.name = name;
this.age = age;
this.address = address;
this.mobileNumber = mobileNumber;
}


public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getMobileNumber() {
return mobileNumber;
}

public void setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
}


Java Part Four : 

package first.learn.datapassingtwo.Person;

public class BusinessClass {

String customerName, customerAddress, phone, quantity, unitPrice, totalPrice;

public BusinessClass() {
}

public BusinessClass(String customerName, String customerAddress, String phone, String quantity, String unitPrice, String totalPrice) {
this.customerName = customerName;
this.customerAddress = customerAddress;
this.phone = phone;
this.quantity = quantity;
this.unitPrice = unitPrice;
this.totalPrice = totalPrice;
}

public String getCustomerName() {
return customerName;
}

public void setCustomerName(String customerName) {
this.customerName = customerName;
}

public String getCustomerAddress() {
return customerAddress;
}

public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getQuantity() {
return quantity;
}

public void setQuantity(String quantity) {
this.quantity = quantity;
}

public String getUnitPrice() {
return unitPrice;
}

public void setUnitPrice(String unitPrice) {
this.unitPrice = unitPrice;
}

public String getTotalPrice() {
return totalPrice;
}

public void setTotalPrice(String totalPrice) {
this.totalPrice = totalPrice;
}
}



Dependecy : 


implementation 'com.google.code.gson:gson:2.10.1'









No comments:

Post a Comment