简体   繁体   English

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

[英]Set bind view in simple adapter?

I want to setup view binder in simple adapter to show photos from contacts, however I set two text view's with name and number with Hash Map, so third value is Image View where I want to put contact photo corresponding to contact ID. 我想在简单的适配器中设置视图活页夹以显示联系人的照片,但是我使用哈希映射设置了两个带有名称和编号的文本视图,因此第三个值是图像视图,我想在其中放置与联系人ID对应的联系人照片。 Thank you in advance, Wolf. 预先谢谢你,沃尔夫。

Here is my code : 这是我的代码:

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);

and my view binder is : 我的观点粘合剂是:

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;

            }
        };

but it's not working. 但它不起作用。 Help please??? 请帮助???

Yes its possible, you just create your own adapter (extends BaseAdapter), override getView method and there add bitmap to imageview. 是的,它是可能的,您只需创建自己的适配器(扩展BaseAdapter),覆盖getView方法,然后在其中将位图添加到imageview。

    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);
    }

Something like this. 这样的事情。 You have to extends this. 您必须扩展它。 Check also : Lazy load of images in ListView 还检查: ListView中的图像的延迟加载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM