【问题标题】:Custom adapter of interface object in AndroidAndroid中接口对象的自定义适配器
【发布时间】:2018-09-27 15:04:09
【问题描述】:

在我的 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>

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

IOperacion operacion = (IOperacion) getItem(position);

所以,对于每个对象,我想在 listView 中显示一些数据,但我无法弄清楚如何做到这一点。任何帮助将不胜感激。

提前致谢。

【问题讨论】:

    标签: android listview interface adapter


    【解决方案1】:

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

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

    【讨论】:

    • 正是我想要的!非常感谢,@WoogieNoogie
    猜你喜欢
    • 2018-11-09
    • 1970-01-01
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 2021-03-15
    相关资源
    最近更新 更多