【问题标题】:android load contact photos to a listandroid将联系人照片加载到列表中
【发布时间】:2010-09-30 10:09:59
【问题描述】:

你好,我有这个代码:`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;
    }
}`

它在列表中加载一些任意数据数组并设置一些示例图标,我希望能够在用户名旁边使用列表中的用户联系人,例如该列表将与用户在他的手机上拥有的联系人一样多,并且在每个联系人中都有照片加载联系人姓名旁边的照片否则加载空白图像,这可能吗?以及如何?

【问题讨论】:

    标签: android


    【解决方案1】:

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

    对于最高 1.6 的 android 版本,您将使用 Contacts.People,对于 2.0 及更高版本,您需要使用 ContactsContract.Contacts。这两者都可以让您访问联系人显示名称和默认照片。

    有很多关于如何使用这些的示例,包括 android 网站上的一些官方示例。

    这里有一篇文章可以帮助您开始使用新的联系人模型: http://developer.android.com/resources/articles/contacts.html

    它包含一些有用的代码 sn-ps 可以帮助您入门。

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

    【讨论】:

    • 您好,我想读取不插入的数据并能够在列表中显示它们
    • 当我说“添加联系人”时,我指的是您的列表而不是数据库。获取联系人数据以显示在您的列表中的过程仍然如我上面列出的那样。如果您想要一种更简单的方式来显示联系人,我可能会建议扩展 SimpleCursorAdapter 而不是 BaseAdapter。如果您愿意,您仍然可以使用 BaseAdapter,您只需自己完成所有光标工作。这是一个示例链接,可帮助您开始访问联系人:developer.android.com/resources/samples/BusinessCard/index.html
    • android 开发人员没有很好地解释任何事情。通常,它的作用更像是一本字典,而不是理解如何完成一项任务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 2017-07-10
    • 2014-10-23
    相关资源
    最近更新 更多