Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用ArrayList HashMap将listview onItemClick数据发送到另一个活动_Java_Android - Fatal编程技术网

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

Java 如何使用ArrayList HashMap将listview onItemClick数据发送到另一个活动,java,android,Java,Android,我对android开发相当陌生。我这里有一个应用程序,它应该将staffList中的值发送到另一个活动 ViewStaffActivity.java package com.example.activity.AdminModule; import androidx.appcompat.app.AppCompatActivity; import android.app.ProgressDialog; import android.content.Intent; import android.o

我对android开发相当陌生。我这里有一个应用程序,它应该将
staffList
中的值发送到另一个活动

ViewStaffActivity.java

package com.example.activity.AdminModule;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.helper.HttpHandler;
import com.example.login1.AppConfig;
import com.example.login1.MainActivity;
import com.example.login1.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class ViewStaffActivity extends AppCompatActivity {

    private String TAG = ViewStaffActivity.class.getSimpleName();

    private ProgressDialog pDialog;
    private ListView lv;


    ArrayList<HashMap<String,String >> staffList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_staff);

        staffList = new ArrayList<>();

        lv = (ListView)findViewById(R.id.staffListView);


        new GetStaff().execute();



    }


    private class GetStaff extends AsyncTask<Void,Void,Void> {

        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(ViewStaffActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(AppConfig.URL_RETRIEVE_STAFF);

            Log.e(TAG, "Response from url: " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr.substring(jsonStr.indexOf("{"), jsonStr.lastIndexOf("}") + 1));

                    // Getting JSON Array node
                    JSONArray staffArray = jsonObj.getJSONArray("user");

                    // looping through All Staff
                    for (int i = 0; i < staffArray.length(); i++) {
                        JSONObject c = staffArray.getJSONObject(i);

                        String name = c.getString("name");
                        String email = c.getString("email");
                        String created_at = c.getString("created_at");
                        String user_type = c.getString("user_type");


                        // tmp hash map for single staff
                        HashMap<String, String> Staff = new HashMap<>();

                        // adding each child node to HashMap key => value
                        Staff.put("name", name);
                        Staff.put("email", email);
                        Staff.put("user_type", user_type);
                        Staff.put("created_at", created_at);

                        // adding contact to contact list
                        staffList.add(Staff);
                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();
                        }
                    });

                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Couldn't get json from server. Check LogCat for possible errors!",
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }

            return null;
        }
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    ViewStaffActivity.this, staffList,
                    R.layout.list_item, new String[]{"name", "email",
                    "created_at","user_type"}, new int[]{R.id.name,
                    R.id.email, R.id.created_at,R.id.user_type});

            lv.setAdapter(adapter);

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                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.setText(hashmap.get(“name”))返回
name:null hashmap size:4

package com.example.activity.AdminModule;
导入androidx.appcompat.app.appcompat活动;
导入android.content.Intent;
导入android.os.Bundle;
导入android.widget.TextView;
导入com.example.login1.R;
导入java.util.HashMap;
公共类ProfileCRUD扩展了AppCompatActivity{
私有文本视图名称;
私人文本查看电子邮件;
私有文本视图用户类型;
在上创建的私有文本视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
name=(TextView)findViewById(R.id.nameTextView);
email=(TextView)findViewById(R.id.emailTextView);
用户类型=(TextView)findViewById(R.id.userTypeTextView);
created_at=(TextView)findViewById(R.id.createdAtTextView);
/*Intent=getIntent();
HashMap HashMap=(HashMap)intent.getSerializableExtra(“数据”);
String lat=hashMap.get(“Coord_lat”);
字符串longi=hashMap.get(“Coord_LONG”)*/
Intent=getIntent();
HashMap HashMap=(HashMap)intent.getSerializableExtra(“数据”);
name.setText(hashMap.get(“name”));
email.setText(hashMap.get(“email”);
user_type.setText(hashMap.get(“user_type”));
created_at.setText(hashMap.get(“created_at”);
任何帮助都将不胜感激

编辑
在正确解决方案的帮助下修复了它。
name.setText
返回null的原因是我的布局文件没有
setContentView

您可以将每个值拆分成一个包,而不是传递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);

您可以将HashMap转换为json并通过intent发送。使用下面的代码发送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());
String jsonStaff=intent.getStringExtra(“jsonStaff”);
HashMap dataStaff=(new Gson()).fromJson(jsonStaff,new HashMap().getClass());
仅此而已

Bundle extras = getIntent().getExtras();
String name = extras.get("NAME");
...
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());