【发布时间】:2015-01-19 07:21:57
【问题描述】:
我正在测试来自foursquare-api的示例代码
我想知道的是,如何获取列表视图的 onClick 项?
所以在获取到场地列表后,如果用户点击列表项,我想将场地名称发送到另一个片段处理。
谢谢
Java 编码
ArrayList<FoursquareVenue> venuesList;
ArrayAdapter<String> myAdapter;
private static ArrayList<FoursquareVenue> parseFoursquare(final String response) {
ArrayList<FoursquareVenue> temp = new ArrayList<FoursquareVenue>();
try {
// make an jsonObject in order to parse the response
JSONObject jsonObject = new JSONObject(response);
// make an jsonObject in order to parse the response
if (jsonObject.has("response")) {
if (jsonObject.getJSONObject("response").has("venues")) {
JSONArray jsonArray = jsonObject.getJSONObject("response").getJSONArray("venues");
for (int i = 0; i < jsonArray.length(); i++) {
FoursquareVenue poi = new FoursquareVenue();
if (jsonArray.getJSONObject(i).has("name")) {
poi.setName(jsonArray.getJSONObject(i).getString("name"));
if (jsonArray.getJSONObject(i).has("location")) {
if (jsonArray.getJSONObject(i).getJSONObject("location").has("address")) {
if (jsonArray.getJSONObject(i).getJSONObject("location").has("city")) {
poi.setCity(jsonArray.getJSONObject(i).getJSONObject("location").getString("city"));
}
if (jsonArray.getJSONObject(i).has("categories")) {
if (jsonArray.getJSONObject(i).getJSONArray("categories").length() > 0) {
if (jsonArray.getJSONObject(i).getJSONArray("categories").getJSONObject(0).has("icon")) {
poi.setCategory(jsonArray.getJSONObject(i).getJSONArray("categories").getJSONObject(0).getString("name"));
}
}
}
temp.add(poi);
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
return new ArrayList<FoursquareVenue>();
}
return temp;
}
@Override
protected void onPostExecute(String result) {
if (temp == null) {
// we have an error to the call
// we can also stop the progress bar
} else {
// all things went right
// parseFoursquare venues search result
venuesList = (ArrayList<FoursquareVenue>) parseFoursquare(temp);
List<String> listTitle = new ArrayList<String>();
for (int i = 0; i < venuesList.size(); i++) {
// make a list of the venus that are loaded in the list.
// show the name, the category and the city
listTitle.add(i, venuesList.get(i).getName() + ", " + venuesList.get(i).getCategory() + "" + venuesList.get(i).getCity());
}
// set the results to the list
// and show them in the xml
myAdapter = new ArrayAdapter<String>(LocationActivity.this, R.layout.row_layout, R.id.listText, listTitle);
setListAdapter(myAdapter);
}
}
谢谢
我已经试过了:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Toast.makeText(getApplicationContext(), "position => " + position +
" - ListView =>" + l +
" - View => " + v +
" - id => " + id
, Toast.LENGTH_LONG).show();
}
可以得到listViewItem的位置,但是不能得到listview的数据。
我也在用这个:
public class FoursquareVenue {
private String name;
private String city;
private String category;
public FoursquareVenue() {
this.name = "";
this.city = "";
this.setCategory("");
}
public String getCity() {
if (city.length() > 0) {
return city;
}
return city;
}
public void setCity(String city) {
if (city != null) {
this.city = city.replaceAll("\\(", "").replaceAll("\\)", "");
;
}
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
【问题讨论】:
-
那些带有
getJSONObject的嵌套if看起来很糟糕。我希望这个方法不会被调用很多。
标签: java android api arraylist foursquare