lundi 10 février 2020

Android Studio - Listview OnClick Activates Multiple Checkboxes

Hello im a beginner in Androidstudio.

I tried to create a Listview with a Checkbox and a TextView On first glance it looks like it worked but the Problem is I check the Checkbox of "My Note: 0" and the Checkbox of "My Note: 9" is checked,I have no Idea why and debugging does not help.

Not only Note 9 is cheked every : Multiple of 8 +1 is Checked . So some checked ones are 9, 17, 25 ,33,41,49,57,65,73,81......

enter image description here

ListView XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="100">

    <CheckBox android:gravity="center"
        android:text=""
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:id="@+id/checkBox_shopEntry"
        android:layout_weight="90"/>


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="10">

        <TextView
            android:id="@+id/textView_feedbackName"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_gravity="left"
            android:fontFamily="casual"
            android:gravity="center|left"
            android:text="TextView1" />


    </LinearLayout>


</LinearLayout>

The ListView Adapter :

package com.example.fragment.Entity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.fragment.R;

import java.util.ArrayList;



public class ShopListAdapter extends ArrayAdapter<ShoppingEntry> {

    private static final String TAG = "ShopListAdapter";

    private Context mContext;
    private int mResource;
    private int lastPosition = -1;

    /**
     * Holds variables in a View
     */
    private static class ViewHolder {
        TextView name;
        CheckBox checkBox;
    }

    public ShopListAdapter(Context context, int resource, ArrayList<ShoppingEntry> objects) {
        super(context, resource, objects);
        mContext = context;
        mResource = resource;
    }

    @Override
    public void add(@Nullable ShoppingEntry object) {
        super.add(object);
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //get the shop list information
        String name = getItem(position).getName();


        //Create the shopEntry object with the information
        ShoppingEntry shopEntry = new ShoppingEntry(name);

        //create the view result for showing the animation
        final View result;

        //ViewHolder object
        ViewHolder holder;


        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource, parent, false);
            holder= new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.textView_feedbackName);
            holder.checkBox = convertView.findViewById(R.id.checkBox_shopEntry);

            result = convertView;

            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
            result = convertView;
        }




        Animation animation = AnimationUtils.loadAnimation(mContext,
                (position > lastPosition) ? R.anim.load_down_anim : R.anim.load_up_anim);
        result.startAnimation(animation);
        lastPosition = position;

        holder.name.setText(shopEntry.getName());

        return convertView;
    }
}

And the Object used in the Adapter

package com.example.fragment.Entity;


public class ShoppingEntry {
    private String name;


    public ShoppingEntry(String note) {
        this.name = note;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}




Aucun commentaire:

Enregistrer un commentaire