android - 如何在自定义适配器类中使用联系人填充 ListView?

标签 android android-listview contacts

我有一个自定义适配器。在我的 getView() 中,我试图用我的联系人列表填充 "holder.contact",但没有成功。我确实有这种用联系人填充 ListView 的方法,但它会破坏我的 CheckBoxes 的功能。此代码在我的 CustomAdapter 类之外:

public void populateContactList() {
    // Build adapter with contact entries
    Cursor cursor = getContacts();
    String[] fields = new String[] {
            ContactsContract.Data.DISPLAY_NAME       
    };

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
            fields, new int[] {R.id.contactEntryText});    
    lv.setAdapter(adapter);    
} // END POPULATECONTACTLIST

public Cursor getContacts()
{ 
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME        
    };

    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
            (false ? "0" : "1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
} // END GETCONTACTS 

但我试图在这里定义它,但没有成功(自定义适配器类):

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    View rowView = convertView;
    ViewHolder holder = null;

    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(
                                           Context.LAYOUT_INFLATER_SERVICE);
        // HERE I AM INFLATING LISTVIEW LAYOUT.
        rowView = inflater.inflate(R.layout.contact_entry, null, false);
        holder = new ViewHolder();
        holder.photo = (ImageView) rowView.findViewById(R.id.iv_contactPic);
        holder.contact = (TextView) rowView.findViewById(R.id.contactEntryText);
        holder.tv = (TextView) rowView.findViewById(R.id.SMSCounttext);
        holder.cb = (CheckBox) rowView.findViewById(R.id.cb_contactHistoryChk);


        rowView.setTag(holder);

    } else {
        holder = (ViewHolder) rowView.getTag();
    }

有没有办法在 getView() 中应用我的 populateContactList() 方法?

我的构造函数看起来像这样。 “元素”已被另一个 TextView 使用:

public CustomAdapter(Context context, int type, ArrayList<String> elements){
    super(context, type, elements);

最佳答案

你的问题有点模棱两可。您有 populateContactList() 将带有 Contacts 数据的 Cursor 绑定(bind)到 SimpleCursorAdapter 并且您想使用它另一个自定义 AdaptergetView() 方法中的方法(如果是这种情况,请不要这样做)?或者您想使用自己的适配器并将数据与联系人绑定(bind)到它(如果是这种情况,则使用查询的结果 cursor,以及您自己的 Adapter)?

您没有说明您的自定义 Adapter 扩展了哪种类型的 Adapter。如果它是一个基于 CursorAdapter,事情就很简单了,你可以使用 bindViewnewView 来访问游标数据(并且您将 Cursor 从构造函数中的 getContacts() 传递给父类(super class))。否则,将使用 getContacts() 获得的 Cursor 中的数据放入类似 ArrayList 的结构中,并将其用作适配器的数据。 (我建议选择第一个选项)。

此外,维护行布局中 CheckBoxes 的状态是您的工作(这里有很多关于如何执行此操作的问题)。

编辑:

    ArrayList<String> names = new ArrayList<String>();
    Cursor cursor = getContacts();
    while (cursor.moveToNext()) {
         names.add(cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME )));

}

编辑:
使用 elements ArrayList 并将其绑定(bind)到行:

    //...
    private ArrayList<String> data;

    public CustomAdapter(Context context, int type, ArrayList<String> elements){
        super(context, type, elements);
        this.data = elements;
    }

    public int getCount() {
        return data.size();  
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder holder = null;

        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(
                                               Context.LAYOUT_INFLATER_SERVICE);
            // HERE I AM INFLATING LISTVIEW LAYOUT.
            rowView = inflater.inflate(R.layout.contact_entry, parent, false); //<= use parent instead of null
            holder = new ViewHolder();
            holder.photo = (ImageView) rowView.findViewById(R.id.iv_contactPic);
            holder.contact = (TextView) rowView.findViewById(R.id.contactEntryText);
            holder.tv = (TextView) rowView.findViewById(R.id.SMSCounttext);
            holder.cb = (CheckBox) rowView.findViewById(R.id.cb_contactHistoryChk);
            rowView.setTag(holder);
        } else {
            holder = (ViewHolder) rowView.getTag();
        }
        //bind the data to the row views
        String contactName = data.get(position);
        holder.contact.setText(contactName);
        // other stuff you might do to build thw row
//...

关于android - 如何在自定义适配器类中使用联系人填充 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10548612/

相关文章:

Android - 使用 AsycTaskLoader 和 MySql 填充列表

java - 如何导出vCard 3.0?

ios - 当用户添加新联系人时更新应用程序核心数据/Realm

android - 如何在 Jetpack Compose 导航期间保存 LazyColumn 的分页状态

android - 从 HttpRequest 获取 POST 数据 (Android)

android - Android apk 构建的条件资源包含/排除

contacts - 如何使用共享联系人按钮为 iOS8 Apple 联系人应用构建扩展

java - java方法接受null类数组的kotlin反射

java - 如何通过动态设置id动态添加自定义布局?

android - Linkify、ListView 和 ActionMode。常规文本不再可点击