代码之家  ›  专栏  ›  技术社区  ›  maxsap

Android将联系人照片加载到列表中

  •  0
  • maxsap  · 技术社区  · 15 年前

    您好,我有以下代码:`public class maxsaplistadapter extends baseadapter{

        private LayoutInflater mInflater;
        private Bitmap mIcon1;
        private Bitmap mIcon2;
        private Bitmap mIconCall;
        private String[] DATA;
    
        MaxsapListAdapter(Context context, String[] DATA) {
            // Cache the LayoutInflate to avoid asking for a new one each time.
            mInflater = LayoutInflater.from(context);
    
            // Icons bound to the rows.
            mIcon1 = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.icon48x48_1);
            mIcon2 = BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.icon48x48_2);
            mIconCall = BitmapFactory.decodeResource(context.getResources(), R.drawable.call);
            this.DATA = DATA;
        }
    
        /**
         * The number of items in the list is determined by the number of speeches
         * in our array.
         * 
         * @see android.widget.ListAdapter#getCount()
         */
        public int getCount() {
            return DATA.length;
        }
    
        /**
         * Since the data comes from an array, just returning the index is sufficent
         * to get at the data. If we were using a more complex data structure, we
         * would return whatever object represents one row in the list.
         * 
         * @see android.widget.ListAdapter#getItem(int)
         */
        public Object getItem(int position) {
            return position;
        }
    
        /**
         * Use the array index as a unique id.
         * 
         * @see android.widget.ListAdapter#getItemId(int)
         */
        public long getItemId(int position) {
            return position;
        }
    
        /**
         * Make a view to hold each row.
         * 
         * @see android.widget.ListAdapter#getView(int, android.view.View,
         *      android.view.ViewGroup)
         */
        public View getView(int position, View convertView, ViewGroup parent) {
            // A ViewHolder keeps references to children views to avoid unneccessary
            // calls
            // to findViewById() on each row.
            ViewHolder holder;
    
            // When convertView is not null, we can reuse it directly, there is no
            // need
            // to reinflate it. We only inflate a new View when the convertView
            // supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
    
                // Creates a ViewHolder and store references to the three children
                // views
                // we want to bind data to.
                holder = new ViewHolder();
                holder.text = (TextView) convertView.findViewById(R.id.text);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);
                holder.btn = (ImageButton) convertView.findViewById(R.id.button);
                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (ViewHolder) convertView.getTag();
            }
            // Bind the data efficiently with the holder.
            holder.text.setText(DATA[position]);
            holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
            holder.btn.setImageBitmap(mIconCall);
            holder.btn.setId((int)this.getItemId(position));
            holder.btn.setFocusable(false);
            holder.btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                          Log.e("maxsap","--------*ButtonClicked*---------");
                  Log.e("maxsap","--------"+v.setTag(key, tag)+"---------");
            Intent callIntent = new Intent(v.getContext(),PhoneCall.class);
                    Log.e("maxsap",PhoneCall.class.toString());
                    startActivity(callIntent);  
                }
            });
            return convertView;
        }
    
         class ViewHolder {
            TextView text;
            ImageView icon;
            ImageButton btn;
        }
    }`
    

    它在列表中加载一些任意的数据数组并设置一些示例图标,我希望能够在用户名旁边的列表中使用用户联系人,例如,列表中的行数与用户在电话中的联系人数相同,并且在每个联系人中,联系人旁边都有一张照片加载。s name否则加载空白图像,是否可能?怎么办?

    1 回复  |  直到 11 年前
        1
  •  0
  •   Charles B    15 年前

    如果要将联系人和照片添加到列表中,则需要访问联系人API。

    对于1.6及以下的Android版本,您将使用contacts.people,对于2.0及更高版本,您将希望使用contacts.contacts。这两个选项都可以让您访问联系人显示名称和默认照片。

    有很多关于如何利用这些的例子,包括Android网站上的一些官方例子。

    下面是一篇文章,您应该了解新的联系人模型: http://developer.android.com/resources/articles/contacts.html

    它包括一些有用的代码片段,可以帮助您开始工作。

    哦,别忘了添加在清单中显示联系人的权限。:)