Pages

Friday 3 February 2012

How to use listview using custom Adapter in Android?

Hope this will help you..:-)

ListView list;
protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
list = (ListView) findViewById(R.id.list);

list.setAdapter(new customadapter());
}

class customadapter extends BaseAdapter{

        public int getCount() {
            // TODO Auto-generated method stub
            return arraylist.size();
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(int postion, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder holder = null;
            LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.readsggs_row,
                        null);
                holder = new ViewHolder();
            }else
            {
              
                    holder = (ViewHolder) convertView.getTag();
              
            }
                //for (i = 0; i < DatabaseMethods.sggsbmid.size(); i++) {
            convertView.setTag(holder);
            if(postion%2==0)
            {
                convertView.setBackgroundColor(Color.parseColor("#A9BCF5"));
            }else
            {
                convertView.setBackgroundColor(Color.parseColor("#ffffff"));
            }
            holder.textview = (TextView) convertView
                        .findViewById(R.id.text);

           <///Like this text view you can use any view that you need////>

            return convertView;
        }
        class ViewHolder {
            TextView textview;
        }
    }


Thanks
V.k.Hooda

No comments:

Post a Comment