in spinner items display the list array to replace drawable images how can implemented
i am using spinner in some application in spinner item list array this text replaced in drawable images how can its implemented
personalinformation = (Spinner) findViewById(R.id.SpinnerCategory);
ArrayAdapter<?> adapterDefaultpersonal = ArrayAdapter.createFromResource(Animals.this, R.array.Animalinformation, android.R.layou开发者_开发技巧t.simple_spinner_item);
adapterDefaultpersonal.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
personalinformation.setAdapter(adapterDefaultpersonal);
How can the R.array.Animalinformation
array list items replaced in drawable images be implemented?
Yes it is look at the code below.. array of data
//stores the image database icons
private static Integer[] imageIconDatabase = { R.drawable.ball, R.drawable.catmouse, R.drawable.cube, R.drawable.fresh, R.drawable.guitar, R.drawable.orange, R.drawable.teapot };
//stores the image database names
private String[] imageNameDatabase = { "ball", "catmouse", "cube", "fresh", "guitar", "orange", "teapot" };
creating List of hashmaps
private void initializeImageList() {
// TODO Auto-generated method stub
ArrayList<HashMap<String, Object>> spinnerList = new ArrayList<HashMap<String,Object>>();
for (int i = 0; i < imageNameDatabase.length; i++) {
HashMap map = new HashMap();
map.put("Name", imageNameDatabase[i]);
map.put("Icon", imageIconDatabase[i]);
spinnerList.add(map);
}
ImageView imageView = new ImageView(this);
imageView.setBackgroundResource((spinnerList.get(0).get("Icon"));
spinnerList.get(0).get("Name");
}
// assigning spinner to adapter
public void createAddDialog() { // TODO
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list_editoradd_dialog);
Spinner spin = (Spinner) findViewById(R.id.spinnerAddImageList);
CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(this, spinnerData, R.layout.spinner_view, new String[] { "Name", "Icon" }, new int[] { R.id.imageNameSpinner, R.id.imageIconSpinner });
spin.setAdapter(adapter);
}
the adapter used above is as given below..
class CustomSpinnerAdapter extends SimpleAdapter {
LayoutInflater mInflater;
private List<? extends Map<String, ?>> dataRecieved;
public CustomSpinnerAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
dataRecieved = data;
mInflater = LayoutInflater.from(context);
}
@SuppressWarnings("unchecked")
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null) {
convertView = mInflater.inflate(R.layout.spinner_view,null);
}
HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
((TextView) convertView.findViewById(R.id.imageNameSpinner)).setText((String) dataRecieved.get(position).get("Name"));
((ImageView) convertView.findViewById(R.id.imageIconSpinner)).setBackgroundResource(dataRecieved.get(position).get("Icon")));
return convertView;
}
}
精彩评论