【问题标题】:Android ArrayList, how to get onClick of ListViewAndroid ArrayList,如何获取ListView的onClick
【发布时间】: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


【解决方案1】:

从 main.xml 初始化你的列表视图

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

并添加

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

}

【讨论】:

  • 需要Activity/Fragment 来不必要地实现一个接口。更不用说投射AdapterView 了。这两种情况都可以避免。
  • 我正在使用 - 公共类 LocationActivity 扩展 ListActivity 实现 LocationListener { - 我没有调用 listView = (ListView) findViewById(R.id.listview);。感谢您的反馈
【解决方案2】:

只需覆盖

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

当列表中的一个项目被选中时调用。因为,您正在调用setListAdapter(),我假设您已经扩展了ListActivityListFragment

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

现在,您会意识到使用ArrayAdapter&lt;FoursquareVenue&gt; 而不是ArrayAdapter&lt;String&gt; 会更好,因为使用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);

然后覆盖FoursquareVenue#toString()

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

【讨论】:

  • 你好 Ravi Thapliyal,谢谢。我试试看。我可以得到 listView 项目的位置,但我不能得到列表视图的数据。我已经编辑了我的问题。
  • 使用ListView#getItemAtPosition() 检索列表项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
相关资源
最近更新 更多