先列出code:(直接copy过来的)
public class MySimpleAdapterActivity extends Activity {
private ListView simpleAdapterlistview;
public String[] strings;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_adapter_main);
simpleAdapterlistview = (ListView)findViewById(R.id.simpleAdapterlistview);
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
for (int j = 0; j < 5; j++) {
Map<String,String> myMap = new HashMap<String, String>();
myMap.put("1", "yang "+Integer.toString(j));
list.add(myMap);
}
MySimpleAdapterDome mySimpleAdapterDome = new MySimpleAdapterDome(this, list, R.layout.simple_adapter_item,new String[]{"1"}, new int[]{R.id.simpleAdaptertextViewId});
mySimpleAdapterDome.setViewBinder(new MyViewBinder());
simpleAdapterlistview.setAdapter(mySimpleAdapterDome);
}
class MySimpleAdapterDome extends SimpleAdapter{
int i=0;
public MySimpleAdapterDome(Context context,
List<Map<String,String>> data, int resource,
String[] from, int[] to) {
super(context, data, resource, from, to);
// TODO Auto-generated constructor stub
}
@Override
public void setViewImage(ImageView v, String value) {
// TODO Auto-generated method stub
super.setViewImage(v, value);
//i++;
Log.i("1", "setViewImage"+i);
}
@Override
public void setViewText(TextView v, String text) {
// TODO Auto-generated method stub
super.setViewText(v, text);
i++;
Log.i("1", "setViewText"+i);
}
}
}
class MyViewBinder implements android.widget.SimpleAdapter.ViewBinder{
int i=0;
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
// TODO Auto-generated method stub
i++;
if (view instanceof ImageView && data instanceof Bitmap) {
ImageView imageView = (ImageView)view;
Bitmap bitmap = (Bitmap)data;
Log.i("1", "ImageView"+i);
return true;
}else if (view instanceof TextView && data instanceof String) {
TextView textView = (TextView)view;
textView.setText("cursor");
Log.i("1", "TextView"+ i);//重要问题出在这里,见下面的《note》
return true;
}
return false;
}
}
《note》:上面的Log.i("1", "TextView"+ i)是用来记录setViewValue(view,data,string)方法执行的次数并显示出来的,
但是当然测试代码时发现在LogCat中显示TextView * 的次数是我要显示数据的长度的3倍
//这里是我要显示的数据,长度为5,不过不在下图显示出来,但问题不出在这里,
//问题却是为什么setViewValue(view,data,string)方法执行的次数是其3倍呢
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
for (int j = 0; j < 5; j++) {
Map<String,String> myMap = new HashMap<String, String>();
myMap.put("1", "yang "+Integer.toString(j));
list.add(myMap);
}
见下图:
我就在这里不知道为什么原因了?
难道是:
//这里摘自Android中文文档中的SimpleAdapter类的概述、
类概述
这是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图。你可以指定数据支持的列表如ArrayList组成的Map。在ArrayList中的每个条目对应List中的一行。Maps包含每行数据。你可以指定一个定义了被用于显示行的视图XML文件,通过关键字映射到指定的视图。绑定数据到视图分两个阶段,首先,如果一个SimpleAdapter.ViewBinder是有效的,setViewValue(android.view.View, Object, String)将被调用。如果返回值是真,绑定完成了。如果返回值为假,下面的视图将按以下顺序去处理:
l 一个实现了Checkable的视图(例如CheckBox),期望绑定值是一个布尔类型。
l TextView期望绑定值是一个字符串类型,通过调用setViewText(TextView, String)绑定。
l ImageView期望绑定值是一个资源id或者一个字符串,通过调用setViewImage(ImageView, int) 或 setViewImage(ImageView, String)。
如果没有一个合适的绑定发生将会抛出IllegalStateException。
===================HELP===================HELP============HELP=====================================
问题到此,请问各位谁知道为什么它会执行三次的。
求解。。。。小弟不胜感激