Create new class and name it whatever you want (here AlbumList_Adapter.java)
public class AlbumList_Adapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
String basePath = "http://example.com/imgFolder/";
public AlbumList_Adapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
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.list_row_simple, null);
TextView txtListItem = (TextView) vi.findViewById(R.id.txtListItem);
ImageView imageView= (ImageView) vi.findViewById(R.id.imageView);
HashMap<String, String> imgData = new HashMap<String, String>();
imgData = data.get(position);
txtListItem.setText(imgData.get("name"));
Picasso.with(context)
.load(basePath+ imgData.get("image"))
.resize(100, 100)
.centerCrop()
.into(imageView);
return vi;
}
}
In your code.java file, declare adapter at the top (before onCreate() so that you can use it anywhere)
AlbumList_Adapter adapter;
In your code after json parsing
ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
// looping through all nodes
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = jsonArray.getJSONObject(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap
map.put("name", jsonObject.getString("albumName"));
map.put("image", jsonObject.getString("albumImage"));
// adding HashMap to ArrayList
dataList.add(map);
}
adapter = new AlbumList_Adapter(codeActivity.this, dataList);
list.setAdapter(adapter);
Note that, I am using Hashmap. You should use your data structure. (You can use Hashmap. Then you have to update your code)
One more thing, following line in AlbumList_Adapter.java refers to xml file I created for list row.
vi = inflater.inflate(R.layout.list_row_simple, null);
Hope this answer helps you. Please let me know if you got some problem implementing this.
Happy coding...