英文:
Display buttons dynamically in recyclerview android studio
问题
我正在开发一个学生追踪应用程序,尝试在每个学生下方显示动态按钮。我将在下面粘贴一个图片,展示我希望它看起来的样子。
英文:
I am working on a student tracker app and I am trying to display dynamic buttons that get created under each of my students. I will paste an image below of what I hope for it to look like.
At the moment, I am hardcoding the buttons for the first image. I am getting the picture of the student and their name from my firbase db. Now my questions is how can I generate the checkBox for each of the student's and make sure it's directly under their picture and name like above.
In my mind, I am thinking the algorithm would be something like
- Loop through my Students ArrayList
 - Generate the checkBox for each student
 - Place them onto my recyclerView.
 
I will paste relevant code down below.
daily_grading.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
    tools:context=".DailyGrading">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="409dp"
        android:layout_height="729dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PASS"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/recyclerView"
        app:layout_constraintHorizontal_bias="0.046"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499" />
    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="FAIL"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.261"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499" />
</androidx.constraintlayout.widget.ConstraintLayout>
student_item.xml
 <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="wrap_content">
    
    
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:foreground="?android:attr/selectableItemBackground"
            app:cardElevation="2dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
        </androidx.cardview.widget.CardView>
    
        <RelativeLayout
            android:id="@+id/relativeLayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp"
    
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="52dp">
    
            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginTop="50dp"
                android:paddingTop="20dp"
                android:scaleType="centerCrop" />
    
            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/imageView"
                android:layout_margin="10dp"
                android:textSize="16sp" />
    
        </RelativeLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
DailyGrading.class
public class DailyGrading extends AppCompatActivity{
    RecyclerView recyclerView;
    Button addStudent;
    private DatabaseReference myRef;
    public ArrayList<Students> students;
    private RecyclerAdapter recyclerAdapter;
    private Button orderStudents;
    private EditText mEditTextAge;
    private EditText mEditTextAssignment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.daily_grading);
        recyclerView = findViewById(R.id.recyclerView);
        addStudent = findViewById(R.id.addStudentButton);
        mEditTextAge = findViewById(R.id.EditTextAge);
        mEditTextAssignment = findViewById(R.id.EditTextAssignment);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView.setHasFixedSize(true);
        myRef = FirebaseDatabase.getInstance().getReference();
        students = new ArrayList<>();
        ClearAll();
        GetDataFromFirebase();
    }
    // fetches images and name from firebase
    private void GetDataFromFirebase() {
        Query query = myRef.child("student");
        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                ClearAll();
                for(DataSnapshot snapshot: dataSnapshot.getChildren()) {
                    Students student = new Students();
                    if (snapshot.child("url").getValue() == null) {
                        student.setImageUrl(snapshot.child("imageUrl").getValue().toString());
                    }
                    else {
                        student.setImageUrl(snapshot.child("url").getValue().toString());
                    }
                    student.setName(snapshot.child("name").getValue().toString());
                    students.add(student);
                }
                recyclerAdapter = new RecyclerAdapter(getApplicationContext(), students);
                recyclerView.setAdapter(recyclerAdapter);
                recyclerAdapter.notifyDataSetChanged();
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
    }
    // will clear recyclerAdapter
    private void ClearAll() {
        if (students != null) {
            students.clear();
            if(recyclerAdapter != null) {
                recyclerAdapter.notifyDataSetChanged();
            }
        }
        students = new ArrayList<>();
    }
    // method to generate checkboxes dynamically
    public void generateButtonsDynamically() {
        for(int i = 0; i < students.size(); i++){
        }
    }
