【发布时间】: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