public class GsonActivity extends Activity {
private TextView nameView;
private TextView ageView;
private TextView timeView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gson);
nameView = (TextView) findViewById(R.id.nameView);
ageView = (TextView) findViewById(R.id.ageView);
timeView = (TextView) findViewById(R.id.timeView);
Student student = new Student("aa",12,new Date());
Student student1 = new Student("bb",21,new Date());
List<Student> list = new ArrayList<Student>();
list.add(student);
list.add(student1);
Gson gson = new Gson();
String jsonList = gson.toJson(list);
list=gson.fromJson(jsonList,new TypeToken<List<Student>>(){}.getType());
for(Student s:list){
nameView.setText(s.getName());
ageView.setText(s.getAge()+"");
timeView.setText(s.getBrithday().toString());
}
}
}
public class Student {
private String name;
private int age;
private Date brithday;
public Student(String name, int age, Date brithday) {
this.name = name;
this.age = age;
this.brithday = brithday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public Date getBrithday() {
return brithday;
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<TextView
android:id="@+id/nameView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView"
android:layout_marginLeft="50dp"
android:text="姓名" />
<TextView
android:id="@+id/ageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView"
android:layout_marginTop="50dp"
android:text="年龄" />
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ageView"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"
android:text="姓名" />
<TextView
android:id="@+id/timeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView"
android:layout_marginTop="100dp"
android:text="生日" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/ageView"
android:layout_marginLeft="50dp"
android:layout_marginTop="100dp"
android:text="姓名" />
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView"
android:layout_marginTop="110dp" />
<ImageView
android:id="@+id/Iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:src="@drawable/abc_ab_share_pack_mtrl_alpha" />
</RelativeLayout>