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

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个回答

oncreate()方法里的代码,可以写在oncreateview里面,有点看不大懂你的代码

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问