lundi 16 juillet 2018

Get Selected Items and their count using Chekboxes and custom listView

I am new to android studio and I know these questions are posted many times but I am not getting the concept of using setTag and getTag methods.I am making an app in which the students data will be retrieved from firebase stored data and checkboxes will be displayed in a custom listView. When user will click on item that student will be marked present and their count will increase and when button is clicked the markes students name should be displayed.Everthing is working fine but when I click on button, NO names of present students are displayed.I don't want to use setTag or these methods because I don't know about them.Can u plz tell the solution in simple manner;

      DatabaseReference databaseReference;
    ListView lst;
    ArrayList<Students> arrayList;
    Button Attendance;
    UserAdapter adapter;
    Students st1;
    int a[];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        lst=(ListView)findViewById(R.id.listView);
        Attendance=(Button)findViewById(R.id.buttonAttendance);
        Attendance.setOnClickListener(this);
        arrayList=new ArrayList<>();
        lst.setOnItemClickListener(this);
        a=new int[10];

        databaseReference= FirebaseDatabase.getInstance().getReference  ( "artists");

    }

    @Override
    protected void onStart() {
        super.onStart();

        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
             public void onDataChange(DataSnapshot dataSnapshot) {
    arrayList.clear();
                for(DataSnapshot stuSnapshot:dataSnapshot.getChildren()){
                    Students st=stuSnapshot.getValue(Students.class);
                    arrayList.add(st);


                }
                Collections.sort(arrayList, new Comparator<Students>() {
                    @Override
                    public int compare(Students students, Students t1) {
                        return students.getName().compareTo(t1.getName());
                    }
                });
               adapter=new UserAdapter     (HomeActivity.this,R.layout.list_item,arrayList);
                lst.setAdapter(adapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                Toast.makeText(getApplicationContext(),"Database error",Toast.LENGTH_SHORT).show();
            }
        });



    }


    @Override
    public void onClick(View view) {
        ArrayList<Students> st=adapter.arrayList;
        StringBuffer responseText = new StringBuffer();
        responseText.append("The following were selected...\n");
        for(int i=0;i<st.size();i++){

            Students students=st.get(i);
            if(students.isCheck()){
                responseText.append("\n" + students.getName());

            }
        }
        Toast.makeText(this, responseText , Toast.LENGTH_SHORT).show();

    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Students st=adapter.getItem(i);
        CheckBox checkBox=(CheckBox) view.findViewById(R.id.checkBoxAttendance);
        checkBox.setChecked(!checkBox.isChecked());
        a[i]++;
       st.setCount(a[i]);
        Toast.makeText(this, "The item selected " +a[i], Toast.LENGTH_SHORT).show();
    }
}









    public class UserAdapter extends ArrayAdapter<Students> {
    Context context;
    int resource;
    ArrayList<Students> arrayList;
    Students user;

     public UserAdapter(@NonNull Context context, int resource, @NonNull   ArrayList<Students> objects) {
        super(context, resource, objects);
     this.context=context;
     this.resource=resource;
      arrayList=objects;


    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view=null;

        view= LayoutInflater.from(context).inflate(resource,parent,false);
        TextView textView=view.findViewById(R.id.textViewAttendance);
        TextView textView1=view.findViewById(R.id.textViewRoll);
        CheckBox cb=view.findViewById(R.id.checkBoxAttendance);

       user=arrayList.get(position);
        textView1.setText(user.getRoll());

        textView.setText(user.getName());


        return view;
    }


     }




      public class Students
    {
     String name;
     String stuId;
     String clas;
    String roll;
    boolean check=false;
    int count;

    public Students(){


    }

    public Students(String name, String stuId, String clas, String roll, Boolean  check, int count) {
        this.name = name;
        this.stuId = stuId;
        this.clas = clas;
        this.roll = roll;
        this.check = check;
        this.count = count;
    }

    public String getName() {
        return name;
    }

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

    public String getStuId() {
        return stuId;
    }

    public void setStuId(String stuId) {
        this.stuId = stuId;
    }

    public String getClas() {
        return clas;
    }

    public void setClas(String clas) {
        this.clas = clas;
    }

    public String getRoll() {
        return roll;
    }

    public void setRoll(String roll) {
        this.roll = roll;
    }

    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}








    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:padding="16dp"
    android:orientation="horizontal"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="54dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textViewAttendance"
            android:layout_width="261dp"
            android:layout_height="11dp"
            android:layout_weight="5"
            android:text="TextView"
            android:textColor="@color/colorPrimary"
            android:textSize="18dp" />

        <TextView
            android:id="@+id/textViewRoll"
            android:layout_width="87dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="TextView" />

        </LinearLayout>
        <CheckBox
            android:id="@+id/checkBoxAttendance"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:clickable="false"
            android:layout_weight="1" />

    </LinearLayout>




Aucun commentaire:

Enregistrer un commentaire