2017-08-09 65 views
-1

我使用自定義列表視圖複選框和text.Data來自服務器,如果有任何項目是可用的服務器上顯示爲列表view.but檢查,但如果我取消選擇一些項目(複選框)在滾動列表視圖後再次被選中。 任何建議都會有所幫助。 這是我的適配器類:複選框再次滾動列表視圖後檢查

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


    } 
+1

是什麼'onCheckBoxClick'的實施?你並沒有改變'subStkMarket'的'selected'狀態,但它不是很清楚'arraylist'是什麼 – Yazan

+0

更好地使用recyclerview –

回答

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

您可能需要聲明subStkMarketfinal

0

我建議你閱讀列表視圖和適配器。請參閱此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; 
      } 
      
     } 
0

只是防止你RecyclerView使用回收:

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

但它可以使一些滾動時滯後,最好的辦法是保存內部的HashMapCheckBox檢查狀態像這樣的Singleton內:

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

例如:

​​

和適配器內:

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

你明白我的問題是因爲在Android重用UI對象名單。

更改此:

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

這樣:

  for(int i=0;i<arrayList.size();i++) 
       { 
        String marketName = arrayList.get(i).getMarketName(); 
        if(marketName.equalsIgnoreCase(subStkMarket.getMarketName())) 
        { 
         checkBox.setChecked(true); 
        }else{ 
         checkBox.setChecked(false); 
        } 
       } 
-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); 
       } 

      } 
     } 
相關問題