在MainActivity聲明一個(gè)動(dòng)態(tài)數(shù)組,把數(shù)據(jù)存在數(shù)組,然后通過(guò)Intent把整個(gè)數(shù)組傳遞到ListActivity。
activity_main.xml:
<?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">
<Button
android:id="@+id/btn_res"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="收到數(shù)據(jù)"/>
<Button
android:id="@+id/btn_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查看記錄"/>
</LinearLayout>
activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv_data"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
MainActivity.java:
package com.example.listviewtest02;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TimeZone;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_res;
private Button btn_find;
Calendar cal;
String year;
String month;
String day;
String hour;
String minute;
String time;
ArrayList<String> strArr = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_res=findViewById(R.id.btn_res);
btn_find=findViewById(R.id.btn_find);
btn_res.setOnClickListener(this);
btn_find.setOnClickListener(this);
}
public void GetTime(){
cal = Calendar.getInstance();
cal.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
year = String.valueOf(cal.get(Calendar.YEAR));
month = String.valueOf(cal.get(Calendar.MONTH)+1);
day = String.valueOf(cal.get(Calendar.DATE));
if (cal.get(Calendar.AM_PM) == 0)
hour = String.valueOf(cal.get(Calendar.HOUR));
else
hour = String.valueOf(cal.get(Calendar.HOUR)+12);
minute = String.valueOf(cal.get(Calendar.MINUTE));
time="時(shí)間:"+year+"年"+month+"月"+day+"日"+hour+"時(shí)"+minute+"分";
}
@Override
public void onClick(View v) {
GetTime();
if(v.getId()==R.id.btn_res){
strArr.add("時(shí)間:"+time+" 狀態(tài):正常");
}
if(v.getId()==R.id.btn_find){
// 創(chuàng)建一個(gè)新意圖
Intent intent = new Intent();
// 設(shè)置意圖要跳轉(zhuǎn)的活動(dòng)類
intent.setClass(this, ListActivity.class);
intent.putExtra("liebiao",strArr);
// 期望接收下個(gè)頁(yè)面的返回?cái)?shù)據(jù)
startActivityForResult(intent, 0);
}
}
}
ListView:
package com.example.listviewtest02;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class ListActivity extends AppCompatActivity {
private ListView lv_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
lv_data=findViewById(R.id.lv_data);
Intent intent = super.getIntent();
ArrayList<String> strArr = new ArrayList<String>();
strArr = intent.getStringArrayListExtra("liebiao");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
ListActivity.this, android.R.layout.simple_list_item_1, strArr);
lv_data.setAdapter(adapter);
}
}
效果:

image.png
點(diǎn)擊若干次"收到數(shù)據(jù)",模擬服務(wù)器發(fā)送數(shù)據(jù)過(guò)來(lái)。

image.png