在简单适配器中设置绑定视图?

时间:2012-02-02 11:18:51

标签: java android

我想在简单的适配器中设置视图活页夹以显示来自联系人的照片,但是我使用哈希地图设置了两个带有名称和编号的文本视图,因此第三个值是图像视图,我想在其中放置与联系人ID对应的联系人照片。 沃尔夫,提前谢谢你。

这是我的代码:

ArrayList<HashMap<String, String>> mapa = new ArrayList<HashMap<String, String>>();

            ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

            if(cur.getCount() > 0){
                while(cur.moveToNext()){
                    id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                    String photoUri = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));

                    if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){

                        final Cursor numCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "  = ?", new String[]{id}, null);


                        for(numCur.moveToFirst(); !numCur.isAfterLast(); numCur.moveToNext()){

                            brTel = numCur.getString(numCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            ime = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


                            tmpIme = new String[] {ime};


                            for(int i = 0; i < tmpIme.length; i++){

                                HashMap<String, String> imeMapa = new HashMap<String, String>();
                                imeMapa.put("imeLista", ime);
                                imeMapa.put("checkBox", photoUri);
                                imeMapa.put("Mobilni", brTel);
                                mapa.add(imeMapa);

                            }

                        }
                        numCur.close();

                    }

                } // While
            }

            SimpleAdapter sa = new SimpleAdapter(getApplicationContext(), mapa, R.layout.imenik, new String[] {"imeLista", "checkBox", "Mobilni"}, new int[] {R.id.tvImeImenik, R.id.cbOznaci, R.id.tvSamoProba});
            sa.setViewBinder(simpleSlika);
            lImenik.setAdapter(sa);

我的视图活页夹是:

private final SimpleAdapter.ViewBinder simpleSlika = new SimpleAdapter.ViewBinder() {

            public boolean setViewValue(View view, Object data,
                    String textRepresentation) {

                if (view instanceof ImageView && data instanceof Bitmap) {
                    ImageView v = (ImageView)view;
                    v.setImageBitmap((Bitmap)data);
                    // return true to signal that bind was successful
                    return true;
                }
                return false;

            }
        };

但它不起作用。 请帮忙???

1 个答案:

答案 0 :(得分:0)

是的,可以创建自己的适配器(扩展BaseAdapter),覆盖getView方法,并在bitview中添加位图。

    public ContactAdapter(Activity a,ArrayList<Object> list)
    {
            activity = a;
            inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
          View v=convertView;
          if(convertView==null)
          v = inflater.inflate(R.layout.contact, null);
          ImageView image = (ImageView)v.findViewById(R.id.img);
    }

像这样的东西。你必须扩展这个。 另请参阅:Lazy load of images in ListView

相关问题