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

我对 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布局文件。


森林海
浏览 73回答 2
2回答

叮当猫咪

您可以将 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");...

明月笑刀无情

您可以将 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());就这些。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java