Question

J'ai une liste de réception avec la case à cocher, chaque fois que l'utilisateur a appuyé sur la case à cocher, je reçois des informations de l'utilisateur (par exemple, disons le profil ID de Facebook) et stocké dans ArrayList.Now Qu'est-ce que le problème est chaque fois que l'utilisateur a défilé la liste de répertorie, d'autres idées de profil sont également ajoutées ou supprimées en outre, eventhough il est non marqué.so il fournit

Indice de tableau hors de l'exception liée

J'ai essayé de régler le problème mais je ne peux pas le faire, s'il vous plaît me fournir une certaine solution.

Voici ma liste complèteView

public class MyComposeListItemAdapter extends ArrayAdapter {

public Context mContext;
int row;
public List<MyComposeObject> arrayList;     
public ImageLoader imageLoader;     
private static LayoutInflater inflater=null;    

/*
 * Create an ArrayList of Boolean Object to store the state of the each CheckBox
 * Initializes the ArrayList items to default value false, means no CheckBox is checked yet.
 * When you click on CheckBox. Set a check against Checked/Unchecked state and store that value in ArrayList.
 * Now set that position to CheckBox using setChecked() method.*/

private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();  

// for radio button 
private RadioButton mCurrentlyCheckedRB;
private boolean userSelected = false;
private int mResourceId = 0;
private RadioButton mSelectedRB;
private int mSelectedPosition = -1;//

public static List<String> selected_profileid =new ArrayList<String>();
public static List<String> selected_profiletype =new ArrayList<String>();

public MyComposeListItemAdapter(Context context, int resource) {
    super(context, resource);
    // TODO Auto-generated constructor stub

    this.mContext = context;
    this.row = resource;
    arrayList = (ArrayList<MyComposeObject>) Woosuite_Login.composeObjectslist;
//  System.out.println("arrayList2"+arrayList.size());      
    imageLoader=new ImageLoader(context);       
    for (int i = 0; i < this.getCount(); i++) {
        itemChecked.add(i, false); // initializes all items value with false
        }       
}   
@Override
public Context getContext() {
    // TODO Auto-generated method stub
    return mContext;
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return arrayList.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(row, null);
        holder = new ViewHolder();
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.userimage_ImageView=(ImageView) convertView.findViewById(R.id.imageView666);
    holder.username_TextView=(TextView) convertView.findViewById(R.id.textView666);
    holder.checkBox=(CheckBox) convertView.findViewById(R.id.checkBox666);
    holder.radioButton=(RadioButton) convertView.findViewById(R.id.radio666);

        holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub

                if(isChecked){                                                                  
                    itemChecked.set(position, true);

                    selected_profileid.add(arrayList.get(position).getCom_profileId());
                    System.out.println("add selected_profileid "+selected_profileid);

                    selected_profiletype.add(arrayList.get(position).getCom_profileType());
                    System.out.println("add selected_profiletype.size(); "+selected_profiletype);

                }else{

                    selected_profiletype.remove(arrayList.get(mposition).getCom_profileType());
                    System.out.println("remove selected_profileid "+selected_profileid);

                    selected_profileid.remove(arrayList.get(mposition).getCom_profileId());
                    System.out.println("remove selected_profiletype.size(); "+selected_profiletype);
                }
            }
        });     

    if(arrayList.get(position).getCom_ProfileImgUrl()!=null){                       
        imageLoader.DisplayImage(arrayList.get(position).getCom_ProfileImgUrl(), holder.userimage_ImageView);           

    }else{
        holder.userimage_ImageView.setImageResource(R.drawable.fb);
    }

    if(arrayList.get(position).getCom_ProfileName()!=null){
        holder.username_TextView.setText(arrayList.get(position).getCom_ProfileName());
    }

    holder.checkBox.setChecked(itemChecked.get(position));
    return convertView;
}

private static class ViewHolder {

    ImageView userimage_ImageView;
    TextView username_TextView;
    CheckBox checkBox;
    RadioButton radioButton;
}
}

Était-ce utile?

La solution

Veuillez ajouter le code suivant et exécutez-le à nouveau et montre-moi la sortie logcat:

 [...]
 @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
             if(position >= getCount()) {
                System.out.println("Error: position '"+position+"' is out of bounds in 'arrayList'");
                return;
             }
 [...]

Prévenir les entrées de liste des doublons:

if(!myList.contains(objectToInsert) {
    myList.add(objectToInsert);
}

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top