Could someone explain me where the following problem is coming from:
I have a list that displays several items according to some preferences . The user can change these preferences through a SlidingDrawer . Whenever this happens I get the items corresponding to the new preferences and then I the set the result to the adapter backing the listview with the code below.
public void update(final List<Report> reports)
{
adapter.setReports(reports);
((ArrayAdapter)list.getAdapter()).notifyDataSetChanged();
list.invalidate();
}
Here’s the adapter class
public class ReportsAdapter extends ArrayAdapter<Report>
{
List<Report> reports;
Activity context;
public void setReports(List<Report> reports)
{
this.reports = reports;
}
ReportsAdapter(Activity context, List<Report> reports)
{
super(context, R.layout.row_report, R.id.report_timestamp,reports) ;
this.reports = reports;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = context.getLayoutInflater().inflate( R.layout.row_report, null);
TextView timestamp = (TextView)row.findViewById(R.id.report_timestamp);
if(timestamp != null)
timestamp.setText("Timestamp: "+reports.get(position).getTimestamp());
TextView type = (TextView)row.findViewById(R.id.report_type);
if(type != null)
type.setText("Type: "+reports.get(position).getReportType().getType().toString());
TextView status = (TextView)row.findViewById(R.id.report_status);
if(status != null)
status.setText("Status: "+reports.get(position).getStatus());
ImageView icon = (ImageView)row.findViewById(R.id.report_icon);
if(icon != null && reports.get(position).getReportType().getType()!= ReportType.RType.DEFAULT)
{
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), reports.get(position).getIconResource());
if(bitmap != null)
{
icon.setImageBitmap(bitmap);
}
}
return row;
}
}
After that the application crashes with an ArrayOutOfBoundsException in the adapter.
Hmmmm what does your custom Adapter extend from? You’re probably not updating the same Array/List that the
getCount()is getting its size from. Or maybe there’s another problem with yourgetCount()implementation. I’ve found through practice that the safest way to get an Adapter working properly is to just extend fromBaseAdapterand fill in any of the methods that the compiler asks you to.Incidentally, you don’t have to get the adapter through the list (since it’s the same as
adapter, hopefully), so you can doadapter.notifyDatasetChanged()directly.If this doesn’t solve your problem, consider posting your adapter code.