使用SimpleAdapter有微调 [英] Using SimpleAdapter with Spinner
问题描述
我是新的Android开发。我试图通过使用SimpleAdapter来填充微调。但微调的名单显示空白元素。当我点击任何一个元素,它的文本正确显示在吐司。请告诉我什么是我下面的code中的问题。
公共无效的onCreate(包savedInstanceState){
私人名单,其中,地图<字符串,字符串>>数据=新的ArrayList<地图<字符串,字符串>>();
的String []从=新的String [] {colorsData};
INT []到=新INT [] {R.id.spinner};
。的String []颜色= getResources()getStringArray(R.array.colorsData);
的for(int i = 0; I< colors.length;我++){
data.add(addData(颜色[I]));
}
微调微调=(微调)findViewById(R.id.spinner);
SimpleAdapter simpleAdapter =新SimpleAdapter(这一点,数据,android.R.layout.simple_spinner_item,从,到);
simpleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(simpleAdapter);
spinner.setOnItemSelectedListener(新OnItemSelectedListener(){
@覆盖
公共无效onItemSelected(适配器视图<>母公司视图中查看,
INT位置,长的id){
Toast.makeText(
parent.getContext(),
选定颜色: -
+ parent.getItemAtPosition(位置),
Toast.LENGTH_LONG).show();
}
});
}
私人地图<字符串,字符串> addData(字符串colorName){
地图<字符串,字符串> MAPLIST =新的HashMap<字符串,字符串>();
mapList.put(colorsData,colorName);
返回MAPLIST;
}
我是约95%确保您的到
数组应该被声明为:
INT []到=新INT [] {android.R.id.text1};
给一个尝试。
修改(根据下面的评论):
好像有哪些原因造成的IllegalStateException异常在旧版本的AndroidOS的错误。 (我没有看到异常的2.2,但我没有看到它在仿真器1.5)的错误可以通过添加ViewBinder到SimpleAdapter被合作周围。 ViewBinder并不难实现;这里有一个例子:
SimpleAdapter.ViewBinder viewBinder =新SimpleAdapter.ViewBinder(){
公共布尔setViewValue(查看视图,对象数据,
字符串textRe presentation){
//我们配置SimpleAdapter创建TextViews(见
//了'到'数组),所以这个转换应该是安全的:
TextView中的TextView =(TextView的)观点;
textView.setText(textRe presentation);
返回true;
}
};
simpleAdapter.setViewBinder(viewBinder);
我的博客上讲述这个<一href="http://www.outofwhatbox.com/blog/2010/12/android-spinners-simpleadapter-and-maybe-viewbinder/">here.
I am new to Android development. I am trying to populate a spinner by using the SimpleAdapter. But spinner's list is showing blank element. When I click any element, its text is shown properly in Toast. Please tell me what is the problem in my code below.
public void onCreate(Bundle savedInstanceState) {
private List<Map<String, String>> data = new ArrayList<Map<String, String>>();
String[] from = new String[] { "colorsData" };
int[] to = new int[] { R.id.spinner };
String[] colors = getResources().getStringArray(R.array.colorsData);
for (int i = 0; i < colors.length; i++) {
data.add(addData(colors[i]));
}
Spinner spinner = (Spinner) findViewById(R.id.spinner);
SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, android.R.layout.simple_spinner_item, from, to);
simpleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(simpleAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(
parent.getContext(),
"Selected Color:- "
+ parent.getItemAtPosition(position),
Toast.LENGTH_LONG).show();
}
});
}
private Map<String, String> addData(String colorName) {
Map<String, String> mapList = new HashMap<String, String>();
mapList.put("colorsData", colorName);
return mapList;
}
I'm about 95% sure that your to
array should be declared as:
int[] to = new int[] { android.R.id.text1 };
Give that a try.
EDIT (based on comments below):
It seems there was a bug in older versions of AndroidOS which caused that IllegalStateException. (I didn't see the exception in 2.2, but I did see it in 1.5 in the emulator.) The bug can be worked around by adding a ViewBinder to the SimpleAdapter. ViewBinder isn't hard to implement; here's an example:
SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
public boolean setViewValue(View view, Object data,
String textRepresentation) {
// We configured the SimpleAdapter to create TextViews (see
// the 'to' array), so this cast should be safe:
TextView textView = (TextView) view;
textView.setText(textRepresentation);
return true;
}
};
simpleAdapter.setViewBinder(viewBinder);
I blogged about this here.
这篇关于使用SimpleAdapter有微调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!