android - 滚动时带有复选框状态更改的 ListView

标签 android

<分区>

Possible Duplicate:
Getting an issue while checking the dynamically generated checkbox through list view

我有带复选框的 ListView 。默认情况下,所有复选框都被选中。之后一些复选框未选中并滚动这些复选框会自动选中。但我想在滚动 ListView 后选中这些复选框。 我的代码是:

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

最佳答案

convertview 可能会为您提供以前生成的 View ,所以不要依赖它。只需将您的复选框状态存储在一个 bool 数组中并相应地根据 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;
    }

关于android - 滚动时带有复选框状态更改的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13081012/

相关文章:

android - 在android中录音期间暂停或停止对讲语音

Android 设计方法说明

java - Android webview chromium 异常 - ContextResult::kFatalFailure: 支持的制服或变量太少

android - 如何将数据库包含在移动设备本身中以供离线Android应用程序使用?

android - 处于打瞌睡模式的白名单 Android 应用程序

java - 可以在两次后续调用 Wea​​kReference.get() 之间对对象进行垃圾回收吗?

android - Android中如何使用HTML5的下线功能?

android - 错误状态和正常状态有不同颜色的提示

PHP - mySql 查询执行两次

java - 为什么我的 gradle 构建花费了太多时间?