获取通讯录数据后布局在自定义的adapter上,怎么获取到checkbox选中的数据?具体怎么实现? 10C

1、实体类
package com.app.bean;

import com.app.dao.PinyinTool;

/**

  • Created by dell-1 on 2017/11/8. */

public class ContactsBean {
private int contactId;
private int icon;
private String contactsName;
private String company;
private String phoneNumber;
private String remark;
private String job;
private String email;
private Boolean selectedContact=false;
public Boolean getSelectedContact() {
return selectedContact;
}

public void setSelectedContact(Boolean selectedContact) {
    this.selectedContact = selectedContact;
}



public String getSort_key_primary() {
    return sort_key_primary;
}

public void setSort_key_primary(String sort_key_primary) {
    this.sort_key_primary = sort_key_primary;
}

private String sort_key_primary;


public String getJob() {
    return job;
}

public void setJob(String job) {
    this.job = job;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}



public int getContactId() {
    return contactId;
}

public void setContactId(int contactId) {
    this.contactId = contactId;
}

public int getIcon() {
    return icon;
}

public void setIcon(int icon) {
    this.icon = icon;
}

public String getContactsName() {
    return contactsName;
}

public void setContactsName(String contactsName) {
    this.contactsName = contactsName;
}

public String getCompany() {
    return company;
}

public void setCompany(String company) {
    this.company = company;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
}

public String getRemark() {
    return remark;
}

public void setRemark(String remark) {
    this.remark = remark;
}

//获取名字拼音首字母
public String getFirstLetterOfName(){
String key="";
if (sort_key_primary!=null){
key= PinyinTool.getFirstSpell(sort_key_primary);

    }
    return key;
}

}
2、主程序
package com.app.com.kag.activity;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.addresslist.R;
import com.app.FragmentAdapter.AddContactFromAddressAdapter;
import com.app.bean.ContactsBean;
import com.app.dao.ContactsDao;

import java.util.ArrayList;
import java.util.List;

public class AddContactFromAddressList extends AppCompatActivity {
private ContactsBean contactsBean;
private ListView lv;
private ArrayList contacts;
private ContactsDao dao;
private Button mAddContacts;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_contact_from_address_list);

   init();
}
private void init(){
    lv = findViewById(R.id.lv_contacts_list);
    mAddContacts=findViewById(R.id.btn_add_contacts);
    //获取通讯录数据并布局到adapter
    contacts = new ArrayList();
    reaContacts(contacts);
    final AddContactFromAddressAdapter adapter = new AddContactFromAddressAdapter(this, contacts);
    lv.setAdapter(adapter);

//

//添加按钮的点击事件
mAddContacts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
//从手机通讯录中读取数据
public List reaContacts(ArrayList contacts) {
Uri uri = Uri.parse("content://com.android.contacts/contacts");
ContentResolver resolver = this.getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, ContactsContract.Contacts.SORT_KEY_PRIMARY);
while (cursor.moveToNext()) {
//

        int id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

// String phone =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));//这行报错:Make sure the Cursor is initialized correctly before accessing data from it
String sort_key = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.SORT_KEY_PRIMARY));
contactsBean = null;
contactsBean = new ContactsBean();//此对象的创建必须在while循环内,不然只得到一条数据。
contactsBean.setContactsName(name);
contactsBean.setContactId(id);
contactsBean.setSort_key_primary(sort_key);
Cursor phone = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null, null);
ArrayList phoneNumber = new ArrayList();
while (phone.moveToNext()) {

            String number = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            phoneNumber.add(number);
            contactsBean.setPhoneNumber(String.valueOf(phoneNumber));
        }

        contacts.add(contactsBean);

// System.out.println(contactsBean.getContactsName() + ":" + contactsBean.getPhoneNumber()+sort_key);

    }

return contacts;

}

}

3、adapter
package com.app.FragmentAdapter;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.addresslist.R;
import com.app.bean.ContactsBean;

;import java.util.ArrayList;
import java.util.List;

/**

  • Created by dell-1 on 2018/1/9. */

