简体   繁体   中英

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. 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.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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