如何使用ArrayAdapter的自定义对象的机器人 [英] how to use an ArrayAdapter in android of custom objects

查看:161
本文介绍了如何使用ArrayAdapter的自定义对象的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能在一个列表视图中使用自定义对象的属性。如果我实现一个ArrayAdapter获得一个字符串列表会显示罚款列表视图,但是当我使用自定义对象的列表,它只是输出内存地址。

How can I use the properties of a custom object in a Listview. If I implement an ArrayAdapter with a list of Strings it displays fine in Listview but when I use a list of custom objects, it just outputs the memory address.

在code我到现在:

ArrayList<CustomObject> allObjects = new ArrayList<>();

allObjects.add("title", "http://url.com"));


  ArrayAdapter<NewsObject> adapter = new ArrayAdapter<NewsObject>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, allNews);


        // Assign adapter to ListView
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Uri uri = Uri.parse( "http://www.google.com" );
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
            }
        });

还有一个类似的问题<一href="http://stackoverflow.com/questions/11593574/adding-a-custom-object-to-an-arrayadapter-how-to-grab-data">here但是这不是我所需要的,因为我只需要拥有的称号显示在列表视图中,当他们点击该URL解压。

There is a similar question here but that is not what I need since I just need to have the title show in list view and when they click extract the url.

推荐答案

这是ArrayAdapter显示由的toString()方法返回的值,所以你需要重写此在自定义对象类方法返回所需的字符串。您还需要有至少一个getter方法​​的URL,这样你就可以检索在单击事件。

An ArrayAdapter displays the value returned by the toString() method, so you will need to override this method in your custom Object class to return the desired String. You will also need to have at least a getter method for the URL, so you can retrieve that in the click event.

public class NewsObject {
    private String title;
    private String url;

    public NewsObject(String title, String url) {
        this.title = title;
        this.url = url;
    }

    public String getUrl() {
        return url;
    }

    @Override
    public String toString() {
        return title;
    }
    ...
}

onItemClick()方法,位置将是该指数对应于您的自定义对象中的ArrayList列表中单击的项目。检索URL,分析它,并调用 startActivity()

In the onItemClick() method, position will be the index in the ArrayList of your custom Objects corresponding to the list item clicked. Retrieve the URL, parse it, and call startActivity().

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            NewsObject item = allNews.get(position);
            String url = item.getUrl();
            Uri uri = Uri.parse(url);
            startActivity(new Intent(Intent.ACTION_VIEW, uri));
        }
    });

请注意,我认为你的自定义类是 NewsObject ,因为这是发生了什么与你的适配器示例中使用。

Please note, I assumed your custom class is NewsObject, as that is what's used with your Adapter example.

这篇关于如何使用ArrayAdapter的自定义对象的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