是的,看下面的代码:
array of data
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 };
private String[] imageNameDatabase = { "ball", "catmouse", "cube", "fresh",
"guitar", "orange", "teapot" };
创建哈希映射列表。
private void initializeImageList() {
for (int i = 0; i < imageNameDatabase.length; i++) {
map = new HashMap<String, Object>();
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");
}
给适配器分配一个旋转器。
public void createAddDialog() {
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);
}
上面使用的适配器如下所示...
package com.tcs.CustomListViewGeneration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
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);
}
((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;
}
}
getDropDownView(...)
,只有getView(...)
。 - Jamie