【问题标题】:Android call findViewById from adapter custom classAndroid 从适配器自定义类调用 findViewById
【发布时间】:2017-03-12 14:14:47
【问题描述】:

下午好,

所以我有一个带有自定义适配器的自定义 ListView,其中有一个项目是每行的复选框。在我的 getView 中,我为我的复选框实现了 setOnCheckedChangeListener 和 onCheckedChanged 处理程序。

现在,问题是:

每当我选中/取消选中列表中的一个项目时,我想用我想要的值更新一个外部 TextView(假设每个项目都有一个相关的价格,所以我想在列表下方显示总价) .

我应该如何从适配器的 getView 到达“外部”视图?我还有什么其他解决方法?

我在自定义适配器的 getView 函数中保留了部分代码:

CheckBox name = (CheckBox) view.findViewById(R.id.product);
    name.setText(content[i][0]);
    final View v = view;
    final int position = i;
    name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton group, boolean isChecked) {
            setCheckedItem(position);
            EditText quantity = (EditText) v.findViewById(R.id.product_quantity);
            content[position][3] = quantity.getText().toString();
            Iterator<String> it = getCheckedItems().values().iterator();
            Double total = 0.0;
            for (int i=0;i<getCheckedItems().size();i++){
                Integer quantity_p = Integer.parseInt(getItem(position)[3]);
                Double price = Double.parseDouble(getItem(position)[2]);
                total += quantity_p*price;
            }
            TextView total_price = (TextView) findViewById(R.id.total_products_price);
            total_price.setText(total.toString());
        }
    });

注意最后两行:我知道我不能调用 findViewById 但我现在不知道该怎么做。任何建议都会很好,谢谢。

【问题讨论】:

  • 可以把你的class代码和xml文件放在这里吗?
  • 把你的适配器类放到活动类中。

标签: android listview adapter findviewbyid


【解决方案1】:

将您的适配器类放在活动类中。 在主活动类中声明TextView total_price 那么

total_price = (TextView) findViewById(R.id.total_products_price);

在您的创建中。 然后您可以在onCheckedChanged 中访问total_price。试试这个,它可能会起作用。

【讨论】:

  • 它可能会起作用,但我真的很想摆脱那个解决方案,因为它会把所有混合在一个文件中的东西弄得一团糟:/
【解决方案2】:

您可以将TextView 传递给构造函数中的适配器。在此之后,您可以拥有一个 private static 类,它将像这样实现 CompoundButton.OnCheckedChangedListener

private static class MyOnCheckedChangedListener 
                     implements CompoundButton.OnCheckedChangedListener {
    TextView myView;
    public MyOnCheckedChangedListener (TextView viewToChange) {
         myView = viewToChange;
    }

    @Override
    public void onCheckedChanged(CompoundButton group, 
                                 boolean isChecked) { ... }
}

在此之后只需 setOnCheckedChangedListener new MyOnCheckedChangedListener (myTextView)(您传递给 Adapter 的那个),您就可以开始了。

【讨论】:

  • 我所做的是在构造函数中传递 TextView,然后在我喜欢的时候使用它,它工作得很好:D
【解决方案3】:

这只是一个想法和理论。但是您能否在您的活动中创建一个静态方法,在该方法中声明您要更改的 TextView,并在该方法中编写代码来更改您的文本,以您希望它更改的方式。

然后从 BaseAdapter 调用该静态方法。 我说的是静态的,所以你不必为整个类创建一个新实例。

【讨论】:

  • 我尝试添加静态方法 para 调用其中的 findViewById,这是不允许的。 (“不能从静态上下文中引用非静态方法 findViewById。”)
【解决方案4】:

我可能会这样做:

  1. 您可以将 textview 引用传递给您的适配器构造函数并使用它来更改其值。

  2. 但好的方法是在您的活动中实现interface,并在其回调方法中更新文本视图。为您适配器在构造函数中传递活动引用并使用其引用来调用interface 回调方法。

a) 拥有interace

public Interface UpdateTextviewInterface { void updateTextview(String value); }

b) 让你的 Activity 实现这个接口

public MainActivity extends Activity implments UpdateTextviewInterface

并覆盖该方法。

c) 将活动引用传递给适配器构造函数。使用活动引用调用updateTextview 方法来更新您的文本视图。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多