ListView sometimes displays values and sometimes doesn't
I'm trying to make an app that uses the Bissection method to find an approximate root of a function. App makes calculation and displays the results in a listView, it works fine for the most time but after some testing I found out that some functions' results are not shown.
E.g. x^3-9x+3 from 1 to 3 works but from 0 to 1 doesn't.
img https://i.sstatic.net/yvEzz.png
What am i missing?
Code:
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;
}
}
In getCount() you need to return the count of the array. check in case of 0 to 1, whether a2[i] ==0, If it is then i would be returned as a count, whereas you need to return a2.length here. getCount() results in invocation of getView() which is then responsible for rendering the view on screen.
For instance, in case of 0 to 1, a2[0] == 0, then i = 0, will be returned, so framework will get to know that there is nothing to display since count is 0.
So you should return a2.length.
It would be even better that you bundle all these arrays in a class and use a arraylist of instances of class and return the size of the list in getCount()
.