public class AddContactFromAddressAdapter extends BaseAdapter {
//数据
private List contacts;

//是否选中
private ArrayList selectedList;
;
private LayoutInflater inflater;

public AddContactFromAddressAdapter(Context context, List<ContactsBean> contacts) {
    this.contacts = contacts;
    inflater = LayoutInflater.from(context);
    selectedList=new ArrayList();


}

@Override
public int getCount() {
    return contacts.size();
}

@Override
public Object getItem(int position) {
    return contacts.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.contacts_cell, null);
        holder = new ViewHolder();
        holder.sort_key = convertView.findViewById(R.id.tv_sort_key);
        holder.mPhones = convertView.findViewById(R.id.tv_phones);
        holder.checkBoxSelected = convertView.findViewById(R.id.cb_selected);
        convertView.setTag(holder);

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

//控件上添加数据
holder.sort_key.setText(contacts.get(position).getFirstLetterOfName());
holder.mPhones.setText(contacts.get(position).getContactsName() + ":"
+ contacts.get(position).getPhoneNumber());
if (contacts.get(position).getSelectedContact()) {
holder.checkBoxSelected.setChecked(true);
} else {
holder.checkBoxSelected.setChecked(false);
}
//checkbox点击事件判断
holder.checkBoxSelected.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (contacts.get(position).getSelectedContact()) {
contacts.get(position).setSelectedContact(false);
selectedList.remove(contacts.get(position));

            } else {
                contacts.get(position).setSelectedContact(true);
                //选中的添加到集合中
                selectedList.add(contacts.get(position));


            }

        }
    });

    return convertView;
}

private final class ViewHolder {
    public TextView sort_key;
    public TextView mPhones;
    public CheckBox checkBoxSelected;
}

}

4、运行的截图
图片说明

5个回答

private HashMap mSelectedGrouplists;//key=列表中展示的数据,value=position;
private List mList;//解决复用
private ArrayList selecteds;//上一个界面携带过来的数据集

public GroupAdapter() {
    mSelectedGrouplists = new HashMap<>();
    mList = new ArrayList<>();
    selecteds = new ArrayList<>();
}

public List<String> getDataSource() {
    return mDataSource;
}

/**
 * 回显
 *
 * @param list
 */
public void checkSelectedGrouplists(ArrayList<String> list) {
    if (list == null) {
        return;
    }
    selecteds = list;
    for (String s : list) {
        if (mDataSource.contains(s)) {
            int i = mDataSource.indexOf(s);
            if (!mSelectedGrouplists.containsKey(s))
                mSelectedGrouplists.put(s, i);
        }
    }

}
//获取选中的键值对集合
public HashMap<String, Integer> getSelectedGrouplists() {
    return mSelectedGrouplists;
}

@Override
public ClickableViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    bindContext(parent.getContext());
    View inflate = LayoutInflater.from(context).inflate(R.layout.group_item, parent, false);
    return new GroupViewHolder(inflate);
}

@Override
public void onBindViewHolder(ClickableViewHolder holder, final int position) {
    super.onBindViewHolder(holder, position);
    if (holder instanceof GroupViewHolder) {
        final GroupViewHolder gvh = (GroupViewHolder) holder;
        final String group = mDataSource.get(position);//group=="id:名称"
        String[] group_split = group.split(":");
        final String group_name = group_split[1];

        mList.clear();
        for (String key : mSelectedGrouplists.keySet()) {
            if (mDataSource.contains(key)) {
                int i = mDataSource.indexOf(key);
                mList.add(i);
            }

        }
        gvh.mGroupName.setText(group_name);//名称
        gvh.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    if (!mList.contains(position)) {
                        mList.add(position);
                    }
                    if (!selecteds.contains(group)) {
                        selecteds.add(group);
                    }
                    if (!mSelectedGrouplists.keySet().contains(group)) {//名称与id拼接的字符串
                        mSelectedGrouplists.put(group, position);
                    }
                } else {
                    if (mList.contains(position)) {
                        int i = mList.indexOf(position);
                        mList.remove(i);
                    }
                    if (selecteds.contains(group)) {
                        int i = selecteds.indexOf(group);
                        selecteds.remove(i);
                    }
                    if (mSelectedGrouplists.keySet().contains(group)) {
                        mSelectedGrouplists.remove(group);
                    }

                }
                itemClickListener.onItemClick(position, gvh);

            }
        });
        //回显
        if (selecteds.contains(group)) {
            if (!mList.contains(position))
                mList.add(position);
        }
        //解决复用问题
        gvh.mCheckBox.setChecked(mList.contains(position) ? true : false);

    }
}
 稍微修改后应该可以解决你的问题。

也可以这样的吧holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ if(!chooseNum.contains(position)){ System.out.println(position); chooseNum.add(position); } }else{ chooseNum.remove(Integer.valueOf(position)); } } });public ArrayList getChooseList(){ return chooseNum; }通过adpter.getChooseList获得选中的 行的数组。

holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ if(!chooseNum.contains(position)){ System.out.println(position); chooseNum.add(position); } }else{ chooseNum.remove(Integer.valueOf(position)); } } });public ArrayList getChooseList(){ return chooseNum; }

取到选中的 联系人唯一标识 然后根据唯一标示取数据库查询

将选中的position放入集合中,List mPosition;

 for(int i = 0;i < mPosition.size; i++){
ContactsBean contactsBean = contacts.get(i)
........
 }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问