RecyclerAdapter.class
package com.example.studenttracker;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
    private OnItemClickListener mListener;
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }
    private static final String Tag = "RecyclerView";
    private Context mContext;
    private ArrayList<Students> studentsArrayList;
    public RecyclerAdapter(Context mContext, ArrayList<Students> studentsArrayList) {
        this.mContext = mContext;
        this.studentsArrayList = studentsArrayList;
    }
    @NonNull
    @Override
    public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.student_item,parent,false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //TextView
        holder.textView.setText(studentsArrayList.get(position).getName());
        Glide.with(mContext).load(studentsArrayList.get(position).getImageUrl()).into(holder.imageView);
    }
    @Override
    public int getItemCount() {
        return studentsArrayList.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
            textView = itemView.findViewById(R.id.textView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        int position = getAdapterPosition();
                        
                    }
                }
            });
        }
    }
}
答案1
得分: 1
在 student_item.xml 而非 daily_grading.xml 中添加复选框。
英文:
Add Check Boxes in student_item.xml instead daily_grading.xml
答案2
得分: 1
请尝试以下代码:
<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="PASS"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="@+id/recyclerView"
    app:layout_constraintHorizontal_bias="0.046"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.499"
    android:visibility="gone" />
<CheckBox
    android:id="@+id/checkBox2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="FAIL"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.261"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.499"
    android:visibility="gone" />
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    //TextView
    holder.textView.setText(studentsArrayList.get(position).getName());
    Glide.with(mContext).load(studentsArrayList.get(position).getImageUrl()).into(holder.imageView);
    if (studentsArrayList.get(position).displayButtons()) { //check if you need the buttons or not
        holder.checkBox.setVisibility(View.VISIBLE);
        holder.checkBox2.setVisibility(View.VISIBLE);
    } else {
        holder.checkBox.setVisibility(View.GONE);
        holder.checkBox2.setVisibility(View.GONE);
    }
}
英文:
Try this:
<CheckBox
        android:id="@+id/checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PASS"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/recyclerView"
        app:layout_constraintHorizontal_bias="0.046"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499"
        android:visibility="gone" />
    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="FAIL"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.261"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499"
        android:visibility="gone" />
and
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    //TextView
    holder.textView.setText(studentsArrayList.get(position).getName());
    Glide.with(mContext).load(studentsArrayList.get(position).getImageUrl()).into(holder.imageView);
    if (studentsArrayList.get(position).displayButtons()) { //check if you need the buttons or not
        holder.checkBox.setVisibility(View.VISIBLE);
        holder.checkBox2.setVisibility(View.VISIBLE);
    } else {
        holder.checkBox.setVisibility(View.GONE);
        holder.checkBox2.setVisibility(View.GONE);
    }
}
答案3
得分: 1
把你的 student_item.xml 代码改成这样:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="wrap_content">
    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:foreground="?android:attr/selectableItemBackground"
        app:cardElevation="2dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    </androidx.cardview.widget.CardView>
    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="52dp">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="50dp"
            android:paddingTop="20dp"
            android:scaleType="centerCrop" />
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageView"
            android:layout_margin="10dp"
            android:textSize="16sp" />
        <CheckBox
            android:id="@+id/passc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/textView"
            android:text="通过" />
        <CheckBox
            android:id="@+id/failc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/textView"
            android:layout_toRightOf="@+id/passc"
            android:text="失败" />
    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
然后从 RecyclerAdapter.class 控制复选框的行为。
英文:
Change the code of your student_item.xml to this :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:foreground="?android:attr/selectableItemBackground"
app:cardElevation="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.cardview.widget.CardView>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="52dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginTop="50dp"
android:paddingTop="20dp"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_margin="10dp"
android:textSize="16sp" />
<CheckBox
android:id="@+id/passc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:text="pass" />
<CheckBox
android:id="@+id/failc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:layout_toRightOf="@+id/passc"
android:text="fail" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
and control the behaviour of the checkboxes from RecyclerAdapter.class
答案4
得分: 1
步骤1:在一个XML文件中添加RecyclerView。
步骤2:在另一个XML文件中,将必须放在RecyclerView内部的元素添加进去(在此处添加所需的按钮)。
步骤3:将这两个XML文件链接起来。
这样,每次动态插入后,都会添加一个按钮。
希望您能找到解决方案。
英文:
Step 1: Add the recyclerview in one xml file.
Step 2: The elements that must be inside a reecyclerview in another xml file (Here add a button that you need).
Step 3: Link these two xml files.
Thus after each insertion dynamically a button will be added.
Hope you will find the solution.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论