0

我有 a ListView,每行由 atextview和 a组成checkbox

用户可以点击每一个TextView,一旦他点击,DialogBox就会向他展示一个他应该选择Yes或的地方No

当他选择否时,需要自动勾选用户点击 的CheckBox旁边。TextView

我面临的问题是,无论TextBox用户选择哪个,CheckBox列表的最后一个都会一直打勾,而不是选中的TextView.

调试后我注意到这是因为getView()适配器类的方法在最后一行停止,因此最后一行总是被选中。我尝试为每一行设置标签,以便我可以checkbox相应地勾选,但随后都没有Checkboxes被勾选。

对于此事,我将不胜感激。

....

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
    .....
    holder.text.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(final View v) 
        {
            final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setTitle("Materials");
            builder.setMessage("Did you require any materials to fix this error?");
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which)
                {
                    String clickedError;            
                    clickedError = holder.text.getText().toString();        

                    Intent intent = new Intent(mContext, Material.class);
                    intent.putStringArrayListExtra("materialList", materialList);
                    intent.putExtra("clickedError", clickedError);
                    intent.putExtra("repairID", repairID);
                    ((Activity)mContext).startActivityForResult(intent, 1);
                }
            });
            builder.setNegativeButton("No", new DialogInterface.OnClickListener()
            {
                @Override
                public void onClick(DialogInterface dialog, int which) 
                {
                    //holder.chk.setChecked(true); // This ticks the last checkbox of the list.

                    if(holder.chk.getTag() == v.getTag())
                    {
                        holder.chk.setChecked(true);
                    }   
                }
            });
            builder.show();

        }
    });
4

2 回答 2

1

尝试将该行的复选框传递给您的点击监听器:

@Override
public View getView(int position, View convertView, ViewGroup parent) 
{
.....
holder.text.setOnClickListener(new MyClickListener(holder.chk);
}
private class MyClickListener implements OnClickListener
{
    Checkbox checkbox = null;
    public MyClickListener(final Checkbox checkbox)
    {
         this.checkbox = checkbox;
    }
    @Override
    public void onClick(final View v) 
    {
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle("Materials");
        builder.setMessage("Did you require any materials to fix this error?");
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                String clickedError;            
                clickedError = holder.text.getText().toString();        

                Intent intent = new Intent(mContext, Material.class);
                intent.putStringArrayListExtra("materialList", materialList);
                intent.putExtra("clickedError", clickedError);
                intent.putExtra("repairID", repairID);
                ((Activity)mContext).startActivityForResult(intent, 1);
            }
        });
        builder.setNegativeButton("No", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                //checkbox.setChecked(true); // This ticks the last checkbox of the list.

                if(checkbox.getTag() == v.getTag())
                {
                    checkbox.setChecked(true);
                }   
            }
        });
        builder.show();

    }
});
于 2013-08-20T13:13:23.527 回答
1

CheckBox 扩展了 TextView。使用 CheckBox.setText(CharSequence | int) 将文本设置为 CheckBox 并删除 TextView (当然,这不会解决您的检查问题)。关于您的问题 - 这是因为大多数 AdapterView 的重用视图。将检查的项目信息存储在某处并在 getView 调用 setChecked(checkedItems.get(pos)) 中。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        .....

        holder.chk.setChecked(checkedItems.get(position))

        .....

        builder.setNegativeButton("No", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which) 
            {
                //holder.chk.setChecked(true); // This ticks the last checkbox of the list.

                if(holder.chk.getTag() == v.getTag())
                {
                    holder.chk.setChecked(true);
                    checkedItems.put(position, true);
                }
            }
        });
    }

其中 checkedItems 是 SparseBooleanArray

于 2013-08-20T13:25:11.477 回答