This text may be helpful for those who want the following two specifications.
Figure 1
Figure 2
1.Contribution.java
public class Contribution implements Serializable{
String category;
Date time;
String title;
String content;
public Contribution(){
category = "general";
time = new Date();
title = "general";
}
public String getCategory() {
return category;
}
public String getTime() {
return time.toString();
}
public void setTime(Date time) {
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String toString() {
return "Contribution-->Title: " + getTitle() + "\nCategory: " + getCategory() +"\nContent:n" + getContent() + "\nDate: " + getTime();
}
}
2.MyContribution.java
public class MyContribution extends Contribution{
public String subTitle;
public MyContribution(String _title,String _content,String _subTitle){
title = _title;
content = _content;
subTitle = _subTitle;
category = "training";
}
public String getSubTitle() {
return subTitle;
}
public String toString(){
return "MyContribution-->Title: " + getTitle() + "\nsubtitle: " + getSubTitle() + "\nCategory: " + getCategory() +"\nContent:\n" + getContent() + "\nDate: " + getTime();
}
}
3.TypeConvertActivity.java
public class TypeConvertActivity extends AppCompatActivity {
Contribution contribution = null;
ArrayList<MyContribution> myContributionArrayList;
ArrayList<Contribution> contributionArrayList;
ArrayList<MyContribution> myRecoveryContributionArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_type_convert);
MyContribution myContribution = new MyContribution("JAVA basics","About development tips","JAVA basics開発編");
contribution = (Contribution) myContribution;
myContributionArrayList = new ArrayList<MyContribution>();
contributionArrayList = new ArrayList<Contribution>();
myRecoveryContributionArrayList = new ArrayList<MyContribution>();
myContributionArrayList.add(new MyContribution("Java Study1","new String('good day')","String Type"));
myContributionArrayList.add(new MyContribution("Java Study2","new Boolean(true)","Boolean Type"));
myContributionArrayList.add(new MyContribution("Java Study3","new Integer(17)","Integer Type"));
myContributionArrayList.add(new MyContribution("Java Study4","new ArrayList<>()","ArrayList Type"));
myContributionArrayList.add(new MyContribution("Java Study5","new Double(3.1415926)","Double Type"));
for(MyContribution m:myContributionArrayList){
Log.d("myContribution",m.toString());
contributionArrayList.add((Contribution) m);
}
for(Contribution m:contributionArrayList){
Log.d("Contribution",m.toString());
myRecoveryContributionArrayList.add((MyContribution) m);
}
for(MyContribution m:myRecoveryContributionArrayList){
Log.d("Recovery Contribution",m.toString());
}
Button bringContributionToNextPage = (Button) findViewById(R.id.bringContributionToNextPage);
bringContributionToNextPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goToActivity();
}
});
}
private void goToActivity(){
Intent intent = new Intent(getApplication(),ReceiveTypeConvertActivity.class);
intent.putExtra("contribution", contribution);
intent.putExtra("contributionArrayList",contributionArrayList);
startActivity(intent);
}
}
4.activity_type_convert.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="com.example.syoui.imagetab.java_knowledge.TypeConvertActivity">
<Button
android:id="@+id/bringContributionToNextPage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BringContributionToNextPage"/>
</android.support.constraint.ConstraintLayout>
5.ReceiveTypeConvertActivity
public class ReceiveTypeConvertActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receive_type_convert);
Intent intent = getIntent();
Serializable contributionSerial = intent.getSerializableExtra("contribution");
if(contributionSerial != null){
Contribution contribution = (Contribution) contributionSerial;
Log.d("Receive:contribution",contribution.toString());
}
Serializable contributionArrayListSerial = intent.getSerializableExtra("contributionArrayList");
if(contributionArrayListSerial != null){
ArrayList<Contribution> contributionArrayList = ( ArrayList<Contribution>)contributionArrayListSerial;
for(Contribution m:contributionArrayList){
Log.d("contributionArrayList",m.toString());
}
}
}
}
6.activity_receive_type_convert.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="com.example.syoui.imagetab.java_knowledge.ReceiveTypeConvertActivity">
</android.support.constraint.ConstraintLayout>
After checking the output of the log, even if I cast the instance to a class that does not have a Contribute class (subtitle), the toString () method is also executing the toString () method of the MyContribution class. Cast an instance of MyContribution to the parent class Contribution once, but the contents do not change. Figure 3 Also, the recipient of the contributionArrayList received the entire contributionArrayList without any problem. Figure 4
It's not a big deal, but it's a problem you can encounter at work.
Recommended Posts