gpt4 book ai didi

java - 如何将 json ListView 上选定项目的值发送到另一个 Activity ?

转载 作者:行者123 更新时间:2023-12-02 13:40:19 25 4
gpt4 key购买 nike

我正在制作一个应用程序,它从 JSON 获取国家/地区数据(名称、纬度、经度等)并创建一个 listview,其中每个项目都是一个不同的国家。

该部分正在工作,但是每次我单击一个项目时,它都会打开 MapActivity, map 以该国家/地区为中心。问题是我无法将坐标从 MainActivity 发送到 MapsActivity。

public class TodasAsCategorias extends AppCompatActivity {

private String TAG = TodasAsCategorias.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
private static String url = "http://*************/api/continent/any/country/all?id=siF1uXXEsltXOi5CWlSIzy7EABlnE5iF33bnNmfAHJiYXYNmjY";
ArrayList<HashMap<String, String>> listaPaises;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_todas_as_categorias);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Categorias");

listaPaises = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetPaises().execute();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
menu.findItem(R.id.spinner_cat).setVisible(false);
menu.findItem(R.id.spinner_pais).setVisible(false);
return true;
}

private class GetPaises extends AsyncTask<Void, Void, Void> implements Serializable {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(TodasAsCategorias.this);
pDialog.setMessage("Aguarde...");
pDialog.setCancelable(false);
pDialog.show();
}

@Override
protected Void doInBackground(Void... params) {
HttpHandler sh = new HttpHandler();
final String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from URL: " + jsonStr);
if (jsonStr != null) {
try {
JSONArray array = new JSONArray(jsonStr);
for (int i = 0; i < array.length(); i++) {
JSONObject jsonObject = array.getJSONObject(i);
JSONArray paises = jsonObject.optJSONArray("paises");
if (paises != null) {
for (int j = 0; j < paises.length(); j++) {
JSONObject jsonObject1 = paises.getJSONObject(j);

String K_PAIS = jsonObject1.getString("K_PAIS");
String Designacao = jsonObject1.getString("Designacao");
String URL_IMAGE_SMALL = jsonObject1.getString("URL_IMAGE_SMALL");
String Coord_LAT = jsonObject1.getString("Coord_LAT");
String Coord_LONG = jsonObject1.getString("Coord_LONG");
String Coord_Zoom = jsonObject1.getString("Coord_Zoom");

HashMap<String, String> pais = new HashMap<>();

pais.put("K_PAIS", K_PAIS);
pais.put("Designacao", Designacao);
pais.put("URL_IMAGE_SMALL", URL_IMAGE_SMALL);
pais.put("Coord_LAT", Coord_LAT);
pais.put("Coord_LONG", Coord_LONG);
pais.put("Coord_Zoom", Coord_Zoom);

listaPaises.add(pais);
}
}
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Json parsin 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 errpr!", Toast.LENGTH_LONG).show();
}
});
}
return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing()) {
pDialog.dismiss();
}
ListAdapter adapter = new SimpleAdapter(TodasAsCategorias.this, listaPaises, R.layout.list_item, new String[]{ "Designacao","Coord_LAT", "Coord_LONG", "Coord_Zoom"},
new int[]{R.id.Designacao,R.id.Lat, R.id.Long, R.id.Zoom});
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> pare, View view, int position, long id) {
Intent intent = new Intent(TodasAsCategorias.this, MapsActivity.class);

startActivity(intent);
}
});
}
}
}

最佳答案

HashMap实现了 Serialized,因此我们可以使用 putExtra 发送 HashMap 对象并使用 getSerializableExtra 接收它

TodasAsCategorias Activity

  @Override
public void onItemClick(AdapterView<?> pare, View view, int position, long id)
{
Intent intent = new Intent(TodasAsCategorias.this, MapsActivity.class);
intent.putExtra("data", listaPaises.get(position));
startActivity(intent);
}

MapsActivity

protected void onCreate(Bundle bundle) {
super.onCreate(savedInstanceState);

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

关于java - 如何将 json ListView 上选定项目的值发送到另一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42769479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com