Simple adapter problem, fine tune text image Java,Android
•
Java
Greetings to all
ArrayAdapter healthadapter = ArrayAdapter.createFromResource( this,R.array.health,android.R.layout.simple_spinner_item); mHealthSpin = (Spinner) findViewById(R.id.health_spin); healthadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mHealthSpin.setAdapter(healthadapter);
Simple enough I want to add an image to the spinner Radio buttons are unnecessary Therefore, the spinner should pop up and list a list:
TEXT *IMAGE* TEXT2 *IMAGE* TEXT3 *IMAGE*
So far, I have a custom simpleadapter This is the problem!: Text appears, but not an image Agent code:
public class stageadapter extends SimpleAdapter { private Context localContext; private ArrayList> localList; public stageadapter(Context context,ArrayList> list,int resource,String[] from,int[] to) { super(context,list,resource,from,to); localContext = context; localList = list; } @Override public View getView(int position,View convertView,ViewGroup parent) { if (null == convertView) { LayoutInflater inflater = (LayoutInflater) localContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.stagerow,null); } TextView name = (TextView) convertView.findViewById(R.id.stage_name); name.setText((String)localList.get(position).get("Name")); ImageView icon = (ImageView) convertView.findViewById(R.id.stage_icon); icon.setImageResource(R.drawable.icon); return convertView; } @Override public View getDropDownView(int position,ViewGroup parent) { if (null == convertView) { LayoutInflater inflater = (LayoutInflater) localContext .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.stagerow,null); } TextView name = (TextView) convertView.findViewById(R.id.stage_name); name.setText((String)localList.get(position).get("Name")); ImageView icon = (ImageView) convertView.findViewById(R.id.stage_icon); icon.setImageResource(R.drawable.icon); return convertView; } }
I'm going to use the switch statement to set a different image for each name But I stopped here until I could see any image to show
I called
ArrayList> list = new ArrayList>(); HashMap map = new HashMap(); map.put("Name","One"); map.put("Icon",R.drawable.icon); list.add(map); map = new HashMap(); map.put("Name","Two"); map.put("Icon",R.drawable.icon); list.add(map); mStageSpin = (Spinner) findViewById(R.id.stage_spin); stageadapter adapter = new stageadapter(getApplicationContext(),R.layout.stagerow,new String[] { "Name","Icon"},new int[] { R.id.stage_name,R.id.stage_icon }); mStageSpin.setAdapter(adapter);
The answer to me is in the comments
Solution
Delete the following line – it confuses your adapter:
healthadapter. setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); ArrayList<HashMap<String,Object>> list = new ArrayList<HashMap<String,Object>>(); HashMap<String,Object> map = new HashMap<String,Object>(); map.put("Name","One"); map.put("Icon",R.drawable.icon); list.add(map); map = new HashMap<String,"Two"); map.put("Icon",R.drawable.icon); list.add(map); Spinner spin = (Spinner) findViewById(R.id.spin); myAdapter adapter = new myAdapter(getApplicationContext(),R.layout.list_layout,new String[] { "Name","Icon" },new int[] { R.id.name,R.id.icon }); spin.setAdapter(adapter); } private class myAdapter extends SimpleAdapter { public myAdapter(Context context,List<? extends Map<String,?>> data,int[] to) { super(context,data,to); } @Override public View getView(int position,ViewGroup parent) { if (convertView == null) { convertView = getLayoutInflater().inflate(R.layout.list_layout,null); } HashMap<String,Object> data = (HashMap<String,Object>) getItem(position); ((TextView) convertView.findViewById(R.id.name)) .setText((String) data.get("Name")); ((ImageView) convertView.findViewById(R.id.icon)) .setImageResource(R.drawable.icon); return convertView; } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码