我的小应用程序出了问题。 有两个ListViews,其中包含另外3个ArrayLists 它们通过从服务接收广播来每秒更新。数据集中有很多数字。 我可以更改数据集的来源。有时我的ListViews(甚至整个活动,因为我有自定义视图,它绘制了我的图表)闪烁,将一个数据集更改为另一个数据集时出现问题。
更新从广播接收器调用的UI方法
private void updateUI(Intent intent)
{
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> values = new ArrayList<String>();
Bundle i = intent.getExtras();
if (i==null)
{
System.out.print("");
}
else
{
if(i.get("chartJSON") !=null)
{
LinearLayout mChartLinearLayout;
mChartLinearLayout = (LinearLayout)findViewById(R.id.header_chart);
chartView.invalidate();
mChartLinearLayout.removeView(chartView);
chartView.setChartJSON((String) i.get("chartJSON"));
mChartLinearLayout.addView(chartView);
chart = true;
}
asksAmount.clear();
asksPrice.clear();
asksSumm.clear();
bidsAmount.clear();
bidsPrice.clear();
bidsSumm.clear();
if (i.get("asksAmount") != null)
{
asksAmount.addAll((ArrayList<String>) i.get("asksAmount"));
asksPrice.addAll((ArrayList<String>) i.get("asksPrice"));
asksSumm.addAll((ArrayList<String>) i.get("asksSumm"));
bidsAmount.addAll((ArrayList<String>) i.get("bidsAmount"));
bidsPrice.addAll((ArrayList<String>) i.get("bidsPrice"));
bidsSumm.addAll((ArrayList<String>) i.get("bidsSumm"));
ordersSellListAdapter.notifyDataSetChanged();
ordersBuyListAdapter.notifyDataSetChanged();
}
names.clear();
values.clear();
if (i.get("names") != null)
{
names.addAll((ArrayList<String>) i.get("names"));
values.addAll((ArrayList<String>) i.get("values"));
alPairNames.clear();
alPairNames.addAll(names);
tempValues.clear();
tempValues.addAll(alValues);
alValues.clear();
alValues.addAll(values);
adapterMenuTickerList.notifyDataSetChanged();
}
}
}
自定义适配器方法:
public class OrdersListAdapter extends ArrayAdapter<String> {
private final Activity context;
private ArrayList<String> price;
private ArrayList<String> amount;
private ArrayList<String> summ;
public OrdersListAdapter(Activity context, ArrayList<String> price, ArrayList<String> amount, ArrayList<String> summ)
{
super(context, R.layout.menu_tickerrows, price);
this.context = context;
this.price = price;
this.amount = amount;
this.summ = summ;
}
static class ViewHolder {
public TextView price;
public TextView amount;
public TextView summ;
}
@Override
public View getView(int pos, View convertView, ViewGroup parent)
{
ViewHolder holder;
View rowView = convertView;
if (rowView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.orders_list, null, true);
holder = new ViewHolder();
holder.price = (TextView) rowView.findViewById(R.id.tvPrice);
holder.amount = (TextView) rowView.findViewById(R.id.tvAmount);
holder.summ = (TextView) rowView.findViewById(R.id.tvSumm);
rowView.setTag(holder);
}
else
{
holder = (ViewHolder) rowView.getTag();
}
holder.price.setText((CharSequence) price.get(pos));
holder.amount.setText((CharSequence) amount.get(pos));
holder.summ.setText((CharSequence) summ.get(pos));
return rowView;
}
}
有时我会:
java.lang.IndexOutOfBoundsException: Invalid index 31, size is 0
on
holder.price.setText((CharSequence) price.get(pos));
当出现此错误时,我尝试滚动我的列表视图。
在
放置断点 ordersSellListAdapter.notifyDataSetChanged();
ordersBuyListAdapter.notifyDataSetChanged();
并且发现Bundle中存在相互更改的数据集。
解决
好的,我发现了我的错误。 我的服务中创建了不同的线程,它们同时发送数据。我添加了一个源的检查,它会中断错误的线程。