【问题标题】:listview with checkbox status changed when scoll [duplicate]滚动时带有复选框状态的列表视图[重复]
【发布时间】:2012-10-16 08:52:49
【问题描述】:

可能重复:
Getting an issue while checking the dynamically generated checkbox through list view

我有带有复选框的列表视图。默认情况下,所有复选框都被选中。之后,一些复选框未选中并自动选中这些复选框。但我希望在滚动列表视图后选中这些复选框。 我的代码是:

public class  FriendListAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    DrunkMessages friendsList;
    boolean[] checkBoxState;

    public FriendListAdapter(DrunkMessages friendsList) {
        this.friendsList = friendsList;
        if (Utility.model == null) {
            Utility.model = new FriendsGetProfilePics();
        }
        Utility.model.setListener(this);
        mInflater = LayoutInflater.from(friendsList.getBaseContext());

        checkBoxState=new boolean[jsonArray.length()];
    }


    public int getCount() {
        return jsonArray.length();
    }


    public Object getItem(int position) {
        return null;
    }


    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

            View hView = getLayoutInflater().inflate(R.layout.friendslist, null);

            ImageView profile_pic = (ImageView) hView.findViewById(R.id.profile_pic);
            TextView name = (TextView) hView.findViewById(R.id.name);

            final CheckBox checkbox=(CheckBox)hView.findViewById(R.id.checkbox);

        profile_pic.setImageBitmap(Utility.model.getImage(friendid[position], pictures[position]));
        name.setText(names[position]);

        checkbox.setOnClickListener(new View.OnClickListener() {

               public void onClick(View v) {
                if(((CheckBox)v).isChecked()){

                checkBoxState[position]=true;
                status[position]="1";

             Log.e("checked","checked");
            Log.e("Checked",status[position]);
                }
                else{
                 checkBoxState[position]=false;
                status[position]="0";

                Log.e("checked","unchecked");
             Log.e("Checked",status[position]);

                }
               }
               });

      return hView;
}

【问题讨论】:

  • 您可以使用带有 simple_list_item_multiple_choice 属性的列表视图,而不是为复选框膨胀另一个 xml。像这个适配器 = new ArrayAdapter(this,android.R.layout.simple_list_item_multiple_choice,objectOfList);
  • Link1,Link2的可能重复

标签: android


【解决方案1】:

convertview 可能会给你以前生成的视图,所以不要依赖它。只需将您的复选框状态存储在一个布尔数组中并相应地进行更改,这取决于 checkchangelistener 回调这里是您的代码我刚刚修改了您的代码的 getView 方法,其他一切都保持原样

 public View getView(final int position, View convertView, ViewGroup parent) {

                View hView = getLayoutInflater().inflate(R.layout.friendslist, null);

                ImageView profile_pic = (ImageView) hView.findViewById(R.id.profile_pic);
                TextView name = (TextView) hView.findViewById(R.id.name);

                final CheckBox checkbox=(CheckBox)hView.findViewById(R.id.checkbox);

                checkbox.setTag(new Integer(position));  
            profile_pic.setImageBitmap(Utility.model.getImage(friendid[position], pictures[position]));
            name.setText(names[position]);

            checkbox.setOnCheckedChangeListener(null);
            if(checkBoxState[position])
              checkbox.setChecked(true);
            else  
              checkbox.setChecked(false);

            checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                   public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Integer pos = (Integer)buttonView.getTag();     
                    if(isChecked){

                    checkBoxState[pos.intValue()]=true;
                    }
                    else{
                     checkBoxState[pos.intValue()]=false;
                    Log.e("checked","unchecked");

                    }
                   }
                   });

          return hView;
    }

【讨论】:

  • 更新了我的答案,请立即查看
  • 我在以下行遇到空指针异常:checkBoxState[pos.intValue()]=true;
  • 检查我更新的答案,你必须像这样设置复选框的标签 checkbox.setTag(new Integer(position));
  • k 工作正常。但我希望默认选中所有复选框。在我的 xml 中也选中 =“true”。但是为什么我默认不选中所有复选框。请回答我
  • 你的数组没有初始化,所以像这样初始化你的 checkBoxState 数组 Arrays.fill(checkBoxState, true);
【解决方案2】:

您可以使用带有 simple_list_item_multiple_choice 属性的列表视图,而不是为复选框膨胀另一个 xml。像这样

 adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_multiple_choice,obje‌​ctOfList);

【讨论】:

  • 我用的是自定义listview,怎么可能?
【解决方案3】:

在您的 getview 方法中添加以下代码

if(checkBoxState.length>0)
checkbox.setChecked(checkBoxState[position]);

【讨论】:

  • 我建议简单的 checkbox.setChecked(checkBoxState[position]);
  • 也遇到了同样的问题
  • 通过log cat只看checkBoxState[position]的值
  • 我是假的,而且默认情况下也没有选中。但在我的 xml 中,我写了 android:checked="true"。为什么我得到假值
  • 请给我答案:我希望默认情况下都选中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 2017-11-17
  • 1970-01-01
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多