Android 中接口对象的自定义适配器

Custom adapter of interface object in Android

在我的 Android 应用程序中,我有一些代表不同类型操作的对象。

public class OperacionDivisa implements IOperacion {
public class OperacionLargo implements IOperacion {
public class OperacionMedio implements IOperacion {
public class OperacionOpciones implements IOperacion {

每种操作都实现了 IOperation 接口,因此我可以制作一个 IOperations 的 ArrayList,并将所有操作存储在一个 ArrayList 中。

现在我想做逆过程。我想从 Firebase 获取操作数组列表(已经实现),我想在 ListView

中显示操作

我创建了一个自定义适配器如下:

public class ListViewAdapterOperaciones extends ArrayAdapter<IOperacion>

问题是我需要将每个对象转换为其原始 class 以在文本视图中显示不同的属性。所以这个没用。

IOperacion operacion = (IOperacion) getItem(position);

因此,对于每个对象,我想在 listView 中显示一些数据,但我一直无法弄清楚如何执行此操作。任何帮助将不胜感激。

提前致谢。

使用 if 语句(或类似语句)检查 instanceof,然后将对象用作子类。

if (operacion instanceof OperacionLargo) {
  // Large operation
} else if (operacion instanceof OperacionDivisa) {
  // Other operation
}