gpt4 book ai didi

Android ListView 复选框选择

转载 作者:太空狗 更新时间:2023-10-29 13:42:23 25 4
gpt4 key购买 nike

我这里有一个由两部分组成的问题。

2) 我有一个 ListView,它使用 multipleChoice 模式进行项目选择。它充满了我的联系人列表中的名字。当我在 ListView 中选择一个项目时,我希望该选定项目触发对我的 SqLite 例程的调用,以将值存储到数据库记录中。当在 ListView 中选中一个项目时,如何触发此事件?

这是我的布局 XML;

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

<ListView
android:id="@+id/lvContacts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:choiceMode="multipleChoice"
/>

</LinearLayout>

这是我用来填充 ListView 的代码;

    private void fillData() {
final ArrayList<String> contacts = new ArrayList<String>();

// Let's set our local variable to a reference to our listview control
// in the view.
lvContacts = (ListView) findViewById(R.id.lvContacts);

String[] proj_2 = new String[] {Data._ID, Phone.DISPLAY_NAME, CommonDataKinds.Phone.TYPE};
cursor = managedQuery(Phone.CONTENT_URI, proj_2, null, null, null);
while(cursor.moveToNext()) {
// Only add contacts that have mobile number entries
if ( cursor.getInt(2) == Phone.TYPE_MOBILE ) {
String name = cursor.getString(1);
contacts.add(name);
}
}

// Make the array adapter for the listview.
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
contacts);

// Let's sort our resulting data alphabetically.
aa.sort(new Comparator<String>() {
public int compare(String object1, String object2) {
return object1.compareTo(object2);
};
});

// Give the list of contacts over to the list view now.
lvContacts.setAdapter(aa);
}

我曾希望能够为每个选中的项目使用类似 onClick 事件的东西,但没有取得任何进展。

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

我认为您的问题的解决方案是使用自定义列表适配器,每个项目都包含联系人姓名和联系人 ID,这里是详细信息:

1) 尝试创建包含 2 个属性的自定义联系人项 bean:contactID、contactName

public class contactItem{
private long contactID;
private String contactName;

//...
}

创建 CustomContactAdapter:

public class CustomContactAdapter extends ArrayAdapter<contactItem>{

ArrayList<contactItem> itemList = null;

//Constructor
public CustomContactAdapter (Context context, int MessagewResourceId,
ArrayList<contactItem> objects, Handler handler) {
//Save objects and get LayoutInflater
itemList = objects;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
final ReceiveMailStruct contact= items.get(position);

if (contact!= null) {
view = inflater.inflate(R.layout.layout_display_contact_item, null);
//Set view for contact here
}
}

}

搜索自定义适配器以获取更多信息

2) 要处理 ListView 的点击事件,您必须为 listitem 注册一个处理程序:第 1 步:将处理程序注册到列表项:

lvContacts.setOnItemClickListener(new HandlerListClickEvent());

Step2:实现点击item时的流程(勾选/取消勾选)

class HandlerListClickEvent implements OnItemClickListener {
public void onItemClick( AdapterView<?> adapter, View view, int position, long id ) {
//Get contact ID here base on item position
}

希望对你有帮助问候,

关于Android ListView 复选框选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4141124/

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