我有一个名为的SearchActivity
活动ListView
来自 JSON 下载的数据子集(标题和距离)。我想在另一个名为ResultsActivity
. 我知道如何从ListView
自身获取数据并将其传递给其他活动,但我不确定如何获取所有数据以传递给第二个活动。
例如,我希望 onClickListener 对当前选定项目的 ArrayList 进行查找,以便我可以执行以下操作:
in.putExtra(TAG_NAME, name); in.putExtra(TAG_STREET, 街道); 等等。
问题是 ListView 中显示的唯一内容是名称和距离。如何使用当前的 Listview 选择进入 ArrayList 条目,以便显示街道?
任何帮助表示赞赏。
SearchActivity.java
package com.chance.squat;
import com.chance.squat.R;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class SearchActivity extends ListActivity {
// url to make request
private static String url = "http://192.168.1.115:3000/bathrooms/nearby.json/?lat=45.580639&lon=-122.677682";
// JSON Node names
private static final String TAG_BATHROOMS = "bathrooms";
private static final String TAG_ID = "ID";
private static final String TAG_ACCESS = "access";
private static final String TAG_CITY = "city";
private static final String TAG_COMMENT = "comment";
private static final String TAG_DIRECTIONS = "directions";
private static final String TAG_NAME = "name";
private static final String TAG_STREET = "street";
private static final String TAG_BATHROOMTYPE = "bathroomtype";
private static final String TAG_DISTANCE = "distance";
private static final String TAG_AVAIL = "avail";
private static final String TAG_COUNTRY = "country";
private static final String TAG_STATE = "state";
private static final String TAG_POSTAL = "postal";
// contacts JSONArray
JSONArray bathrooms = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
// Hashmap for ListView
ArrayList<HashMap<String, String>> bathroomList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
bathrooms = json.getJSONArray(TAG_BATHROOMS);
// looping through All Contacts
for(int i = 0; i < bathrooms.length(); i++){
JSONObject c = bathrooms.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String access = c.getString(TAG_ACCESS);
String city = c.getString(TAG_CITY);
String comment = c.getString(TAG_COMMENT);
String directions = c.getString(TAG_DIRECTIONS);
String name = c.getString(TAG_NAME);
String street = c.getString(TAG_STREET);
String bathroomtype = c.getString(TAG_BATHROOMTYPE);
String distance = c.getString(TAG_DISTANCE);
String distanceTrimmed = distance.substring(0,4) + " " + "miles away";
String avail = c.getString(TAG_AVAIL);
String country = c.getString(TAG_COUNTRY);
String state = c.getString(TAG_STATE);
String postal = c.getString(TAG_POSTAL);
System.out.println(name);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_ACCESS, access);
map.put(TAG_CITY, city);
map.put(TAG_COMMENT, comment);
map.put(TAG_DIRECTIONS, directions);
map.put(TAG_NAME, name);
map.put(TAG_STREET, street);
map.put(TAG_BATHROOMTYPE, bathroomtype);
map.put(TAG_DISTANCE, distanceTrimmed);
map.put(TAG_AVAIL, avail);
map.put(TAG_COUNTRY, country);
map.put(TAG_STATE, state);
map.put(TAG_POSTAL, postal);
// adding HashList to ArrayList
bathroomList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, bathroomList,
R.layout.list_item,
new String[] { TAG_NAME, TAG_DISTANCE}, new int[] {
R.id.name, R.id.distance });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
//String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
//String distance = ((TextView) view.findViewById(R.id.distance)).getText().toString();
// Starting new intent
//Intent in = new Intent(getApplicationContext(), SeachActivity.class);
//in.putExtra(TAG_NAME, name);
//in.putExtra(TAG_DISTANCE, distance);
//startActivity(in);
}
});
}
}