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

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

问题描述

如何在 Listview 中使用自定义对象的属性.如果我实现一个带有字符串列表的 ArrayAdapter,它在 Listview 中显示良好,但是当我使用自定义对象列表时,它只输出内存地址.

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.

我到现在为止的代码:

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

这里有一个类似的问题 但这不是我需要的,因为我只需要在列表视图中显示标题,当他们点击提取 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() 方法返回的值,因此您需要在自定义 Object 类中覆盖此方法返回所需的字符串.您还需要至少有一个用于 URL 的 getter 方法,以便您可以在点击事件中检索该方法.

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() 方法中,position 将是与单击的列表项对应的自定义对象的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,因为这与您的 Adapter 示例一起使用.

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

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

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