【问题标题】:Android IndexOutOfBoundsException when trying to add RowItem to ListView尝试将 RowItem 添加到 ListView 时出现 Android IndexOutOfBoundsException
【发布时间】:2017-07-26 15:42:14
【问题描述】:

我正在尝试从 JSON 对象中提取一些数据以插入到添加到 ListView 的 CustomRowItem 中,JSON 被提取得很好,但是当我尝试将数据添加到行项目时,我得到了:@987654321 @错误。

完整代码如下:

    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();
}

代码在CurrencyRowItem item = new CurrencyRowItem(currency.jsonCurrencyName.get(i), currency.jsonCurrencyPrice.get(i), currency.jsonPercentchange24hr.get(i), currency.jsonVolume24hr.get(i)); 上失败 Line 和它正在努力解决这个问题,提前感谢您的帮助。

适配器代码

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;
    }
}

【问题讨论】:

  • 显示您的适配器代码
  • 上面添加的片段
  • 总是发布完整的堆栈跟踪/LogCat。此外,您应该尝试将该行分成更小的行,例如itemCurrencyName = currency.jsonCurrencyName.get(i); 等。这样您就可以准确地知道问题发生在哪里。
  • 你怎么知道传递给drawListRows()的变量i在数组currency.jsonCurrencyName的范围内?

标签: java android json listview


【解决方案1】:

我猜应该是这样的:

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"));

你没有填充数据,这个数组可能是空的

【讨论】:

  • 是的,修复了它!如此愚蠢的疏忽......我真的应该在盯着这个几个小时后定期休息......非常感谢!!!!
【解决方案2】:

你的数组是空的。

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

尝试添加这个:

if (returnedJsonArrayResult != null && 
    !returnedJsonArrayResult.isEmpty()) {
    for (int i = 0; ...) {
        // your code
    }
}

另外,循环整数从 0 开始,而不是 1。数组从 0 开始。如果你从一个开始,你总是会跳过第一个条目。

【讨论】:

  • 更改了在尝试调试时,.isEmpty() 似乎在阵列上未解决。单步执行循环时,数据确实似乎在那里。
  • 如果你在 0 处得到一个索引越界异常,那么你的数组是空的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
  • 2011-09-20
  • 1970-01-01
  • 2014-08-26
  • 1970-01-01
相关资源
最近更新 更多