如何使用浮动图标 return 所有复选框值到字符串

How to return all checkbox values to a string using a floating icon

我不确定如何从包含复选框的列表视图中 return 值。总的来说,我对 Android 和 ArrayAdapters 还很陌生,所以请多多包涵。

这是我的列表适配器,有两个文本视图和一个复选框

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="fill_parent"
    android:layout_height="match_parent">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/checkBox"
        android:checked="false"
        android:layout_alignBottom="@+id/NumberView"
        android:layout_alignParentTop="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/NameView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:paddingRight="20dp"
        android:textSize="28sp"
        android:textIsSelectable="true"
        android:layout_marginTop="8dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/NumberView"
        android:layout_below="@+id/NameView"
        android:paddingRight="20dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="5dp" />

</RelativeLayout>

activity_mail 上的按钮调用此:

 public void grabthecontacts(View view) {
    setContentView(R.layout.selectcontacts);
    populateListView(view);
}

现在我不确定在下一个函数之后要做什么,这就是创建和填充列表的原因,但是我如何获得结果?

这是我的 populatelistview 函数,它提取所有人员联系人,然后将其放入适配器

public void populateListView(View view) {
    try {
        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

        while (phones.moveToNext()) {
            String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            list.add(new Person(name, phoneNumber));
        }
        phones.close();
    }
    catch (Exception e){
        e.printStackTrace();
    }
    ArrayAdapter<Person> adapter = new myListAdapter();
    ListView listview2 = (ListView) findViewById(R.id.contactlistview);
    listview2.setAdapter(adapter);
}

public class myListAdapter extends ArrayAdapter<Person> {

    public myListAdapter() {
        super(MainActivity.this, R.layout.da_item, list);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if (itemView == null) {
            itemView = getLayoutInflater().inflate(R.layout.da_item, parent, false);
        }
        // Find person wot work with
        Person currentperson = list.get(position);

        // Fill the view
        TextView nameboxview = (TextView)itemView.findViewById(R.id.NameView);
        nameboxview.setText(currentperson.getName());

        TextView numberboxview = (TextView)itemView.findViewById(R.id.NumberView);
        numberboxview.setText(currentperson.getPhone());



        return itemView;
    }
}

制作你自己的 Person class 它将有一个 boolean 实例变量,它会告诉你是否选择了特定的项目。

/**
 * Created by Pankaj Nimgade on 19-12-2015.
 */
public class Person {

    private boolean isChecked;
    private String phoneNumber;

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean checked) {
        isChecked = checked;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

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

制作一个 ArrayList 这个 class 类型并填充你的 ListView.

对您的适配器进行少量更改...添加以下代码以跟踪选择了哪些项目。

CheckBox checkBox = (CheckBox)itemView.findViewById(R.id.checkBox);
if (checkBox.isChecked()) {
   person.setChecked(true);
}

填充列表视图后

ArrayList<Person> searchList = searchList(list);

以下是查找已检查项目的代码。

private ArrayList<Person> searchList(ArrayList<Person> list) {
    ArrayList<Person> tempList = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i).isChecked()) {
            tempList.add(list.get(i));
        }
    }
    return list;
}