import java.io.ByteArrayInputStream;
import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import edu.spring.user.R;public class MyAdapter extends CursorAdapter
{
private LayoutInflater layoutInflater;
public MyAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
System.out.println("执行自定义适配器构造函数");
} public void bindView(View view, Context context, Cursor cursor) {
System.out.println("bindview");
setChildView(view,cursor);
} public View newView(Context context, Cursor cursor, ViewGroup parent) {
System.out.println("newView");
layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(cursor.getCount()== 0)
{
System.out.println("getCount");
return null;
}
else
{
System.out.println("else");
View view = layoutInflater.inflate(R.layout.dish_item, null);
setChildView(view,cursor);
return view;
}
}
private void setChildView(View view,Cursor cursor)
{
if(cursor.isBeforeFirst())
{
cursor.moveToFirst();
}
System.out.println("setview");
ImageView image = (ImageView)view.findViewById(R.id.dish_image);
System.out.println("image");
TextView name = (TextView)view.findViewById(R.id.dish_name);
TextView nprice = (TextView)view.findViewById(R.id.dish_nprice);
TextView mprice = (TextView)view.findViewById(R.id.dish_mprice);
name.setText(cursor.getString(cursor.getColumnIndex("name")));
System.out.println(cursor.getString(cursor.getColumnIndex("name")));
nprice.setText(cursor.getString(cursor.getColumnIndex("price")));
mprice.setText(cursor.getString(cursor.getColumnIndex("m_price")));
byte[] picture = cursor.getBlob(cursor.getColumnIndex("picture"));
System.out.println(picture);
ByteArrayInputStream bais = new ByteArrayInputStream(picture);
image.setImageDrawable(Drawable.createFromStream(bais, "picture"));
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货