gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:56:20 26 4
gpt4 key购买 nike

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

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);
}
});

}

}



}


当我通过将 .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");*/

Intent intent = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>)intent.getSerializableExtra("data");

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

最佳答案

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

关于java - 如何使用ArrayList HashMap将listview onItemClick数据发送到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58438542/

26 4 0