Wednesday 3 August 2011

Custom List Adapter

Note: You want to customize your List Adapter as per your requirement then here is some useful code for you but  modify it as you require ....... It is just a trailor, rest is in your hands.

import java.util.Vector;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class YourAdapterName extends BaseAdapter{

private Context mContext;
private Vector mValuestoShow;

/**
 * Constructor to be called to initialize adapter with values.
 * @param context
 * @param vector
 */
public YourAdapterName(Context context, Vector vector){
    mContext = context;
    mValuestoShow = vector;
}

public int getCount() {
    if(null != mValuestoShow){
        return mValuestoShow.size();
    }
    return 0;
}

public Object getItem(int position) {
    if(position < mValuestoShow.size())
        return  mValuestoShow.get(position);
    else
        return null;
}

public long getItemId(int position) {
    return 0;
}

/**
 * This method can be override to enable/disable particular list row.
 */
@Override
public boolean isEnabled(int position) {
    //Write your code here......
    return super.isEnabled(position);
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder ;
    if(convertView == null){
        LayoutInflater li =(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = li.inflate(R.layout.your_layout, null);
        holder = new ViewHolder();
        holder.txt_name = (TextView) convertView.findViewById(R.id.name_txt);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }
  
    /**
 * Use of enable method how to set different color for disabled row....
 * You can also customize your row background color or text color without using enable method
 * in the same way as below is done as per your conditions.
 */
    if(!isEnabled(position)){
        holder.txt_name.setBackgroundColor(mContext.getResources().getColor(R.color.color_code));
        holder.txt_name.setTextColor(mContext.getResources().getColor(R.color.white));
    }else{
        holder.txt_name.setBackgroundColor(mContext.getResources().getColor(R.color.color_code));
        holder.txt_name.setTextColor(mContext.getResources().getColor(R.color.black));
    }
  
    holder.txt_name.setText(getItem(position).toString());
  
    return convertView;
}

class ViewHolder {
    TextView txt_name;
}

}

your_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width = "fill_parent"
    android:layout_height = "wrap_content"
    android:padding = "10dp" >
   
<TextView
    android:id = "@+id/txt_type1"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content" />
   
<TextView
    android:id = "@+id/txt_type2"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content" />
   
<ImageView
    android:id = "@+id/image"
    android:src = "@drawable/image"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content" />
           
</RelativeLayout>

Note:You can add more views like Button , ImageButton, EditText as per your need.



No comments:

Post a Comment