【问题标题】:Android using custom object with ArrayAdapter, and communicating back viewsAndroid 使用带有 ArrayAdapter 的自定义对象,并传递返回视图
【发布时间】:2021-04-16 14:00:42
【问题描述】:

有一些类似的问题,但我找不到我想要的。我正在使用一个存储在 ArrayAdapter 中的自定义类来填充一个具有线性布局的 ListView,我可以通过修改该类来自定义它 - 这是因为我每个都有一个按钮,它完全符合我的要求。 LinearLayout 看起来像:[TextView][EditText][Button],当单击 Button 时,它会使用 EditText 的内容执行一些数据库操作。没关系。

我遇到的问题是,我想(例如)在我的活动中更新 ListView 中 1 个布局中的 EditText 的文本内容,而不是在创建视图和设置按钮侦听器的类中等等在。我可能还想说“禁用列表视图第二项中的按钮”等。

我尝试了很多不同的方法,在适配器上传递上下文或调用方法,而不是在实际的对象列表上,使 EditText 成为带有 getter 的成员变量,我总是什么也没发生,或者我到达按 ID 查找 EditText 时出现 NullPointerException。

这甚至可能吗?我是否需要创建一个最终变量来保存每个 EditText 并以某种方式将其传递回活动或其他东西?我最终会在我的活动中拥有多个具有相同 ID 的视图,但它们将由单独的对象创建

最终我要做的是制作它,以便我可以稍微抽象一下我的 GUI。目前我有 8 个活动,大约 90% 相同,但我在每个活动中设置了视图的重复代码。我想将其抽象为类,我可以说“给我一个带有任何标签的新文本输入行,我将覆盖按钮行为”,然后将此任务重构为 1 个活动。当用户在“行”之一中输入数据时,活动可能需要更新其他行之一,(或在按下后退按钮时清除 1 等),这就是为什么我希望能够回调数组适配器内对象内布局内的特定视图:D

例如,我希望能够创建 3 个新的 textInputLineBuilder 对象(可能最终将其子类化)并将它们显示为:

[名称:][名称编辑文本][按钮]

[股票:][股票编辑文本][按钮]

[数量:][数量编辑文本][按钮]

这是(缩减的)布局,activity_row.xml,自定义对象膨胀并返回到 ArrayAdapter

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
    <TextView
        android:id="@+id/rowLabel"/>    
    <EditText
        android:id="@+id/rowInput"/>    
    <Button
        android:id="@+id/rowButton"/>
</LinearLayout>

以下是活动中的相关代码:

public class AnActivity implements UICallBack {
    private ListView listOfInputs;
    private ArrayList<UIElementInterface> listOfUIElements;
    private UIElementsAdapter UIAdapter;
    
    protected void onCreate() {
        listOfInputs = (ListView) findViewById(R.id.listInputs);
        
        listOfUIElements = new ArrayList<>();
        listOfUIElements.add(new textInputLineBuilder("<label>", this));    //this implements a callback interface
        //add a few more

        UIAdapter = new UIElementsAdapter(this.getApplicationContext(), listOfUIElements);
        UIAdapter.notifyDataSetChanged();
        listOfInputs.setAdapter(UIAdapter);
    }
    
    public void updateUIState() {
        //I have tried all sorts here
        for (int i=0; i<listOfUIElements.size(); i++) {
            UIAdapter.setTheText("test "+i, i);
        }

        UIAdapter.notifyDataSetChanged;
        listOfInputs.setAdapter(UIAdapter);
    }
}

然后我有我的 ArrayAdapter 类 UIElementsAdapter.java 从列表中检索适当的对象,然后调用 getRowView 以使对象膨胀将构成此“行”的布局

public class UIElementsAdapter extends ArrayAdapter<UIElementInterface> {
    private final Context context;

    public UIElementsAdapter(Context context, ArrayList<UIElementInterface> listOfUIElements) {
        super(context, 0, listOfUIElements);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getItem(position).getRowView(context, parent);   //retrieves the UIElementInterface object aka textInputLineBuilder object
    }

    public void setTheText(String s, Integer position) {
        try {
            getItem(position).setEditText(s);
        } catch(NullPointerException e) {
            e.printStackTrace();
        }
    }
}

最后我有textInputLineBuilder.java,它实现了 UIElementInterface (getRowView & setEditText)

public class textInputLineBuilder implements UIElementInterface {
    private View convertView;

    public textInputLineBuilder(String label, UICallBack callback) {
        this.labelText = label;
        this.callback = callback;   //used with button onclicklistener
    }
    
    public View getRowView(Context context, ViewGroup parent) {     
        LayoutInflater layout = LayoutInflater.from(context);
        convertView = layout.inflate(R.layout.activity_row, parent, false);

        label = (TextView) convertView.findViewById(R.id.rowLabel);
        fuzzy = (Button) convertView.findViewById(R.id.rowButton);
        input = (EditText) convertView.findViewById(R.id.rowInput);
        final EditText inputFinal = input;    //for the button listener.  was trying using input as a member variable for setEditText but that didn't work
        
        label.setText(labelText);
        
        //add onclicklistener for the fuzzy button. works fine
        
        return convertView;
    }
    
    public void setEditText(String s) {
        //I have tried all sorts here, passing in context from various places, passing in layout inflaters etc
        EditText e = (EditText) convertView.findViewById(R.id.rowInput);
        e.setText(s);
    }
}

【问题讨论】:

  • 通常修改数据然后通知适配器数据已更改。话虽如此,我不确定我是否理解您的方法。你能添加更多代码吗?
  • 是的,这就是 Activity 类的 updateUIState() 方法中发生的事情 - 我正在尝试在 ArrayAdapter 的 1 项中编辑 EditText 的内容,然后通知数据集有已更改,我无法添加更多代码,因为就是这样!除了活动类中不相关的其他内容。我应该说 - 当点击任何按钮时调用 updateUIState() 方法,我现在只是将它用作测试
  • 所以 UICallback 有这个 updateUIState() 方法,就是这样?我之所以问,是因为我想设置一个小应用程序,看看到目前为止发生了什么,什么不起作用。
  • 是的,回调还有其他一些方法,但它们在这里不相关。回调仅在 textInputLineBuilder 类中使用,当前在单击按钮时执行某些操作。这部分工作正常。为了测试,我只是在 Activity 类中直接调用 updateUIState() ,所以我目前没有为此使用回调。我实际上只是在创建活动时调用 updateUIState() 来尝试设置所有 EditTexts 的文本,以确保它有效。但我做不到

标签: android listview callback android-arrayadapter abstraction


【解决方案1】:

updateUIState() 中,您当前尝试通过调用setTheText() 直接修改EditTexts。使用调试器,我可以看到这在一定程度上是有效的——EditTextsetEditText() 的末尾具有所需的值。但是由于EditText 是由适配器管理的,因此单独设置文本并不能达到预期的效果。如果之后调用e.invalidate(),则文字显示正确,如果也跳过adapter.notifyDatasetChanged()

所以基本上可以修改Views。但这样做是个坏主意。

相反,您应该更改数据类。在您的情况下,您需要一种方法来为EditText 中的TextInputLineBuilder 设置所需的文本(String)。那么当getRowView()被调用时,这个更新后的文本应该和EditText input一起使用。

返回updateUIState():修改数据列表后调用notifyDatasetChanged()是正确的,但不需要将适配器重新分配给ListView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多