- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这里遵循一个很棒的编码示例:This SO question .它是关于实现数组适配器的 SectionIndexer 接口(interface)。
但是,如果您的 ArrayAdapter 传递的是 ArrayList< MyObject > 而不是 ArrayList< String >,您将如何做同样的事情?
例如,这是我的代码与他的代码不同的地方。他有:
class AlphabeticalAdapter extends ArrayAdapter<String> implements SectionIndexer {
private HashMap<String, Integer> alphaIndexer;
private String[] sections;
public AlphabeticalAdapter(Context c, int resource, List<String> data) {
alphaIndexer = new HashMap<String, Integer>();
for (int i = 0; i < data.size(); i++) {
String s = data.get(i).substring(0, 1).toUpperCase();
alphaIndexer.put(s, i);
}
// other stuff
}
我在根据我的情况调整 for 循环时遇到问题。我不能像他那样量尺寸。在他有以上内容的地方,我的适配器开始于。
public class CustomAdapter extends ArrayAdapter<Items> implements
SectionIndexer {
public ItemAdapter(Context context, Items[] objects) {
在他传递一个 ArrayList 的地方,我必须传递三个,但要做到这一点,必须包装在一个自定义对象类中。我要排序的 ArrayLists 之一是名为“名称”的类中的三个字段之一。明明是字符串。
我想根据该名称字段使用 SectionIndex 按字母顺序滚动浏览。如何更改其他问题中的示例代码以在这种情况下工作?
他有“data.size()”,我需要类似“name.size()”的东西——我想?
最佳答案
Where he is passing one ArrayList, I have to pass in three, but to make that happen, had to wrap in a custom object class. One of the ArrayLists that I want to sort is one of three fields in the class called "name".
你没有三个ArrayLists
, 你有一个 ArrayList
由三个构建的自定义对象 ArrayLists
(因此大小是您传递给适配器的 List
的大小)。从这个角度来看,您的代码中唯一的变化是使用来自该自定义对象的名称 Items
构建部分:
for (int i = 0; i < data.size(); i++) {
String s = data.get(i).name.substring(0, 1).toUpperCase();
if (!alphaIndexer.containsKey(s)) {
alphaIndexer.put(s, i);
}
}
// ...
没有其他变化。您还可能需要对 List
进行排序的 Items
您使用以下方式传递给适配器:
Collections.sort(mData);
你的Items
在哪里类必须实现 Comparable<Items>
接口(interface):
class Items implements Comparable<Items> {
String name;
// ... rest of the code
@Override
public int compareTo(Items another) {
// I assume that you want to sort the data after the name field of the Items class
return name.compareToIgnoreCase(another.name);
}
}
关于android - SectionIndexer - 与 ArrayAdapter 和 CustomObject 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11641267/
调用自定义对象时出现以下错误 “‘customObject’类型的对象无法转换为‘customObject’类型。” 以下是我遇到此错误时的情况: 我动态调用 dll 中的方法。 加载程序集 创建实例
我这周开始使用 ObjectMapper,我正在尝试将 JSON 映射到 2 个 CustomClasses,但我不知道 ObjectMapper 是否具有某些功能来执行我想要的操作。第一个 Cust
我创建了一个名为“Item”的对象,并且我想序列化其中包含 Items 的 ArrayList。我的程序与 ArrayList 完美配合,但它不适用于 ArrayList 。我相信这与我的对象有关。这
我的 ArrayList 变量有问题,该变量包含 Videos 的对象我创建的类。 ArrayList我创建的似乎总是空的,即使我 add给它一个对象。这是我声明并使用此 ArrayList 的代码:
我想对自定义类的条目执行文本搜索。该文本搜索不仅应匹配输入的字符串,还应检查拼写错误的单词。所以我正在寻找一个可以为我做这件事的图书馆。 更多背景信息:假设我从 SQLServer2008 数据库生成
我正在尝试更新多条记录权限。当我只更新一条记录的权限时,一切正常,但当更新多条记录时,没有任何反应。 List qbCustomObjectList = new LinkedList(); QBPer
我有这两个对象: public class BaseModule { public long BaseModuleID { get; set; } public string Modu
我在尝试对自定义对象的 ArrayList 进行排序时遇到问题。事实上,在排序之后,我的 ArrayList 中没有任何变化。我的方法有问题吗? 这是我的艺术家自定义对象属性: public clas
这个问题在这里已经有了答案: Getting an NSArray of a single attribute from an NSArray (3 个答案) 关闭 9 年前。
我得到了一个包含图片信息的类,比如文件路径、哈希值、字节。在另一个类中,我得到了一个通用列表,我在其中放置了包含图片信息的类中的对象。 那个类看起来像这样: [Serializable()]
我正在尝试保留一个假设为 xyz 的类的对象列表。当我在 NodeEntity 类中执行此操作时: @Property List listOfConditions 当通过 Neo4jOperation
我有一个如下的插件 class AbstractConfigExtension { public static final String NAME = "abstract_extension"
这是一个简单的问题,比任何事情都更具理论性。 Powershell 中的自定义对象和 PSObject 之间的真正区别是什么?您为什么以及何时创建它们? 谢谢! 最佳答案 PSCustomObject
我正在通过应用程序上下文将数据从 iOS 应用程序传输到其 Watch 扩展。我想发送我创建的自定义对象(名为WeatherReport)。 let context = ["report" : Wea
我在 Quickblox 控制台中创建了一个自定义对象,我可以检索这些对象,但现在需要访问原始 JSON 对象。 我可以通过字段手动迭代重建对象,并希望我可以直接引用和原始 JSON,从而节省大型数据
我有以下代码: ActiveModule resultObject = null; StringBuilder sqlStringBuilder = new Strin
有没有一种简单快速的方法来对 Java 中的 ArrayList 中的元素进行分组?我在问是否已经有办法不用我自己实现了?类似 Collections.sort(monuments,new objec
我创建了一个通用函数来检索JSON并对其进行解析: public IList RetrieveView(string result) { JavaScriptSeria
首先,如果我错了,请指正。我想从 ArrayList 中找到项目的索引(即字符串值)不使用 For 循环。 POJO: id; name; 代码: ArrayList list = new Array
嗨我正在尝试序列化 FileStream fileStream = new FileStream("batches.xml", FileMode.Open); XmlSerializer serial
我是一名优秀的程序员,十分优秀!