滚动列表视图后再次选中复选框

时间:2017-08-09 09:06:29

标签: android listview checkbox adapter

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

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


    }

5 个答案:

答案 0 :(得分:0)

从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

答案 1 :(得分:0)

<强> RecyclerView

我建议使用Recyclerview它会有所帮助

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

<强>的ListView

你需要为那个

维护布尔arraylist

Onscroll selected checkbox getting unchecked using listiview

答案 2 :(得分:0)

我建议你阅读listview和适配器。请参阅此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;
      }
     
     }

答案 3 :(得分:0)

使用以下方法阻止您的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));
}

答案 4 :(得分:-1)

这对我有帮助

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

            }
        }