Java 如何知道listview特定行的复选框值

Java 如何知道listview特定行的复选框值,java,android,checkbox,Java,Android,Checkbox,嘿,我怎么知道当单击下面的复选框时,它来自列表的哪一行。 有没有办法知道这一点。每次yo都返回false,这是它的默认值 checkBox.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { CheckBox cb = (CheckBox) v ; call

嘿,我怎么知道当单击下面的复选框时,它来自列表的哪一行。 有没有办法知道这一点。每次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;
            }

        });
    }
public class callBlockOptions {
     public static String name;
     public static Boolean isChecked;
}
callBlockOptions.isChecked = false;
Boolean boolVal = callBlockOptions.isChecked;
然后您可以获取并设置名称,并按如下方式进行检查:

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

        });
    }
public class callBlockOptions {
     public static String name;
     public static Boolean isChecked;
}
callBlockOptions.isChecked = false;
Boolean boolVal = callBlockOptions.isChecked;

您正在侦听ListView上的事件。在我看来,您似乎对ListView中的项目感兴趣,例如复选框和TextView。此外,如果您希望类callBlockOptions中的变量是静态的,您将希望类变量是静态的,而不是类本身。然后您可以使用callBlockOptions.variableName获取和设置它们,而无需实例化callBlockOptions对象。嘿,谢谢您的帮助!我还有最后一个查询,在下面的代码中,我如何知道我的复选框属于哪个列表行。如果这对您有效,请确保您投票支持我的答案,并通过单击灰色复选框接受答案:)。我不记得您的方法对callBlockOptions类做了什么。这对你不起作用吗?此外,您可能希望对组合框使用setOnCheckedChangeListener。无论如何,您应该能够从这里找到答案:)。