by hiteshrao » Tue May 27, 2008 4:48 pm
hi ,
you can do this using the Base Adapter class
i am giving you a sample programe
but thire is error while attahcment.
so iam giving you code
package com.sample.android;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
public class CustomAdapterActivity extends ListActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
ArrayList<Weather> weatherList = new ArrayList<Weather>();
Weather w = new Weather(Weather.OVERCAST,"Master Blaster" );
weatherList.add( w );
w = new Weather(Weather.OVERCAST,"Snakes" );
weatherList.add( w );
w = new Weather(Weather.SUNNY,"Love Songs" );
weatherList.add( w );
w = new Weather(Weather.RAIN,"Default");
weatherList.add( w );
Log.i("In side CustomAdapter","*******************");
WeatherAdapter weatherAdapter = new WeatherAdapter( this,weatherList );
Log.i("In side CustomAdapter","*******************");
setListAdapter( weatherAdapter );
}
}
package com.sample.android;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
public static final int show=0x7f020001;
public static final int weather_overcast=0x7f020002;
public static final int weather_rain=0x7f020003;
public static final int weather_sunny=0x7f020004;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040000;
public static final int main_no_items=0x7f040001;
}
}
package com.sample.android;
public class Weather
{
public static final int NA = -1;
public static final int SUNNY = 0;
public static final int OVERCAST = 1;
public static final int RAIN = 2;
public String city = null;
public int temperature = 0;
public int sky = NA;
public Weather(int sky,String city )
{
this.sky = sky;
this.city = city;
}
public String getCity()
{
return city;
}
public int getTemperature()
{
return temperature;
}
public int getSkyResources()
{
switch(sky)
{
case SUNNY:
return R.drawable.weather_sunny;
case OVERCAST:
return R.drawable.weather_overcast;
case RAIN :
return R.drawable.weather_rain;
}
return android.R.drawable.unknown_image;
}
}
package com.sample.android;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class WeatherAdapter extends BaseAdapter
{
private Context context;
private List<Weather> weatherList;
public WeatherAdapter(Context context, List<Weather> weatherList )
{
this.context = context;
this.weatherList = weatherList;
}
public int getCount()
{
return weatherList.size();
}
public Object getItem(int position)
{
return weatherList.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View arg1, ViewGroup arg2)
{
Log.i("this method call ","*******************");
Weather weather = weatherList.get(position);
return new WeatherAdapterView(this.context, weather );
}
}
package com.sample.android;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class WeatherAdapterView extends LinearLayout
{
public WeatherAdapterView(Context context,Weather weather)
{
super(context);
Log.i("WeatherAdapterView","*******************");
this.setOrientation(HORIZONTAL);
LinearLayout.LayoutParams skyParams = new LinearLayout.LayoutParams(25,LayoutParams.WRAP_CONTENT);
skyParams.setMargins(1,1,1,1);
ImageView skyControl = new ImageView( context );
skyControl.setImageResource(weather.getSkyResources());
addView(skyControl, skyParams);
LinearLayout.LayoutParams cityParams = new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT);
cityParams.setMargins(1, 1, 1, 1);
TextView cityControl = new TextView( context );
cityControl.setText( weather.getCity() );
cityControl.setTextSize(17f);
cityControl.setTextColor(Color.WHITE);
addView( cityControl, cityParams);
LinearLayout.LayoutParams temperatureParams = new LinearLayout.LayoutParams(20, LayoutParams.WRAP_CONTENT);
temperatureParams.setMargins(1, 1, 1, 1);
TextView temperatureControl = new TextView(context);
temperatureControl.setText( Integer.toString( weather.temperature ) );
temperatureControl.setTextSize( 14f );
temperatureControl.setTextColor(Color.WHITE);
addView( temperatureControl, temperatureParams);
}
}