简体   繁体   中英

Android ArrayList, how to get onClick of ListView

I was testing the sample code from foursquare-api

What I would like to know, How can I get the onClick item for the list view ?

so after the get the list of venue, If the user click on the list item, I want to send the avenue name to another fragment to handle it.

thanks

Java Coding

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);
    }
}

Thanks

I have tried this :

 @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();
 }

I can get the position of the listView Item, But I can not the the data of the list view.

I am also using this:

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;
    }

}

Just override

onListItemClick(ListView l, View v, int position, long id)

which is called when an item in the list is selected. Since, you are invoking setListAdapter() I'm assuming you've extended either ListActivity or ListFragment .

To retrieve the ListView data use ListView#getItemAtPosition() method.

Now, here's where you would realise that using an ArrayAdapter<FoursquareVenue> instead of ArrayAdapter<String> would have been better because with the String version, all you would be able to retrieve with getItemAtPosition() , is exactly the same string that you passed at

listTitle.add(i, venuesList.get(i).getName() + ", " +
    venuesList.get(i).getCategory() + "" + venuesList.get(i).getCity());

which is clearly not very flexible. You should pass your venuesList directly to the adapter as

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

and then override FoursquareVenue#toString()

public String toString() {
    return new StringBuilder(name).append(", ")
           .append(category).append(", ").append(city).toString();
}

init your listview from main.xml

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

and add

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM