我正在使用custom adpater in AlertDialog
。在那個適配器中,我使用的是TextView and CheckBox
.. 現在我想處理CheckBox的setOnCheckedChangeListener
用於檢查選中或未選中的CheckBox。並且根據CheckBox的狀態,我想實現一些代碼。但是這個聽衆不止一次被解僱。那麼我該如何處理呢?如果有人有想法,建議我。Alertdialg與定製適配器containg複選框及其監聽器
準確地說,我的問題是當我檢查複選框,我想增加一些值,當我未選中時,我想減少一些value.but我沒有得到確切的總和值,如果我滾動然後這個總和值是改變..所以我必須做什麼?
以下是我的自定義Adaper:
private class Updateinfo_ServiceAdapter extends BaseAdapter
{
@Override
public int getCount() {
// TODO Auto-generated method stub
return _options_services.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Viewholder holder;
LayoutInflater inflater=getLayoutInflater();
if(convertView==null)
{
convertView=inflater.inflate(R.layout.row_updateinfo_service, null);
holder=new Viewholder();
holder.txtname=(TextView)convertView.findViewById(R.id.serviceName);
holder.chkSelected=(CheckBox)convertView.findViewById(R.id.chk);
convertView.setTag(holder);
}
else
{
holder=(Viewholder)convertView.getTag();
}
holder.txtname.setText(_options_services[position]);
holder.chkSelected.setChecked(_selections_services[position]);
holder.chkSelected.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (isChecked) {
allowServicesSum = allowServicesSum
+ Integer.parseInt((String) services[position]
.getSystemServiceID());
System.out.println("AllowService sum is "+allowServicesSum);
} else {
allowServicesSum = allowServicesSum
- Integer.parseInt((String) services[position]
.getSystemServiceID());
System.out.println("AllowService sum is "+allowServicesSum);
}
}
});
if(_selections_services[position])
{
holder.chkSelected.setEnabled(false);
}
else
{
holder.chkSelected.setEnabled(true);
}
return convertView;
}
private class Viewholder
{
TextView txtname;
CheckBox chkSelected;
}
}
我在gettag後立即調用了監聽器,但沒有工作..我的問題是當我檢查複選框時,我想增加一些值,當我未選中時,我想減少一些值。但如果我滾動然後這個總和值改變..所以我必須做什麼? –
我更新了我的答案,明確表示您必須**取消設置**聽衆。對不起,最初並不清楚。 –
如果我沒有設置聽衆,那麼仍然存在問題..如果我檢查了該項目並向下滾動,那麼所選項目將自動取消選中。那麼這裏應該做什麼? –