为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用ArrayList HashMap将listview onItemClick数据发送到另一个

如何使用ArrayList HashMap将listview onItemClick数据发送到另一个

森林海 2023-11-10 15:54:51
我对 Android 开发相当陌生。我这里有一个应用程序,它应该将内部的值发送staffList到另一个活动。ViewStaffActivity.java.get(position).toString()当我测试分配给是否有任何值时String test,它输出了我单击的正确值。    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                    String test = staffList.get(position).toString();                    Intent intent = new Intent(ViewStaffActivity.this,ProfileCRUD.class);                    intent.putExtra("data",staffList.get(position));                    startActivity(intent);                }intent但是当我使用PROFILE_CRUD.java传递值时,它name: null hashmap size : 4在这一行返回name.setText(hashMap.get("name"));package com.example.activity.AdminModule;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;import com.example.login1.R;import java.util.HashMap;public class ProfileCRUD extends AppCompatActivity {    private TextView name;    private TextView email;    private TextView user_type;    private TextView created_at;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        name = (TextView)findViewById(R.id.nameTextView);        email = (TextView)findViewById(R.id.emailTextView);        user_type = (TextView)findViewById(R.id.userTypeTextView);        created_at = (TextView)findViewById(R.id.createdAtTextView);        /*Intent intent = getIntent();        HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("data");        String lat = hashMap.get("Coord_LAT");        String longi = hashMap.get("Coord_LONG");*/任何帮助,将不胜感激!编辑 在正确解决方案的帮助下修复了它。返回 null 的原因name.setText是因为我没有setContentView布局文件。
查看完整描述

2 回答

?
叮当猫咪

TA贡献1776条经验 获得超12个赞

您可以将 Bundle 中的每个值分开,而不是在 Intent 中传递 HashMap。就像这样:


Intent intent = new Intent(ViewStaffActivity.this,ProfileCRUD.class);

HashMap<String, String> hashMap = staffList.get(position);

Bundle extras = new Bundle();

extras.putString("NAME", hashmap.get("name"));

extras.putString("EMAIL", hashmap.get("email"));

extras.putString("USER_TYPE", hashmap.get("user_type"));

extras.putString("CREATED_AT", hashmap.get("created_at"));

intent.putExtras(extras);

startActivity(intent);

然后在另一个 Activity 的 onCreate() 中您将使用:


Bundle extras = getIntent().getExtras();

String name = extras.get("NAME");

...


查看完整回答
反对 回复 2023-11-10
?
明月笑刀无情

TA贡献1828条经验 获得超4个赞

您可以将 HashMap 转换为 json 并通过 Intent 发送。使用以下代码发送意图:


String jsonStaff = (new Gson()).toJson(staffList.get(position));

intent.putExtra("jsonStaff", jsonStaff);

并专注于另一项活动,如下所示:


String jsonStaff = intent.getStringExtra("jsonStaff");

HashMap<String,String > dataStaff = (new Gson()).fromJson(jsonStaff, new HashMap<String,String >().getClass());

就这些。


查看完整回答
反对 回复 2023-11-10
  • 2 回答
  • 0 关注
  • 65 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信