我有一個活動,名爲SearchActivity
,其中ListView
包含JSON下載的一部分數據(標題和距離)。我想顯示所有JSON數據並在另一個名爲ResultsActivity
的活動中完成記錄。我知道如何從ListView
本身獲取數據並將其傳遞給其他活動,但我不確定如何獲取所有數據以傳遞到第二個活動。Android:使用ListView時在活動之間傳遞數據的最佳方式
例如,我想爲onClickListener做一個查找到ArrayList中當前所選的項目,這樣我可以做這樣的事情:
in.putExtra(TAG_NAME,姓名); in.putExtra(TAG_STREET,street); in.putExtra(TAG_STREET,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);
}
});
}
}
所以你要到一個特定的記錄發送到下一個活動或完整的ArrayList?你可以發送這兩個,如果你想要 – noob
我只想發送一個特定的記錄,即一個由被選中的ListView選擇引用的那個,但我不知道如何使用ListView獲得對ArrayList的引用? –