Jaspal Hayer Jaspal Hayer - 1 year ago 49
Android Question

Android IndexOutOfBoundsException when trying to add RowItem to ListView

I'm trying to extract some data from a JSON object to insert into a CustomRowItem which is added to a ListView, the JSON is extracted fine but when I try to add the data to the row item I get I:

D/ERROR: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
error.

The full code is below:

protected void parseJsonToCurrencyRowItem(Currency cryptoCurrency) {
try {
for (int i = 0; i < returnedJsonArrayResult.length(); i++) {
JSONObject jOb = returnedJsonArrayResult.getJSONObject(i);
cryptoCurrency.jsonCurrencyName.add(jOb.getString("name"));
cryptoCurrency.jsonCurrencyName.add(jOb.getString("percent_change_24h"));
cryptoCurrency.jsonCurrencyName.add(jOb.getString("price_gbp"));
cryptoCurrency.jsonCurrencyName.add(jOb.getString("24h_volume_gbp"));
drawListRows(cryptoCurrency, i);
}


} catch (Exception e) {
Log.d("ERROR", e.toString());
}

}

protected void drawListRows(Currency currency, int i) {
CurrencyRowItem item = new CurrencyRowItem(currency.jsonCurrencyName.get(i), currency.jsonCurrencyPrice.get(i), currency.jsonPercentchange24hr.get(i), currency.jsonVolume24hr.get(i));
rowItemList.add(item);
adapter.notifyDataSetChanged();
}


The code fails on the
CurrencyRowItem item = new CurrencyRowItem(currency.jsonCurrencyName.get(i), currency.jsonCurrencyPrice.get(i), currency.jsonPercentchange24hr.get(i), currency.jsonVolume24hr.get(i));

Line and its doing my head in trying to figure it out, thanks for help in advance.

AdapterCode

public class BrowseCustomAdapter extends BaseAdapter {
List<CurrencyRowItem> listingRowItems;
Context context;

BrowseCustomAdapter(Context context, List<CurrencyRowItem> listingRowItems) {
this.context = context;
this.listingRowItems = listingRowItems;
}

@Override
public int getCount() {
return listingRowItems.size();
}

@Override
public Object getItem(int position) {
return listingRowItems.get(position);
}

@Override
public long getItemId(int position) {
return listingRowItems.indexOf(getItem(position));
}

private class ViewHolder {
TextView currency_name;
TextView currency_price;
TextView curency_percent24hr;
TextView currency_24hrvolume;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;

LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.currency_row, null);
holder = new ViewHolder();

holder.currency_name = (TextView) convertView.findViewById(R.id.crypto_name);
holder.curency_percent24hr = (TextView) convertView.findViewById(R.id.crypto_percentage_change);
holder.currency_price= (TextView) convertView.findViewById(R.id.crypto_current_price);
holder.currency_24hrvolume= (TextView) convertView.findViewById(R.id.crypto_volume);

CurrencyRowItem row_pos = listingRowItems.get(position);

holder.currency_name.setText(row_pos.currencyName);
holder.curency_percent24hr.setText(row_pos.currencyPercentageChange);
holder.currency_price.setText(row_pos.currencyPrice);
holder.currency_24hrvolume.setText(row_pos.currency24hrVolume);


convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
}

Answer Source

I guess it should be look like:

cryptoCurrency.jsonCurrencyName.add(jOb.getString("name"));
        cryptoCurrency.jsonCurrencyPercent.add(jOb.getString("percent_change_24h"));
        cryptoCurrency.jsonCurrencyPrice.add(jOb.getString("price_gbp"));
        cryptoCurrency.jsonCurrencyVolume.add(jOb.getString("24h_volume_gbp"));

You not fill data sa this arrays could be empty

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download