Page 133

Android Programming Cookbook

125 / 143

import android.widget.ImageView; import java.util.ArrayList; public class StackAdapter extends BaseAdapter { ArrayList arrayList; LayoutInflater inflater; ViewHolder holder = null; public StackAdapter(Context context, ArrayList arrayList) { this.arrayList = arrayList; this.inflater = LayoutInflater.from(context); } @Override public int getCount() { return arrayList.size(); } @Override public StackItems getItem(int pos) { return arrayList.get(pos); } @Override public long getItemId(int pos) { return pos; } @Override public View getView(int pos, View view, ViewGroup parent) { if (view == null) { view = inflater.inflate(R.layout.item, parent, false); holder = new ViewHolder(); holder.image = (ImageView) view.findViewById(R.id.image); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.image.setBackgroundResource(arrayList.get(pos).getImage()); return view; } public class ViewHolder { ImageView image; } }

7.6

Create the source code of the AndroidStackViewActivity

Add a new Java class inside src/com.javacodegeeks.AndroidStackViewExample/ so that we are going to have the src/com.javacodegeeks.AndroidStackViewExample/AndroidStackViewActivity.java file and paste the code below. AndroidStackViewActivity.java

SKY MAGZ  

This is Yahya Christian School Magazine

Advertisement