在MainActivity声明一个动态数组,把数据存在数组,然后通过Intent把整个数组传递到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="收到数据"/>
<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="时间:"+year+"年"+month+"月"+day+"日"+hour+"时"+minute+"分";
}
@Override
public void onClick(View v) {
GetTime();
if(v.getId()==R.id.btn_res){
strArr.add("时间:"+time+" 状态:正常");
}
if(v.getId()==R.id.btn_find){
// 创建一个新意图
Intent intent = new Intent();
// 设置意图要跳转的活动类
intent.setClass(this, ListActivity.class);
intent.putExtra("liebiao",strArr);
// 期望接收下个页面的返回数据
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);
}
}
效果:
点击若干次"收到数据",模拟服务器发送数据过来。