问题描述
- 为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); }
}
解决方案
oncreate()方法里的代码,可以写在oncreateview里面,有点看不大懂你的代码
时间: 2025-01-24 09:16:14