2013-06-18 58 views
3

我使用下面的代码维护复选框状态,但它不工作。我只想选中两个复选框。复选框在自定义列表视图中检查并保持其状态?

public class CACompareList extends ArrayAdapter<CompareListData>{ 

    public static int count = 0; 
    public LayoutInflater inflater; 
    public static ArrayList<CompareListData> selectedId; 
    public ArrayList<CompareListData> listObjects; 
    Context contex; 
    public CACompareList(Context context, int textViewResourceId, 
      ArrayList<CompareListData> objects) { 
     super(context, textViewResourceId, objects); 
     this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     this.listObjects = objects; 
     selectedId = new ArrayList<CompareListData>(); 
     this.contex = context; 
    } 
    public static class ViewHolder 
    { 
     TextView txtViewLoanName; 
     TextView txtViewHtmlString; 
     CheckBox chkSelected; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 
     if(convertView==null) 
     { 
      holder = new ViewHolder(); 
      convertView = inflater.inflate(R.layout.row_comparelist, null); 

      holder.txtViewLoanName= (TextView) convertView.findViewById(R.id.rowcomparelist_tv_loanname); 
      holder.txtViewHtmlString= (TextView) convertView.findViewById(R.id.rowcomparelist_tv_loandetail); 
      holder.chkSelected= (CheckBox) convertView.findViewById(R.id.rowcomparelist_chk_selected); 
      convertView.setTag(holder); 
     } 
     else{ 
      holder=(ViewHolder)convertView.getTag(); 
     } 

     final CompareListData data = this.listObjects.get(position); 

     holder.txtViewLoanName.setText(this.listObjects.get(position).getLoanName()); 
     holder.txtViewHtmlString.setText(Html.fromHtml(this.listObjects.get(position).getHtmlString())); 


     holder.chkSelected.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

       //listObjects.get(position).setSelected(isChecked); 
       if(isChecked){ 
        count++; 

       }else{ 
        count--; 
        selectedId.remove(data); 
       } 
       if(count >= 3)// it will allow 3 checkboxes only 
       { 
        Toast.makeText(contex, "Select only two Loan", Toast.LENGTH_LONG).show(); 
        buttonView.setChecked(false); 
        count--; 
       } 
       else 
       { 
        selectedId.add(data); 
        buttonView.setSelected(isChecked); 
        int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
        listObjects.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state. 
        Log.e("Selected Position is:", String.valueOf(getPosition)); 
       } 
      } 
     }); 
     holder.chkSelected.setTag(position); 
     holder.chkSelected.setSelected(this.listObjects.get(position).getSelected()); 
     Log.e("Position is:", String.valueOf(position)); 
     return convertView; 
    } 

} 

,当我检查的第一个复选框,比向下滚动起来,我得到不同的结果,我的第一个复选框没有被选中,另外一个选择。

我也检查了listView项目点击列表,但它也不帮助我。

lvLoanLiat.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       if(IN_EDIT_MODE == false){ 
        CompareListData data = (CompareListData)arg0.getItemAtPosition(arg2); 

        CheckBox cBox = (CheckBox) arg1.findViewById(R.id.rowcomparelist_chk_selected); 
        if(cBox.isChecked()){ 
         count--; 
         selectedId.remove(data); 
        }else{ 
         count++; 
        } 
        if(count >= 3)// it will allow 3 checkboxes only 
        { 
         Toast.makeText(CompareListActivity.this, "Select only two Loan", Toast.LENGTH_LONG).show(); 
         cBox.setChecked(false); 
         count--; 
        } 
        else 
        { 
         selectedId.add(data); 
         cBox.setSelected(true); 
         data.setSelected(true); 
         Log.e("Selected Position is:", String.valueOf(arg2)); 
        } 
       } 

      } 
     }); 

下面是屏幕截图,我只检查了第一项,就像下面的截图一样。 enter image description here

比我滚动上下比我得到像下面的图像结果。

enter image description here

谢谢。

+0

http://stackoverflow.com/questions/16685366/customised-listview-using-arrayadapter-class-in-android/16686623#16686623。检查这可能会帮助 – Raghunandan

+1

@Raghunandan我已经看到它,但它不帮助我。 –

+0

它为什么不帮你? – Raghunandan

回答

0

您是否尝试过使用:

<activity name= ".YourActivity" android:configChanges="screenSize"/>

希望这有助于。让我知道如果没有。

+0

我有问题在listView中不在活动中滚动。 –

+0

UI被捆绑到活动中。你有没有尝试过使用上述参数。使用参数 –

1

在getView(),你应该取消设置监听第一

holder.chkSelected.setOnCheckedChangeListener(null); 
holder.chkSelected.setSelected(isSelected); 

this可能帮助你。公告行622.

+0

我已经看到它,但不能帮助我,我仍然得到weired结果。 –

+0

@GirishBhutiya也许你应该把setTag()放在buttonView.getTag()之前,但是我认为没必要使用setTag(position),值getPosition alawys等于position – twocity

相关问题