亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用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 關注
  • 209 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號