weixin_31119207
weixin_31119207
2015-11-23 03:54

为Fragment中的ListView中的ImageView赋值在setAdapter()时报错

  • simpleadapter
  • fragment
  • list

public class MyFragment3 extends ListFragment{
private ListView lv;
private Context context;
private List> data;
private Map map;

public MyFragment3(Context context){
    this.context = context;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.mylog,container,false);
    lv = (ListView) view.findViewById(R.id.log_lv);
    return view;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dataInit();
    SimpleAdapter adapter = new SimpleAdapter(context,data,R.layout.log_item,
            new String[]{"pic"},
            new int[]{R.id.log_pic});

    adapter.setViewBinder(new ViewBinder() {
        @Override
        public boolean setViewValue(View view, Object data,
                String textRepresentation) {
            if(view instanceof ImageView & data instanceof Bitmap){
                ImageView iv = (ImageView) view;
                Bitmap bt = (Bitmap) data;
                iv.setImageBitmap(bt);
                return true;
            }
            return false;
        }
    });

    lv.setAdapter(adapter);
}



@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

public void dataInit(){
    InputStream in = getResources().openRawResource(R.drawable.a);
    Bitmap bit = BitmapFactory.decodeStream(in);

    InputStream in1 = getResources().openRawResource(R.drawable.b);
    Bitmap bit1 = BitmapFactory.decodeStream(in1);

    data = new ArrayList<Map<String,Object>>();

    map = new HashMap<String,Object>();
    map.put("pic", bit);
    data.add(map);

    map = new HashMap<String,Object>();
    map.put("pic", bit1);
    data.add(map);
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答