Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓ArrayList,如何点击ListView_Java_Android_Api_Arraylist_Foursquare - Fatal编程技术网

Java 安卓ArrayList,如何点击ListView

Java 安卓ArrayList,如何点击ListView,java,android,api,arraylist,foursquare,Java,Android,Api,Arraylist,Foursquare,我正在测试来自的示例代码 我想知道的是,如何获取列表视图的onClick项 因此,在获取场馆列表后,如果用户单击列表项,我想将avenue名称发送到另一个片段来处理它 谢谢 Java编码 ArrayList<FoursquareVenue> venuesList; ArrayAdapter<String> myAdapter; private static ArrayList<FoursquareVenue> parseFoursquare(fina

我正在测试来自的示例代码

我想知道的是,如何获取列表视图的onClick项

因此,在获取场馆列表后,如果用户单击列表项,我想将avenue名称发送到另一个片段来处理它

谢谢

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);
    }
}
我可以获取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;
    }

}
只需覆盖

onListItemClick(ListView l, View v, int position, long id)
当选择列表中的项目时调用。因为,您正在调用
setListAdapter()
我假设您已经扩展了
ListActivity
ListFragment

要检索
ListView
数据,请使用
ListView#getItemAtPosition()
方法

现在,在这里,您会意识到使用
ArrayAdapter
而不是
ArrayAdapter
会更好,因为使用
String
版本,您可以使用
getItemAtPosition()
检索的所有内容都与传递的字符串完全相同

listTitle.add(i, venuesList.get(i).getName() + ", " +
    venuesList.get(i).getCategory() + "" + venuesList.get(i).getCity());
这显然不是很灵活。您应该将
venuesList
直接传递到适配器

myAdapter = new ArrayAdapter<FoursquareVenue>(
    LocationActivity.this, R.layout.row_layout, R.id.listText, venuesList);

从main.xml初始化listview

listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(this);    

 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

}
public void onItemClick(适配器视图arg0,视图arg1,内部位置,长arg3){
}

那些嵌套的if中带有
getJSONObject
的看起来很糟糕。我希望这种方法不会被称为太多。你好,拉维·塔普利亚尔,谢谢。我试试看。我可以获取listView项的位置,但无法获取列表视图的数据。我已编辑了我的问题。请使用
ListView#getItemAtPosition()
检索列表项。需要
活动
/
片段
来不必要地实现接口。更不用说铸造
适配器视图了。这两种情况都可以避免。我使用的是-public类LocationActivity扩展ListActivity实现LocationListener{-我不是调用listView=(listView)findViewById(R.id.listView);。感谢您的反馈
listView = (ListView) findViewById(R.id.listview);
listView.setOnItemClickListener(this);    
 public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

}