I am trying to call the LazyAdapter constructor for setting the ListView. And I always see a red line on new LazyAdapter(this, mStrings); asking to resolve this. I am not sure what should I add here in place of this, so that it can start working. When I try to replace this with getApplicationContext then I always get error as ClassCastException. Any thoughts what I need to add inplace of this?
@Override
protected void onPostExecute(ArrayList<User> response) {
if (response!=null) {
adapter=new LazyAdapter(this, mStrings);
listView.setAdapter(adapter);
}
for(User user : response){
//Doing Some Stuff Here
}
}
And LazyAdapter constructor is something like this-
public LazyAdapter(Activity a, String[] d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
Update:-
My LazyAdapter full code-
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private String[] data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, String[] d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item, null);
TextView text=(TextView)vi.findViewById(R.id.text);;
ImageView image=(ImageView)vi.findViewById(R.id.image);
text.setText("item "+position);
imageLoader.DisplayImage(data[position], image);
return vi;
}
}
The problem is that you’re initializing the
Adapterinside anAsyncTaskmethod, so at the initialization timethispoints toAsyncTaskinstance. If theAsyncTaskis an inner class of anActivity, then you can simply changethistoActivity.this, otherwise you need to pass anActivityto theAsyncTaskconstructor and use it to initialize yourAdapter. Hope this helps.