item_list.xml
main.xml
PutImageToSpinnerExample.java
package example.com; import java.util.ArrayList; public class PutImageToSpinnerExample extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList> list = new ArrayList >(); HashMap map = new HashMap (); map.put("Name", "Item One"); map.put("Icon", R.drawable.icon); list.add(map); map = new HashMap (); map.put("Name", "Item Two"); map.put("Icon", R.drawable.icon); list.add(map); Spinner spin = (Spinner) findViewById(R.id.spin); myAdapter adapter = new myAdapter(getApplicationContext(), list, R.layout.item_list, new String[] { "Name", "Icon" }, new int[] { R.id.imagetext, R.id.image }); spin.setAdapter(adapter); } } class myAdapter extends SimpleAdapter { private Context mcontext; public myAdapter(Context context, List> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); mcontext = context; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater object = (LayoutInflater)mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (convertView == null) { convertView = object.inflate(R.layout.item_list, null); } HashMap data = (HashMap ) getItem(position); ((TextView) convertView.findViewById(R.id.imagetext)) .setText((String) data.get("Name")); ((ImageView) convertView.findViewById(R.id.image)) .setImageResource((Integer) data.get("Icon")); return convertView; } }
0 comments:
Post a Comment