我正在将数据从Json对象设置为MyData类,并添加到ArrayList
,并希望将此ArrayList
发送到另一个Activity
,以在RecyclerView
上显示数据。 / p>
我尝试使用parcelable
,但它对我不起作用。
public class MyData implements Parcelable {
private String name,password;
public MyData() {
this.name = name;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
protected MyData(Parcel in) {
name = in.readString();
password = in.readString();
}
public static final Creator<MyData> CREATOR = new Creator<MyData>() {
@Override
public MyData createFromParcel(Parcel in) {
return new MyData(in);
}
@Override
public MyData[] newArray(int size) {
return new MyData[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(password);
}
}
Mydata Calss的此部分与Getter和Setter一起
class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private Context context;
private List<MyData> myData;
public CustomAdapter(Context context, List<MyData> myData) {
this.context = context;
this.myData = myData;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_inflater, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.name.setText(myData.get(position).getName());
holder.pass.setText(myData.get(position).getPassword());
}
@Override
public int getItemCount() {
return myData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name,pass;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name_txt);
pass = (TextView) itemView.findViewById(R.id.pass_txt);
}
}
} //我的适配器Plz Hlep
MainActivity1
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data_list = new ArrayList<>();
}
public void GoOnClick(View view) {
load_from_data();
Intent intent = new Intent(MainActivity.this , Main2Activity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("key", (ArrayList<? extends Parcelable>) data_list);
intent.putExtras(bundle);
startActivity(intent);
}
private void load_from_data()
{
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, get_info_url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("users");
for (int i = 0 ; i < jsonArray.length() ; i++)
{
JSONObject users = jsonArray.getJSONObject(i);
MyData myData = new MyData();
myData.setName(users.getString("name"));
myData.setPassword(users.getString("password"));
data_list.add(myData);
}
} catch (Exception e) {}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//setrecyclerview(data_list);
requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(jsonObjectRequest);
}
//获取Json Object数据的方法
MainActivity2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mainList = new ArrayList<>();
mainList = this.getIntent().getExtras().getParcelableArrayList("key");
setrecyclerview(mainList);
}
private void setrecyclerview(List<MyData> data_list) {
CustomAdapter adapter = new CustomAdapter(this, data_list);
DividerItemDecoration divider = new DividerItemDecoration(recyclerView.getContext(), new LinearLayoutManager(this).getOrientation());
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.addItemDecoration(divider);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
}
//在Recycler视图上设置数据,但不起作用:(
06-07 17:04:30.190 6255-6255 / com.example.bat.ivolleyrecycler E / Android运行时:致命异常:主要 流程:com.example.bat.ivolleyrecycler,PID:6255 java.lang.IllegalStateException:无法执行android:onClick的方法 在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) 在android.view.View.performClick(View.java:4780) 在android.view.View $ PerformClick.run(View.java:19866) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5293) 在java.lang.reflect.Method.invoke(本机方法) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 造成原因:java.lang.reflect.InvocationTargetException 在java.lang.reflect.Method.invoke(本机方法) 在java.lang.reflect.Method.invoke(Method.java:372) 在android.support.v7.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 在android.view.View.performClick(View.java:4780) 在android.view.View $ PerformClick.run(View.java:19866) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:135) 在android.app.ActivityThread.main(ActivityThread.java:5293) 在java.lang.reflect.Method.invoke(本机方法) 在java.lang.reflect.Method.invoke(Method.java:372) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 原因:java.lang.ClassCastException:无法将java.util.ArrayList强制转换为android.os.Parcelable 在com.example.bat.ivolleyrecycler.MainActivity.GoOnClick(MainActivity.java:55)
//错误
我希望在MainActivity1中单击“打开”按钮时在MainActivity2上的Recycler View上向我显示数据
答案 0 :(得分:0)
尝试这样:
ArrayList<MyData> list = new ArrayList<MyData>();
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
Bundle args = new Bundle();
args.putSerializable("myList",(Serializable)list);
intent.putExtra("myKey",args);
startActivity(intent);
将数组列表放入MainActivity.java
中,如下所示:
Bundle args = getIntent().getBundleExtra("myKey");
ArrayList<MyData> newList = (ArrayList<MyData>) args.getSerializable("myList");
答案 1 :(得分:0)
Gson gson = new Gson;
//this code to send data via intent
String json = new Gson().toJson(yourList);
Intent intent = new Intent(MainActivity.this , Main2Activity.class);
intent.putExtra("key",json);
startActivity(intent);
//this code to get data from intent
mainList = new ArrayList<>();
mainList = gson.fromJson(getIntent().getStringExtra("key"), new TypeToken<List<MyData>>() {
}.getType());
答案 2 :(得分:0)
您可以在线在jsonpojo中创建JSON数据的模型,然后重新设计模型类。 http://www.jsonschema2pojo.org/。然后它将起作用。
Intent intent = new Intent(context, Activity.class);
intent.putExtra("A", ArrayList.);
context.startActivity(intent);
在下一个活动中
if(getIntent().getParcelableExtra("A") != null){ list = getIntent().getParcelableExtra("A");}
希望有帮助
答案 3 :(得分:0)
使用putExtra代替putParcelableArrayList,而使用getParcelableExtra代替getParcelableArrayList。
传递数据:
intent.putExtra(key, list)
获取数据:
list=getIntent().getParcelableExtra(key)