首先bean类要实现
implements Serializable类
工具类中代码
package com.example.yinchenglong1229; import android.annotation.SuppressLint; import android.content.Intent; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.google.gson.Gson; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import bean.SuperClass; import url.GetURL; public class Main2Activity extends AppCompatActivity { private TextView tv; private List<SuperClass.DataBean> list = new ArrayList<>(); private Button btn; @SuppressLint("StaticFieldLeak") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); //获取id tv = findViewById(R.id.tv); btn = findViewById(R.id.btn); //使用AsyncTask解析 new AsyncTask<String, String, String>() { @Override protected String doInBackground(String... strings) { String data = GetURL.getData(); return data; } //解析 @Override protected void onPostExecute(String s) { super.onPostExecute(s); Log.e("wzq",s); Gson gson = new Gson(); SuperClass superClass = gson.fromJson(s, SuperClass.class); Toast.makeText(Main2Activity.this,superClass+"",Toast.LENGTH_SHORT).show(); List<SuperClass.DataBean> data = superClass.getData(); list.addAll(data); tv.setText(superClass.getInfo()); } }.execute(); //点击传值 btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Main2Activity.this, Main3Activity.class); Bundle bundle=new Bundle(); bundle.putSerializable("list1", (Serializable) list); intent.putExtra("name1",bundle); startActivity(intent); } }); } }
//布局
<TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="9" android:id="@+id/tv"/> <Button android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="跳转并传值" android:id="@+id/btn"/>
//接受值的类
package com.example.yinchenglong1229; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ListView; import java.io.Serializable; import java.util.List; import adapter.MyAdapter; import bean.SuperClass; public class Main3Activity extends AppCompatActivity { private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); lv = findViewById(R.id.lv); Intent intent = getIntent(); Bundle bundle = intent.getBundleExtra("name1"); List<SuperClass.DataBean> list = (List<SuperClass.DataBean>) bundle. getSerializable("list1"); lv.setAdapter(new MyAdapter(list,Main3Activity.this)); } }