选定的listitem通过json在另一个活动中显示它们的详细信息后?
Posted
技术标签:
【中文标题】选定的listitem通过json在另一个活动中显示它们的详细信息后?【英文标题】:After selected listitem shows the details of them on another activity through json? 【发布时间】:2013-04-06 01:47:17 【问题描述】:我有显示列表项的活动,并且在列表项中我只想显示地点的名称并且我能够显示它,但是当我单击我想要与该地点名称相关的项目时,它会显示有关另一项活动的详细信息,即位置详细信息。所以请帮我解决我的问题,我是 android 新手。
这是我的两个活动代码:
locationList=( ListView)findViewById(R.id.list_Surroundings);
// Hashmap for ListView
ArrayList<HashMap<String, String>> locationOfList = new ArrayList<HashMap<String, String>>();
adapter = new SimpleAdapter(this, locationOfList,
R.layout.list_item1,
new String[] TAG_NAME , new int[]
R.id.name);
locationList.setAdapter(adapter);
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try
// Getting Array of Category
location=json.getJSONArray(TAG_LOCATION);
//looping through all categories
for(int i = 0; i < location.length(); i++)
JSONObject c = location.getJSONObject(i);
JSONObject c1=c.getJSONObject(TAG_LOCATION1);
// Storing each json item in variable
String LocationID = c1.getString(TAG_LOCATION_ID);
String Name = c1.getString(TAG_NAME);
String Phone = c1.getString(TAG_PHONE);
String FormattedPhone = c1.getString(TAG_FORMATTED_PHONE);
String Address = c1.getString(TAG_ADDRESS);
String CrossStreet = c1.getString(TAG_CROSS_STREET);
String Lat = c1.getString(TAG_LAT);
String Lng = c1.getString(TAG_LNG);
String Distance = c1.getString(TAG_DISTANCE);
String PostalCode = c1.getString(TAG_POSTAL_CODE);
String City = c1.getString(TAG_CITY);
String State = c1.getString(TAG_STATE);
String Country = c1.getString(TAG_COUNTRY);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_LOCATION_ID, LocationID);
map.put(TAG_NAME, Name);
map.put(TAG_PHONE, Phone);
map.put(TAG_FORMATTED_PHONE, FormattedPhone);
map.put(TAG_ADDRESS, Address);
map.put(TAG_CROSS_STREET, CrossStreet);
map.put(TAG_LAT, Lat);
map.put(TAG_LNG, Lng);
map.put(TAG_DISTANCE, Distance);
map.put(TAG_POSTAL_CODE, PostalCode);
map.put(TAG_CITY, City);
map.put(TAG_STATE, State);
map.put(TAG_COUNTRY, Country);
// adding HashList to ArrayList
locationOfList.add(map);
catch (Exception e)
// TODO: handle exception
e.printStackTrace();
locationList.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
// TODO Auto-generated method stub
// getting values from selected ListItem
String Name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String Distance = ((TextView) view.findViewById(R.id.distance_list1)).getText().toString();
String Country = ((TextView) view.findViewById(R.id.country_list1)).getText().toString();
String City = ((TextView) view.findViewById(R.id.city_list1)).getText().toString();
String Phone = ((TextView) view.findViewById(R.id.phone_list1)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), LocationDetails.class);
in.putExtra(TAG_NAME, Name);
in.putExtra(TAG_DISTANCE, Distance);
in.putExtra(TAG_COUNTRY, Country);
in.putExtra(TAG_CITY, City);
in.putExtra(TAG_PHONE, Phone);
startActivity(in);
);
public class LocationDetails extends Activity
// JSON node keys
private static final String TAG_NAME = "Name";
private static final String TAG_PHONE = "Phone";
private static final String TAG_ADDRESS = "Address";
private static final String TAG_DISTANCE= "Distance";
private static final String TAG_CITY = "City";
private static final String TAG_COUNTRY= "Country";
private TextView name,phone,address,distance,city,country;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.location_details);
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String Name = in.getStringExtra(TAG_NAME);
String Phone = in.getStringExtra(TAG_PHONE);
String Address = in.getStringExtra(TAG_ADDRESS);
String Distance = in.getStringExtra(TAG_DISTANCE);
String City = in.getStringExtra(TAG_CITY);
String Country = in.getStringExtra(TAG_COUNTRY);
// Displaying all values on the screen
name= (TextView) findViewById(R.id.TxtView_name);
phone=(TextView)findViewById(R.id.TxtView_PhoneField);
address=(TextView)findViewById(R.id.TxtView_AddressField);
distance=(TextView)findViewById(R.id.TxtView_DistanceField);
city=(TextView)findViewById(R.id.TxtView_CityField);
country=(TextView)findViewById(R.id.TxtView_CountryField);
name.setText(Name);
phone.setText(Phone);
address.setText(Address);
distance.setText(Distance);
city.setText(City);
country.setText(Country);
【问题讨论】:
你遇到了什么问题? 我写在顶部实际上我可以获取列表视图项目,但是当我点击它时,我希望与该名称相关,它会显示电话号码等详细信息。 ,距离,所有国家/地区。 【参考方案1】:在发送活动时,尝试替换
in.putExtra(TAG_NAME, Name);
in.putExtra(TAG_DISTANCE, Distance);
in.putExtra(TAG_COUNTRY, Country);
in.putExtra(TAG_CITY, City);
in.putExtra(TAG_PHONE, Phone);
与
Bundle extras = new Bundle();
extras.putString(TAG_NAME, Name);
extras.putString(TAG_DISTANCE, Distance);
extras.putString(TAG_COUNTRY, Country);
extras.putString(TAG_CITY, City);
extras.putString(TAG_PHONE, Phone);
extras.putExtras(extras);
在接收活动中,尝试替换
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
String Name = in.getStringExtra(TAG_NAME);
String Phone = in.getStringExtra(TAG_PHONE);
String Address = in.getStringExtra(TAG_ADDRESS);
String Distance = in.getStringExtra(TAG_DISTANCE);
String City = in.getStringExtra(TAG_CITY);
String Country = in.getStringExtra(TAG_COUNTRY);
与
Bundle extras = getIntent().getExtras();
if(extras != null)
String Name = extras.getString(TAG_NAME);
String Phone = extras.getString(TAG_PHONE);
String Address = extras.getString(TAG_ADDRESS);
String Distance = extras.getString(TAG_DISTANCE);
String City = extras.getString(TAG_CITY);
String Country = extras.getString(TAG_COUNTRY);
【讨论】:
【参考方案2】:您正在尝试从 TextViews 中获取数据,这完全不对。
您需要在 listItem 的 onClick() 中使用 locationOfList(position)
从 ArrayList
中获取所选项目的值。
类似这样的:
locationList.setOnItemClickListener(new OnItemClickListener()
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
// TODO Auto-generated method stub
// getting values from selected ListItem
HashMap<String, String> selectedMap = locationOfList.get(position);
// Starting new intent
Intent in = new Intent(getApplicationContext(), LocationDetails.class);
in.putExtra(selectedMap);
startActivity(in);
);
在您的第二个 Activity 中,使用 this example 从 HashMap 中检索数据。
【讨论】:
我不明白你是否能够做你试图向我解释的事情,那么我感谢你@swayam 在您的代码中,您试图通过使用 TextViews 的内容来获取已单击项目的数据。那是错的。您需要识别已单击的元素,然后查看您的 arrayList 以找到该位置的地图并从那里提取信息。我希望我说得通。 如果我知道该怎么做,我不会在这里问你是否给我一个代码,你可以解释我在上面的代码中所做的更改,那么它会更好。 当我使用您的代码时,它在 locationOfList 上显示一个错误,“无法引用以不同方法定义的内部类中的非最终变量 locationOfList”,所以下一步该做什么。 @swayam 是的,那是因为您已经在 onCreate() 中声明了它。将其声明为全局类变量,即不在任何函数内。以上是关于选定的listitem通过json在另一个活动中显示它们的详细信息后?的主要内容,如果未能解决你的问题,请参考以下文章
获取 ListItem ContextMenu 中的选定项目