Reputation: 6666
I am trying to set the data to an adapter through an AsyncTask. This has caused alot of grief - Most recently when trying to set the Array Adapter.
The following method is called onPostExecute();
private void setQueues(final JSONObject[] qInfo)
{
queues = new QueueItem[qInfo.length];
for(int i = 0; i < qInfo.length; i++)
{
queues[i] = new QueueItem();
//final int ii = i;
// Formatting the queue title
String name = qInfo[i].optString("name").replace("-", " ");
queues[i].label = name;
try {
if(qInfo[i].getString("active").contains("1"))
{
queues[i].value = true;
}
else
{
queues[i].value = false;
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
lv.setAdapter(new QueueAdapter(getActivity(),
R.layout.queues_items, queues));
This causes the following exception runtime : link here
EDIT : As requested, here is QueueAdapter :
public class QueueAdapter extends ArrayAdapter<QueueItem>{
Context context;
int layoutResourceId;
QueueItem data[] = null;
public QueueAdapter(Context context, int layoutResourceId, QueueItem[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
QueueHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new QueueHolder();
holder.queueswitch = (Switch)row.findViewById(R.id.queues_item_switch);
holder.txtLabel = (TextView)row.findViewById(R.id.queues_item_text);
row.setTag(holder);
}
else
{
holder = (QueueHolder)row.getTag();
}
QueueItem queue = data[position];
holder.txtLabel.setText(queue.label);
holder.queueswitch.setChecked(queue.value);
return row;
}
static class QueueHolder
{
Switch queueswitch;
TextView txtLabel;
}
}
Upvotes: 0
Views: 442
Reputation: 13501
lv.setAdapter(new QueueAdapter(getActivity(),
R.layout.queues_items, queues));
this snippet should be in try block.. Because If there is JSONException
all elements in that array will be null...
I mean to say.. put for loop inside try block and not the oppt.. if you still want to loop when an Exception occurs.. Then Try using Collections instead of array..
Upvotes: 1