是否可以将2个参数传递给listfieldcallback类?

时间:2011-11-28 08:06:13

标签: blackberry

我是黑莓开发的新手。我想知道有没有其他方法来解决我的应用程序。我开发了一个简单的计算器应用程序,显示如,

value1 : 6
value2 : 3
Calculate

+  :  9
-  :  3
*  :  18
/  :  2

其中value1和value2是两个编辑字段,calculate是ButonField。在我的代码中,我编写了一个String数组{“+”,“ - ”,“*”,“/”},用于传递给实现ListFieldCallBack的类。

它的编码类似于

for(int i = 0; i<array.length;i++)
        {
            mylist.insert(i);
                    //calculated the result 
            myCallBack.insert(array[i]+"#"+result );
        }

在我的drawListRow中,我已经分离了数组值和结果。并以两个drawtexts显示。

运作良好。现在我的问题是,有没有其他方法可以传递数组中的值和计算结果?现在我将数组值和结果作为字符串传递并将其拆分以显示我的结果。但如果可以分开传递参数,我不需要在那里使用分离器函数。那是什么方式?或者还有其他方法吗? PLZ建议

1 个答案:

答案 0 :(得分:3)

嗯,这与黑莓无关。这与我们在OOP删除中使用的习语有关。

由于您的列表项代表2条信息 - 数学运算和结果 - 您需要为列表项创建一个模型,用于封装这2条信息:

public class ListItem {

    public final String operation;
    public final String result;

    public ListItem(String operation, String result) {
        this.operation = operation;
        this.result    = result;
    }
}

然后在ListFieldCallBack内,您应该有VectorListItem个实例。因此,当您致电myCallBack.insert(new ListItem(operation, result));时,它会将列表项添加到向量中。

当您在drawListRow(ListField listField, Graphics graphics, int index, int y, int width)时,首先通过索引获取列表项,然后您可以轻松获取列表项状态,而无需拆分字符串或执行任何其他脏操作。所以代码变得干净,OOP-ish:

ListItem li = // get ListItem by index

li.operation; // operation
li.result;    // result
相关问题