gpt4 book ai didi

java - 如何使用 float 图标将所有复选框值返回到字符串

转载 作者:行者123 更新时间:2023-11-30 10:52:23 25 4
gpt4 key购买 nike

我不确定如何从包含复选框的 ListView 中返回值。总的来说,我对 Android 和 ArrayAdapter 还比较陌生,所以请多多包涵。

这是我的列表适配器,有两个 TextView 和一个复选框

    <?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 类,它有一个 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 并填充您的 ListView

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

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

在你的 ListView 被填充之后

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;
}

关于java - 如何使用 float 图标将所有复选框值返回到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34366921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com