[英]How to send values from a selected item on a json listview to another activity?
我正在制作一個從JSON
獲取國家/地區數據(名稱,緯度,經度,...)並創建listview
,其中每個項目都是一個不同的國家/地區。
該部分正在運行,但是,每次我單擊某個項目時,它都會打開MapActivity,地圖以該國家/地區為中心。 問題是我無法將坐標從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
實現Serializable
,所以我們可以把HashMap
使用對象putExtra
和使用接收它getSerializableExtra
TodasAsCategorias
活動
@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");
}
通過Extras將數據傳輸到MapsActivity。 開始這樣的活動:
Intent intent = new Intent(TodasAsCategorias.this, MapsActivity.class);
intent.putExtra("Coord_LAT", value);
intent.putExtra("Coord_LONG", value);
startActivity(intent);
並在MapsActivity中檢索數據:
String Coord_LAT = getIntent().getStringExtra("Coord_LAT");
String Coord_LONG = getIntent().getStringExtra("Coord_LONG");
onItemClick()方法提供了被單擊的列表項的“位置”。 該位置處的項目包含您傳遞給其他活動的緯度和經度,我猜是這樣。 然后,可以使用Extras按照其他答案的說明傳遞緯度和經度。
更新:由於您的listaPaises是一個實例變量,因此@PavneetSingh答案會更直接。
您可以重寫SimpleAdapter的getItem(int position)以返回HashMap條目:
ListAdapter adapter = new SimpleAdapter(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}) {
@Override
public Object getItem(int position) {
if (listaPaises!=null) {
return listaPaises.get(position);
}
return super.getItem(position);
}
};
然后在ListView的setOnItemClickListener(...)方法中,可以通過調用以下命令獲取返回的條目:
HashMap<String, String> pais = (HashMap<String, String>) adapter.getItem(position)
並將帶有Intent的putExtra()方法的HashMap條目傳遞給MapsActivity。
請注意,為了在匿名內部類中調用適配器,您需要將局部變量ListAdapter適配器更改為類的實例變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.