2011-02-14 97 views
0

我已經使用baseAdepter在列表中顯示三個視圖。 前兩個是textView,最後一個是checkBox View。 但是,當我去檢查的複選框和滾動列表,在項目的某些特定號碼後,其他的複選框是自動unaseptedlly檢查..android問題帶有baseAdapter的複選框

我沒有辦法,爲什麼這種事情發生是。

是誰德誰知道它的解決方案..

這裏是我的baseadepter代碼..

private static class AdepterGetUnit extends BaseAdapter{ 
private LayoutInflater mInflater; 

public AdepterGetUnit (Context context){ 
    mInflater = LayoutInflater.from(context); 
} 
@Override 
public int getCount() { 

    positionList = sharedPreferences.getInt(UnitScreen.POSITION, 0);   

    if(positionList==0){currentListLenght=ArrayHolder.dbSpeed.size();} 
    else if(positionList==1){currentListLenght=ArrayHolder.dbAngle.size();} 
    else if(positionList==2){currentListLenght=listTemperature.length;} 
    else if(positionList==3){currentListLenght=ArrayHolder.dbLenght.size();} 
    else if(positionList==4){currentListLenght=ArrayHolder.dbWeightMass.size();} 
    else if(positionList==5){currentListLenght=ArrayHolder.dbPressure.size();} 
    else if(positionList==6){currentListLenght=ArrayHolder.dbPower.size();} 
    else if(positionList==7){currentListLenght=ArrayHolder.dbCurrency.size();} 
    else if(positionList==8){currentListLenght=ArrayHolder.dbDataSize.size();} 
    else if(positionList==9){currentListLenght=ArrayHolder.dbVolume.size();} 
    else if(positionList==10){currentListLenght=ArrayHolder.dbTime.size();} 
    else if(positionList==11){currentListLenght=ArrayHolder.dbArea.size();} 
    else if(positionList==12){currentListLenght=ArrayHolder.dbCoocking.size();} 
    return currentListLenght; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
     if (convertView==null){ 
      convertView = mInflater.inflate(R.layout.row_edit_unit, null); 
      holder = new ViewHolder(); 
      holder.text = (TextView) convertView.findViewById(R.id.txt_show_units); 
      holder.textTotle = (TextView) convertView.findViewById(R.id.txt_unit); 
      holder.checkBox = (CheckBox) convertView.findViewById(R.id.check_box); 
      convertView.setTag(holder); 
     } 
     else 
     { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
//   positionList = sharedPreferences.getInt(UnitScreen.POSITION, 0);   

     switch (positionList) { 
    case 0: 
     holder.text.setText(ArrayHolder.dbSpeed.get(position)); 
     holder.textTotle.setText(ArrayHolder.dbSpeedUnit.get(position)); 
     txtUnitTitleName.setText("Speed"); 
     break; 
    case 1: 
     holder.text.setText(ArrayHolder.dbAngle.get(position)); 
     holder.textTotle.setText(ArrayHolder.dbAngleUnit.get(position)); 
     txtUnitTitleName.setText("Angle"); 
     break; 
    case 2: 
     holder.text.setText(ArrayHolder.dbTemperature.get(position)); 
     holder.textTotle.setText(ArrayHolder.dbTemperatureUnit.get(position)); 
     txtUnitTitleName.setText("Temperature"); 
     break; 
    case 3: 
     holder.text.setText(ArrayHolder.dbLenght.get(position)); 
     holder.textTotle.setText(ArrayHolder.dbLenghtUnit.get(position)); 
     txtUnitTitleName.setText("Lenght"); 
     break; 
    case 4: 
     holder.text.setText(ArrayHolder.dbWeightMass.get(position)); 
     holder.textTotle.setText(ArrayHolder.dbWeightMassUnit.get(position)); 
     txtUnitTitleName.setText("Weight/Mass"); 
     break; 
    case 5: 
     holder.text.setText(ArrayHolder.dbPressure.get(position)); 
     holder.textTotle.setText(ArrayHolder.dbPressureUnit.get(position)); 
     txtUnitTitleName.setText("Pressure"); 
     break; 
    case 6: 
     holder.text.setText(ArrayHolder.dbPower.get(position)); 
     holder.textTotle.setText(ArrayHolder.dbPowerUnit.get(position)); 
     txtUnitTitleName.setText("Power"); 
     break; 
    case 7: 
     holder.text.setText(ArrayHolder.dbCurrency.get(position)); 
     holder.textTotle.setText(ArrayHolder.dbCurrencyUnit.get(position)); 
     if(!holder.checkBox.isChecked()) 
     holder.checkBox.setChecked(false); 
     else 
     holder.checkBox.setChecked(true); 
     txtUnitTitleName.setText("Currency"); 
     break; 
    case 8: 
     holder.text.setText(ArrayHolder.dbDataSize.get(position)); 
     holder.textTotle.setText(ArrayHolder.dbDataSizeUnit.get(position)); 
     txtUnitTitleName.setText("DataSize"); 
     break; 
    case 9: 
     holder.text.setText(ArrayHolder.dbVolume.get(position)); 
     holder.textTotle.setText(ArrayHolder.dbVolumeUnit.get(position)); 
     txtUnitTitleName.setText("Volume"); 
     break; 
    case 10: 
     holder.text.setText(ArrayHolder.dbTime.get(position)); 
     holder.textTotle.setText(ArrayHolder.dbTimeUnit.get(position)); 
     txtUnitTitleName.setText("Time"); 
     break; 
    case 11: 
     holder.text.setText(ArrayHolder.dbArea.get(position)); 
     holder.textTotle.setText(ArrayHolder.dbAreaUnit.get(position)); 
     txtUnitTitleName.setText("Area"); 
     break; 
    case 12: 
     holder.text.setText(ArrayHolder.dbCoocking.get(position)); 
     txtUnitTitleName.setText("Coocking"); 
     break; 
    default: 
     break; 
    }  

    return convertView; 
} 
static class ViewHolder { 

    TextView text ,textTotle; 
    ImageView icon; 
    CheckBox checkBox;   
} 
@Override 
public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return position-1; 
} 
@Override 
public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position-1; 
} 

