ListView并不总是显示值

时间:2014-03-14 15:53:06

标签: android listview double listview-adapter

ListView有时显示值,有时不显示我正在尝试创建一个使用Bissection方法查找函数近似根的应用程序。应用程序进行计算并在listView中显示结果,它在大多数时间内都能正常工作,但经过一些测试后我发现某些函数的结果没有显示。

E.g。 x ^ 3-9x + 3从1到3工作,但从0到1不工作。

img http://i.stack.imgur.com/yvEzz.png

  • 尝试调试,两个输入都通过相同的代码行,一切似乎都很好。在Logcat中没有显示任何内容。
  • ListView正在运行,因为某些输入显示值
  • ArrayAdapter的工作原理与listView相同,也是因为示例中的两个数组都有类似的double值。这应该不是问题


我缺少什么?

代码:

setListAdapter(new BisseccaoAdapter(a_array, b_array, c_array, fc_array));

class BisseccaoAdapter extends BaseAdapter {

    double[] a2, b2, c2, fc2;


    BisseccaoAdapter() {
        a2 = null;
        b2 = null;
        c2 = null;
        fc2 = null;
    }

    public BisseccaoAdapter(double[] a_array, double[] b_array,
            double[] c_array, double[] fc_array) {

        a2 = a_array;
        b2 = b_array;
        c2 = c_array;
        fc2 = fc_array;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolderItem viewHolder;
        if(convertView==null){

            // inflate the layout
            LayoutInflater inflater = getLayoutInflater();
            convertView = inflater.inflate(R.layout.linha_listview, parent, false);

            // set up the ViewHolder
            viewHolder = new ViewHolderItem();
            viewHolder.i = (TextView) convertView.findViewById(R.id.lista_i);
            viewHolder.at = (TextView) convertView.findViewById(R.id.lista_a);
            viewHolder.bt= (TextView) convertView.findViewById(R.id.lista_b);
            viewHolder.ct = (TextView) convertView.findViewById(R.id.lista_c);
            viewHolder.fct = (TextView) convertView.findViewById(R.id.lista_fc);

            // store the holder with the view.
            convertView.setTag(viewHolder);

        }else{

            viewHolder = (ViewHolderItem) convertView.getTag();
        }

        // assign values if the object is not null
        if(a2.length != 0) {
            // get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values
            viewHolder.i.setText(Integer.toString(position + 1));
            viewHolder.at.setText(new DecimalFormat("#.#######").format(a2[position]));
            viewHolder.bt.setText(new DecimalFormat("#.#######").format(b2[position]));
            viewHolder.ct.setText(new DecimalFormat("#.########").format(c2[position]));
            viewHolder.fct.setText(new DecimalFormat(".#######E0").format(fc2[position]));
        }

        return convertView;
    }

    @Override
    public int getCount() {

        for (i = 0; i < a2.length; i++) {
            if (a2[i] == 0) {
                break;
            }

        }

        return i;
    }

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }
}

1 个答案:

答案 0 :(得分:0)

在getCount()中,您需要返回数组的计数。检查0到1的情况,是否a2 [i] == 0,如果是,那么我将作为计数返回,而你需要在这里返回a2.length。 getCount()导致调用getView(),然后负责在屏幕上呈现视图。

例如,在0到1的情况下,将返回a2 [0] == 0,然后i = 0,因此框架将知道由于count为0,因此无需显示任何内容。

  

所以你应该返回a2.length。

将所有这些数组捆绑在一个类中并使用类实例的arraylist并返回getCount()中列表的大小会更好。

相关问题