android spinner 下拉列表

我现在想要在spinner的下拉列表中显示图片,不知道该如何做啊,下面这种方法:<?xmlversion="1.0"encoding="utf-8"?><TextView... 我现在想要在spinner的下拉列表中显示图片,不知道该如何做啊,下面这种方法:

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="24sp"
android:singleLine="true"
style="?android:attr/spinnerDropDownItemStyle" />

在Activity中我们可以这样调用
private static final String[] countriesStr={"","","",""}
mySpinner = (Spinner) findViewById(R.id.mySpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countriesStr);
adapter.setDropDownViewResource(R.layout.myspinner_dropdown);
mySpinner.setAdapter(adapter);
没有绑定数据源啊,我图片的数据该在哪里个adapter绑定上啊
展开
 我来答
raycoast
推荐于2016-10-25 · TA获得超过3408个赞
知道大有可为答主
回答量:1976
采纳率:50%
帮助的人:2585万
展开全部
你想做个spinner用来选择国家,国家名旁边显示国旗?
我做过一个类似的功能,截取一段代码给你参考一下。
你要自定义一个Adapter。
package xxxx.widget;

import java.util.List;
import java.util.Map;

import xxxx.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class CountryAdapter extends BaseAdapter {

private int mResource = 0;
private int mFieldId = 0;

private boolean mNotifyOnChange = true;

private Context mContext = null;
private List<? extends Map<String, Object>> mList = null;

private LayoutInflater mInflater = null;

public CountryAdapter(Context context, List<? extends Map<String, Object>> list, int resource, int resourceId) {
mContext = context;
mList = list;
mResource = resource;
mFieldId = resourceId;
mInflater = LayoutInflater.from(mContext);
}

public CountryAdapter(Context context, List<? extends Map<String, Object>> list, int resource) {
mContext = context;
mList = list;
mResource = resource;
mInflater = LayoutInflater.from(mContext);
}

public void setList(List<? extends Map<String, Object>> list) {
mList = list;
if (mNotifyOnChange) notifyDataSetChanged();
}

@Override
public int getCount() {
return mList == null ? 0 : mList.size();
}

@Override
public Map<String, Object> getItem(int position) {
return mList == null ? null : mList.get(position);
}

@Override
public long getItemId(int position) {
return mList == null ? -1 : position;
}

public int getPosition(String code) {
for (int i = 0; i < mList.size(); i++) {
Map<String, Object>map = mList.get(i);
if (code.equals(map.get(StringUtil.KEY_CODE).toString())) {
return i;
}
}
return -1;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (mList == null) return null;
Map<String, Object> map = mList.get(position);

View view;
ImageTextView imageTextView;
ImageView voiceView;
ImageView speechView;

if (convertView == null) {
view = mInflater.inflate(mResource, parent, false);
} else {
view = convertView;
}
try {
if (mFieldId == 0) {
imageTextView = (ImageTextView) view;
} else {
imageTextView = (ImageTextView) view.findViewById(mFieldId);
}
} catch (ClassCastException e) {
throw new IllegalStateException(
"CountryAdapter requires the resource ID to be a CheckedImageTextView", e);
}
imageTextView.setKey(map.get(StringUtil.KEY_CODE).toString());
imageTextView.setImageResource(Integer.parseInt(map.get(StringUtil.KEY_FLAG).toString()));
imageTextView.setText(map.get(StringUtil.KEY_NAME).toString());

try {
voiceView = (ImageView) view.findViewById(R.id.voice);
if (voiceView != null) {
if (Boolean.parseBoolean(map.get(StringUtil.KEY_VOICE).toString()))
voiceView.setVisibility(View.VISIBLE);
else
voiceView.setVisibility(View.GONE);
}
speechView = (ImageView) view.findViewById(R.id.speech);
if (speechView != null) {
if (Boolean.parseBoolean(map.get(StringUtil.KEY_SPEECH).toString()))
speechView.setVisibility(View.VISIBLE);
else
speechView.setVisibility(View.GONE);
}
} catch (ClassCastException ignore) {
//
}
return view;
}

@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
mNotifyOnChange = true;
}

public void setNotifyOnChange(boolean notifyOnChange) {
mNotifyOnChange = notifyOnChange;
}

@Override
protected void finalize() throws Throwable {
mList.clear();
super.finalize();
}
}

然后在窗体Activity的onCreate里这样调用。创建一个HashMap的ArrayList,每个HashMap存储一个选项的内容:国家名,国旗还有一个code。用这个ArrayList作为参数创建一个刚才自定义的Adapter的实例。再把这个Adapter传给spinner。
ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

HashMap<String, Object> map = new HashMap<String, Object>();
map.put(StringUtil.KEY_CODE, "CN");
map.put(StringUtil.KEY_NAME, "China");
map.put(StringUtil.KEY_FLAG, "@drawable/cn_flag");
list.add(map);

CountryAdapter adapter = new CountryAdapter(this,list,R.layout.country_singlechoice,R.id.checked1);

spnCountry = (Spinner)findViewById(R.id.spn_Country);
if (spnCountry != null) {
spnCountry.setAlwaysDrawnWithCacheEnabled(false);
spnCountry.setDrawingCacheEnabled(false);
spnCountry.setAdapter(adapter);
spnCountry.setSelection(adapter.getPosition("CN"));
spnCountry.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Map<String, ?> map = adapter.getItem(position);
// do something
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
});
}
资源文件R.layout.country_singlechoice:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<jp.co.efficient.mTranslate.widget.ImageTextView
android:id="@+id/checked1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceSmallInverse"
android:gravity="center_vertical"
android:singleLine="true"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:ellipsize="marquee"
style="?android:attr/spinnerItemStyle" />
<ImageView
android:id="@+id/voice"
android:src="@drawable/mic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip" />
<ImageView
android:id="@+id/speech"
android:src="@drawable/speak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip" />
</LinearLayout>
因为不方便给你源代码,所以截取了一部分,还作了一些修改,所以恐怕会出错。你按照这个思路修改一下。
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
julyjar
2011-01-06
知道答主
回答量:49
采纳率:0%
帮助的人:12万
展开全部
这是什么问题
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式