gpt4 book ai didi

android - listView 项目上的多个 ToggleButton。单击一个时,其他人将打开/关闭

转载 作者:搜寻专家 更新时间:2023-11-01 09:10:55 25 4
gpt4 key购买 nike

我有一个包含多个 ToggleButton 项的 ListView 。当我点击一个时,另一个被打开/关闭。但是,它会执行与正确的 ListView 项目相关的操作。

它们在自定义适配器中进行管理,这是我的代码:

public class AdapterContacts extends BaseAdapter implements OnClickListener {

ToggleButton btnIsSending;
.
.
.

public View getView(int position, View convertView, ViewGroup viewGroup) {
Contact entry = contactsList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.contact_row, null);
}
.
.
.
// isSending button
btnIsSending = (ToggleButton) convertView.findViewById(R.id.btnIsSending);
btnIsSending.setFocusableInTouchMode(false);
btnIsSending.setFocusable(false);
btnIsSending.setOnClickListener(this);
btnIsSending.setTag(entry);

return convertView;
}

public void onClick(View view) {
final Contact entry = (Contact) view.getTag();
Log.d(TAG, "entry " + entry.getPhoneNr());

LookalizeData lookData = (LookalizeApp.getContext()).lookalizeData;
// Toggle send button
if( view.getId() == btnIsSending.getId()){
if(btnIsSending.isChecked())
...
else
...
}

还有其他按钮,它们工作正常。

有什么想法或经验可以分享吗?

最佳答案

在您的 getView 中,您必须执行类似这样的操作(这段代码让您了解它在您的情况下如何工作):

ListView lv = ((ListActivity)context).getListView();
// Containing all check states
SparseBooleanArray sba = lv.getCheckedItemPositions();

btnIsSending = (ToggleButton) convertView.findViewById(R.id.btnIsSending);
btnIsSending.setFocusableInTouchMode(false);
btnIsSending.setFocusable(false);
btnIsSending.setTag(entry);

btnIsSending.setChecked(false);

// Cursor is passed as an argument.
if(sba != null)
if(sba.get(cursor.getPosition()))
btnIsSending.setChecked(true);

这就是您在 Activity 方面所需要的。

您已通过 xml 中的 android:choiceMode 或使用 setChoiceMode 将您的 ListView 设置为多选模式。

您必须删除按钮上的 onClick 监听器。无论您在按钮的 onClick 中做什么,您都必须将该逻辑添加到 ListActivtiyonListItemClick

关于android - listView 项目上的多个 ToggleButton。单击一个时,其他人将打开/关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8409900/

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