gpt4 book ai didi

java - 如何知道 ListView 特定行的复选框值

转载 作者:行者123 更新时间:2023-12-01 12:38:40 24 4
gpt4 key购买 nike

嘿,我怎么知道单击时下面的复选框来自列表的哪一行。有什么方法可以知道这一点吗?每次 yo 都会返回 false,这是它的默认值。

checkBox.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
callBlockOptions callBlockOptions = (callBlockOptions) cb.getTag();

callBlockOptions.setChecked( cb.isChecked() );

String yo;

if(callBlockOptions.getPosition()=="0")
{
callBlockOptions.setAllCalls();
}


if(callBlockOptions.getAllCalls() ==true)
yo="true";
else
yo="false";

Toast.makeText(getContext(), callBlockOptions.getPosition(), Toast.LENGTH_LONG).show();

}
});
}

最佳答案

查看代码后,您可能想要尝试的一件事是为 ListView 中的各个子项设置监听器。你可以这样做:

ListView listView = (ListView)findViewById(R.id.listView);
int count = listView.getChildCount();
for (int x = 0; x < count; x++)
{
Class<? extends View> c = listView.getChildAt(x).getClass();
if (c == CheckBox.class)
{
CheckBox checkBox = (CheckBox)(listView.getChildAt(x));
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton compoutButton, boolean isChecked) {
// TODO Auto-generated method stub
callBlockOptions.isChecked = isChecked;
}
});
}
else if (c == TextView.class)
{
TextView textView = (TextView)(listView.getChildAt(x));
textView.setOnEditorActionListener(new OnEditorActionListener(){

@Override
public boolean onEditorAction(TextView tView, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
callBlockOptions.name = tView.getText().toString();
return true;
}

});
}

你可能希望你的其他类(class)是这样的:

public class callBlockOptions {
public static String name;
public static Boolean isChecked;
}

然后你可以像这样获取和设置 name 和 isChecked :

callBlockOptions.isChecked = false;
Boolean boolVal = callBlockOptions.isChecked;

关于java - 如何知道 ListView 特定行的复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25333385/

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