【问题标题】:Check box again get checked after scroll listview滚动列表视图后再次选中复选框
【发布时间】:2017-08-09 09:06:28
【问题描述】:

我正在为复选框和文本使用自定义列表视图。数据来自服务器,如果服务器上有任何项目可用,它会在列表视图中显示为选中状态。但是如果我取消选择某些项目(复选框),它会再次得到滚动列表视图后选择。 任何建议都会有所帮助。 这是我的适配器类:

public class SetWorkAdapter extends BaseAdapter {
    Context mContext;
    public ArrayList<SubStkMarket> mSubStkMarkets;
    checkBoxListener checkBoxListener;
    ArrayList<MarketNameUID> arrayList;

    public SetWorkAdapter(Context mContext, ArrayList<SubStkMarket> mSubStkMarkets,ArrayList<MarketNameUID> arrayList) {
        this.mContext = mContext;
        this.mSubStkMarkets = mSubStkMarkets;
        this.arrayList=arrayList;
    }
    public interface checkBoxListener {
        public void onCheckBoxClick(int position, boolean isChecked, ArrayList<MarketNameUID> mList);
    }
    public void setCustomButtonListner(checkBoxListener listener) {
        this.checkBoxListener = listener;
    }
    @Override
    public int getCount() {
        return mSubStkMarkets.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        View convertview;
        TextView code, name;
        final CheckBox checkBox;
        convertview = LayoutInflater.from(mContext).inflate(R.layout.set_work_type_item, null);
        code = (TextView) convertview.findViewById(R.id.set_work_type_item_market_code);
        name = (TextView) convertview.findViewById(R.id.set_work_type_item_market_name);
        checkBox = (CheckBox) convertview.findViewById(R.id.set_work_type_item_market_checkbox);
        SubStkMarket subStkMarket = mSubStkMarkets.get(position);
        checkBox.setChecked(subStkMarket.isSelected());
        code.setText(String.valueOf(subStkMarket.getSubStkUID()));
        name.setText(subStkMarket.getMarketName());
        if(arrayList!=null)
        {
            for(int i=0;i<arrayList.size();i++)
            {
                String marketName = arrayList.get(i).getMarketName();
                if(marketName.equalsIgnoreCase(subStkMarket.getMarketName()))
                {
                    checkBox.setChecked(true);
                }
            }
           int cnt=arrayList.size();
        }
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

                if (checkBoxListener != null) {


                    checkBoxListener.onCheckBoxClick(position,isChecked,arrayList);
                }
            }
        });

        return convertview;
    }


    }

【问题讨论】:

  • onCheckBoxClick 的实现是什么?您没有将selected 的状态更改为subStkMarket,也不清楚arraylist 是什么
  • 更好地使用recyclerview

标签: android listview checkbox adapter


【解决方案1】:

从 ArrayList 对象更改选定状态。

    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {

            if (checkBoxListener != null) {
                 checkBoxListener.onCheckBoxClick(position,isChecked,arrayList);
                subStkMarket.setSelected(isChecked);
            }
        }
    });

您可能必须将subStkMarket 声明为final

【讨论】:

    【解决方案2】:

    RecyclerView

    我建议使用 Recyclerview 会有所帮助

    http://android-pratap.blogspot.in/2015/01/recyclerview-with-checkbox-example.html

    列表视图

    您需要为此维护布尔数组列表

    Onscroll selected checkbox getting unchecked using listiview

    【讨论】:

      【解决方案3】:

      我建议您阅读有关列表视图和适配器的信息。请参阅此article。您必须将检查的状态存储在模型中的数组或 ArrayList 中,并将它们设置回 getView 中。这是一个代码示例:

              //define your custom adapter
          private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>
          {
             // boolean array for storing
             //the state of each CheckBox 
             boolean[] checkBoxState;
             
            
             ViewHolder viewHolder;
             
             public CustomAdapter(Context context, int textViewResourceId,
             ArrayList<HashMap<String, Object>> players) {
           
              //let android do the initializing :)
              super(context, textViewResourceId, players); 
              
            //create the boolean array with
             //initial state as false
            checkBoxState=new boolean[players.size()];
            }
           
           
              //class for caching the views in a row  
           private class ViewHolder
           {
             ImageView photo;
             TextView name,team;
             CheckBox checkBox;
           }
           
             
           
           @Override
           public View getView(final int position, View convertView, ViewGroup parent) {
           
             if(convertView==null)
              {
             convertView=inflater.inflate(R.layout.players_layout, null);
             viewHolder=new ViewHolder();
           
              //cache the views
              viewHolder.photo=(ImageView) convertView.findViewById(R.id.photo);
              viewHolder.name=(TextView) convertView.findViewById(R.id.name);
              viewHolder.team=(TextView) convertView.findViewById(R.id.team);
              viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkBox);
           
               //link the cached views to the convertview
              convertView.setTag( viewHolder);
              
           
            }
            else
             viewHolder=(ViewHolder) convertView.getTag();
           
                      
            int photoId=(Integer) players.get(position).get("photo");
           
            //set the data to be displayed
            viewHolder.photo.setImageDrawable(getResources().getDrawable(photoId));
            viewHolder.name.setText(players.get(position).get("name").toString());
            viewHolder.team.setText(players.get(position).get("team").toString());
              
             //VITAL PART!!! Set the state of the 
             //CheckBox using the boolean array
                  viewHolder.checkBox.setChecked(checkBoxState[position]);
                       
                    
                     //for managing the state of the boolean
                     //array according to the state of the
                     //CheckBox
                     
                     viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
               
             public void onClick(View v) {
              if(((CheckBox)v).isChecked())
               checkBoxState[position]=true;
              else
               checkBoxState[position]=false;
                
              }
             });
           
             //return the view to be displayed
             return convertView;
            }
           
           }
      

      【讨论】:

        【解决方案4】:

        只是防止您的RecyclerView 使用:

        recyclerView.getRecycledViewPool().setMaxRecycledViews(0,0);
        

        但滚动时可能会出现一些滞后,最好的方法是将CheckBox检查状态保存在HashMap中,就像这样在Singleton中:

        HashMap<Integer, Boolean> map = new HashMap<>();// key is the position, value is status.
        

        例子:

        class MySingleton {
            private static final MySingleton ourInstance = new MySingleton();
        
            public HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>;
        
            static MySingleton getInstance() {
                return ourInstance;
            }
        
            private MySingleton() {
            }
        }
        

        在你的适配器内部:

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        MySingleton.getInstance.map.put(position, isChecked);
                    }
                });
        

        然后在加载时调用HashMap

        if (MySingleton.getInstance().map.containsKey(position)) {
                checkBox.setChecked(map.get(position));
        }
        

        【讨论】:

          【解决方案5】:

          这对我有帮助

          for(int i=0;i<arrayList.size();i++)
                  {
                      String marketName = arrayList.get(i).getMarketName();
                      if(marketName.equalsIgnoreCase(subStkMarket.getMarketName()))
                      {
          
                          if(subStkMarket.isSelected()==true)
                          {
                              subStkMarket.setSelected(true);
                              checkBox.setChecked(true);
                          }
          
                      }
                  }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-01-19
            • 2015-11-30
            • 2016-06-03
            • 2016-09-05
            • 1970-01-01
            相关资源
            最近更新 更多