@Override 
public void notifyDataSetChanged() { 
    super.notifyDataSetChanged(); 
} 

} 

回答

1

嘗試下面的代碼。你的回答是對的,我只是編輯它。

package com.android.diseaseRating; 
import java.util.Vector; 

import com.android.diseaseRating.db.DBConnect; 
import com.android.diseaseRating.settings.SettingsList; 
import android.content.Context; 
import android.database.Cursor; 
import android.graphics.Color; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class ColorPickerBase1 extends BaseAdapter{ 

public ColorPickerBase1(Context context , Vector<String> colorName ,Vector<String> colorHex) { 
layoutInflater = LayoutInflater.from(context); 

this.colorHex = colorHex; 
this.colorName = colorName; 

dbConnect = new DBConnect(context, Bean.DATABASE_NAME); 

} 

@Override 
public int getCount() { 
    return colorName.size(); 
} 

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

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

@Override  
public View getView(int position, View convertView, ViewGroup parent) { 
    viewHolder holder ; 
    if(convertView==null){ 
     holder = new viewHolder(); 
     convertView = layoutInflater.inflate(R.layout.row_color_picker, null); 

     holder.imgColorView = (ImageView)convertView.findViewById(R.id.imgViewColor); 
     holder.txtColorName = (TextView)convertView.findViewById(R.id.txtColorName); 
     holder.chkBoxColorCked = (CheckBox)convertView.findViewById(R.id.chkBOx); 

     boolean stop=true; 
     if(SettingsList.SETTING) 
      holder.chkBoxColorCked.setEnabled(false); 


     convertView.setTag(holder); 
    } 
    else{ 
     holder = (viewHolder)convertView.getTag(); 
    } 
    System.out.println("TEH POSITION IS "+position) ; 
    System.out.println("TEH COLOR IS "+colorHex.get(position)) ; 

    if (position == 801 || position == 802 || position == 803 
      || position == 1009 || position == 1011 || position == 1013 
      || position == 1012 || position == 1017 || position == 1018 
      || position == 1019 || position == 1024 || position == 1026 
      || position == 1027 || position == 1028 || position == 1029 
      || position == 1030 || position == 1029 || position == 1030 
      || position == 1049 || position == 1068 || position == 1076 
      || position == 1082) {   
    } 
    else{ 
     try { 
      holder.imgColorView.setBackgroundColor(Color.parseColor("#" 
        + colorHex.get(position))); 


     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     holder.txtColorName.setText("" + colorName.get(position)); 

    } 

    holder.chkBoxColorCked.setTag(position); 
    holder.chkBoxColorCked.setOnCheckedChangeListener(btnChkListner); 


    if(Bean.tempVector.get(position)){ 
     holder.chkBoxColorCked.setChecked(true); 
    } 
    else{ 
     holder.chkBoxColorCked.setChecked(false); 
    } 

    return convertView; 
} 

CompoundButton.OnCheckedChangeListener btnChkListner = new CompoundButton.OnCheckedChangeListener() 
{ 
    @Override 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    { 
     //s("onCheckedChanged", "onCheckedChanged"); 
     final int positionChecked = (Integer)buttonView.getTag(); 
     //System.out.println("Position ====="+positionChecked); 
     if(buttonView.isChecked()) 
     {      
      Bean.colorRangeOnePicker1.add(positionChecked); 
      Bean.tempVector.set(positionChecked, true); 

      if(Bean.colorRangeOnePicker1.size()>2){ 
       Bean.colorRangeOnePicker1.remove(0); 

//     String SQL = "UPDATE DisplaySettings SET range1color1 = '"+Bean.colorRangeOnePicker1.get(0) +"',range1color2 ='"+Bean.colorRangeOnePicker1.get(1)+"' where id ='"+ImageDisplay.SettingID+"'"; 
//     
//     dbConnect.execNonQuery(SQL); 

      } 
      System.out.println("COLOR RANGE ONE IS "+Bean.colorRangeOnePicker1); 
      //System.out.println("IN IF ...IS CHEKED "); 
     } 
     else{   
      Bean.tempVector.set(positionChecked, false); 
     } 


     for(int i=0; i<3; i++){ 

     } 
    } 
}; 


LayoutInflater layoutInflater ; 


class viewHolder{ 
    ImageView imgColorView; 
    TextView txtColorName; 
    CheckBox chkBoxColorCked; 
} 

Vector<String > colorName; 
Vector<String> colorHex; 

    DBConnect dbConnect; 
}