String[] fields=
new String[]{"foodname","price","taste","stuff","make","image"
};
final int[] Id=
new int[]{R.id.FoodName,R.id.FoodPrice,R.id.FoodTaste,R.id.FoodStuff,R.id.FoodMake,R.id.FoodPhoto};
@SuppressWarnings("deprecation"
)
ListAdapter adapter=
new SimpleCursorAdapter(
this, R.layout.fooddetil_content, cursor, fields, Id); listview.setadapter(adapter);
FoodDetilsListView.setAdapter(adapter);
//将数据绑定到自定义中的各个空中显示出来
((SimpleCursorAdapter) adapter).setViewBinder(
new ViewBinder ()
{
public boolean setViewValue(View view, Cursor cursor,
int columnIndex)
{
for(
int i=0;i<6;i++
)
{
if(view.findViewById(Id[i])
instanceof ImageView)
{
ImageView imageView =
(ImageView) view.findViewById(Id[i]);
//得到数据库中存放的图片信息,并把它存放到byte数组中
byte[] image=cursor.getBlob(cursor.getColumnIndex("image"
));
//把数组中的数据封装到bitmap对象中,让后通过setimagebitmap方法显示到imageView控件中
Bitmap bitmap=BitmapFactory.decodeByteArray(image, 0
, image.length);
imageView.setImageBitmap(bitmap);
}
else if(view.findViewById(Id[i])
instanceof TextView)
{
try {
if(i==0
)
{
//获取textview控件
TextView name=
(TextView) view.findViewById(Id[i]);
//通过cursor对象找到foodname字段,并设置它的编码格式,然后显示到控件中去
String FoodName=
new String(cursor.getString(cursor.getColumnIndex("foodname")).getBytes(),"utf-8"
);
name.setText(FoodName);
}else if(i==1
)
{
TextView FoodPrice=
(TextView) view.findViewById(Id[i]);
String FoodPrices =
new String(cursor.getString(cursor.getColumnIndex("price")).getBytes(),"utf-8"
);
FoodPrice.setText("售价:"+FoodPrices+"元(每 份/个)"
);
}
else if(i==2
)
{
TextView FoodTaste=
(TextView) view.findViewById(Id[i]);
String taste=
new String(cursor.getString(cursor.getColumnIndex("taste")).getBytes(),"utf-8"
);
FoodTaste.setText("口味:"+
taste);
}
else if(i==3
)
{
TextView FoodStuff=
(TextView) view.findViewById(Id[i]);
String stuff=
new String(cursor.getString(cursor.getColumnIndex("stuff")).getBytes(),"utf-8"
);
FoodStuff.setText("食材: "+
stuff);
}
else
{
String Make = ""
;
TextView FoodMake=
(TextView) view.findViewById(Id[i]);
String make=cursor.getString(cursor.getColumnIndex("make"
));
String[] a=make.split("/"
);
for(
int b=0;i<a.length;b++
)
{
Make+=a[b]+"\n\n"
;
FoodMake.setText(Make);
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
return true;
}});
转载于:https://www.cnblogs.com/tianshidechibang234/p/3287547.html
相关资源:SimpleCursorAdapter简单实例