简体   繁体   English

ArrayAdapter:创建显示自定义类型的toString()方法中的文本的ArrayAdapter的最佳方法是什么?

[英]ArrayAdapter: What is the best way to approach creating an ArrayAdapter that would display the text from the toString() method of a custom type?

I was going to create an ArrayList of GPS coordinates that are saved in a custom object. 我打算创建一个保存在自定义对象中的GPS坐标的ArrayList。 I then want to display a list of these GPS coordinates. 然后,我想显示这些GPS坐标的列表。 My current solution is to create a parallel ArrayList of strings that essentially are the GPS.toString() results. 我当前的解决方案是创建一个字符串的并行ArrayList,该字符串本质上是GPS.toString()结果。

This seems like a complicated bit of extra work that might be able to be avoided and simplified. 这似乎是一件繁琐的额外工作,可以避免和简化。 Can anybody think of a better way to do this that doesn't require an awkwardly large amount of work. 谁能想到一种不需要繁琐工作的更好的方法。 I looked a little into creating a custom ArrayAdapter but I'm not sure I fully understand how to convert the GPS coordinate object into something useful for the ListView. 我花了一点时间来创建自定义ArrayAdapter,但是不确定我是否完全理解如何将GPS坐标对象转换为对ListView有用的东西。 If you think this is a better solution, could you perhaps give a little insight into how to use this properly? 如果您认为这是一个更好的解决方案,是否可以对如何正确使用此方法提供一些见解? (even if it's just a link) (即使只是一个链接)

Thanks in advance! 提前致谢! Matt 马特

如果您不希望沿这些坐标显示一些奇特的东西,也许应该坚持使用GPS.toString()的ArrayList随附的默认ArrayAdapter。

Why don't you override the toString() method in the custom object, then just iterate over them from your array? 为什么不覆盖自定义对象中的toString()方法,然后仅从数组中对其进行迭代?

@Override
public String toString() {
     return "Latitude: " + this.latitude; //....;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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