6 Mar
2011

Android custom ListView

Go for this ListView if the you have a very long list of items where you have images and text in the list.
Putting images in the list is costly affair and will take up lot of memory and processor.

If you see the list not being scrolled smoothly that is probably because GC is being called to free memory.

Here is how we go.

1.write a custom ArrayAdaptor
and overload the getView(int position, View convertView, ViewGroup parent)

private class CustomFriendArrayAdapter extends ArrayAdapter {
		public CustomFriendArrayAdapter(Context context, int resource,
				int textViewResourceId) {
			super(context, resource, textViewResourceId, mFriend);
		}
                public View getView(int position, View convertView, ViewGroup parent) {
			SimpleListView view = null;
			Friend friend = mFriend.get(position);
			if (convertView == null) {
				Log.i("--->", "--->New Instance------->>>>");
				view = new SimpleListView(getApplicationContext(),80,friend.getImageuri(),friend.getFirstname()+ " "+friend.getLastname().charAt(0));
				convertView = view;
			}else{
				Log.i("--->>>");
				((SimpleListView)convertView).setData(friend.getImageuri(),friend.getFirstname()+ " "+friend.getLastname().charAt(0));

			}
                        return convertView;
                 }
       }

Here is how we write SimpleListView (This is not complete implementation but surely helps)

public class SimpleListView extends View
{
      	String mImageUrl = null;
	String mUserName = null;
        //write the setup up code in the constructor (this will ve called only when a new view is requested -not more
        //then number of items that are visible at the same time in the list)
     public SimpleListView(Context context, int height, String url,
			String username) {
		super(context);
		//Log.e("SimpleListview", "In constaructor");
		mContext = context;
		defaultBitmap = BitmapFactory.decodeResource(mContext.getResources(),
				R.drawable.default_image);
		lightBlue = mContext.getResources().getColor(
				R.color.application_sky_blue_color);
		paint = new Paint();
		// set's the paint's colour
		paint.setColor(mContext.getResources().getColor(
				R.color.backgroundColorDark));
		// smooth's out the edges of what is being drawn
		paint.setAntiAlias(true);
		paint.setTypeface(Typeface.createFromAsset(mContext.getAssets(),
				"arialbd.ttf"));
	}
        @Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
			mCanvas = canvas;
			URL url = null;
			paint.setColor(lightBlue);
			canvas.drawRect(0, 0, getMeasuredWidth() * widthRatio,
					getMeasuredHeight() * heightRatio, paint);
			paint.setColor(Color.WHITE);
			paint.setTextSize(28 * heightRatio);
			canvas.drawText(mUserName, 100, 40,paint);
                       //write your code to fetch image from mImageUrl
			mCanvas.drawBitmap(bitmap, null, new Rect(
							0,0,
							getMeasuredHeight(), getMeasuredHeight()),
							null);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec)
		setMeasuredDimension(480,100);
	}

	public void setData(String url, String username) {
		mImageUrl = url;
		mUserName = username;
	}
}

Follow Me!

Follow Me! Follow Me! Follow Me! Follow Me!