显示正确视图的 XML

huangapple go评论79阅读模式
英文:

Showing xml for correct view

问题

以下是你提供的代码部分的翻译:

ViewStudents.java

// 这部分代码没有需要翻译的内容,已包含在问题描述中。

RecyclerAdapter.java

// 这部分代码没有需要翻译的内容,已包含在问题描述中。

activity_view_students.xml

<!-- 这部分代码没有需要翻译的内容,已包含在问题描述中。 -->

daily_grading.xml

<!-- 这部分代码没有需要翻译的内容,已包含在问题描述中。 -->

activity_student_item.xml

<!-- 这部分代码没有需要翻译的内容,已包含在问题描述中。 -->

GradingRecyclerAdapter

// 这部分代码没有需要翻译的内容,已包含在问题描述中。

activity_grading_student_item.xml

<!-- 这部分代码没有需要翻译的内容,已包含在问题描述中。 -->

请注意,这是你提供的代码部分的翻译。如果你有其他问题或需要进一步帮助,请随时提问。

英文:

When a user logs in to my app, they can either click the view students button or daily grading button. The view students will display a student's image and their name. The daily grading will display the student's image, name, and two checkboxes that says pass or fail. Now the issue I have is that the checkboxes for pass and fail are showing up in my activity_view_students.xml view when it should not be. It should only show when a user clicks daily grading. I will put images below to make it clearer

What it looks like in the activity_view_students.xml

显示正确视图的 XML

What it should look like in activity_view_students.xml

显示正确视图的 XML

I will paste all relevant code below.

ViewStudents.java

package com.example.studenttracker;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;


public class ViewStudents extends AppCompatActivity {


    RecyclerView recyclerView;
    Button addStudent;


    private DatabaseReference myRef;

    public ArrayList&lt;Students&gt; 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.activity_view_students);

        recyclerView = findViewById(R.id.recyclerView);
        addStudent = findViewById(R.id.addStudentButton);
        mEditTextAge = findViewById(R.id.EditTextAge);
        mEditTextAssignment = findViewById(R.id.EditTextAssignment);
        orderStudents = findViewById(R.id.orderStudents);

        addStudent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(ViewStudents.this, AddStudent.class));
            }
        });

        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView.setHasFixedSize(true);

        myRef = FirebaseDatabase.getInstance().getReference();

        students = new ArrayList&lt;&gt;();

        ClearAll();

        GetDataFromFirebase();



    }
    private void GetDataFromFirebase() {
        Query query = myRef.child(&quot;student&quot;);

        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                ClearAll();
                for(DataSnapshot snapshot: dataSnapshot.getChildren()) {
                    Students student = new Students();
                    if (snapshot.child(&quot;url&quot;).getValue() == null) {
                        student.setImageUrl(snapshot.child(&quot;imageUrl&quot;).getValue().toString());
                    }
                    else {
                        student.setImageUrl(snapshot.child(&quot;url&quot;).getValue().toString());

                    }
//                    student.setAge(mEditTextAge.getText().toString());
//                    student.setAssignment(mEditTextAssignment.getText().toString().trim());
                    student.setName(snapshot.child(&quot;name&quot;).getValue().toString());
                    students.add(student);
                }
                recyclerAdapter = new RecyclerAdapter(getApplicationContext(), students);
                recyclerView.setAdapter(recyclerAdapter);
                recyclerAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
    private void ClearAll() {
        if (students != null) {
            students.clear();

            if(recyclerAdapter != null) {
                recyclerAdapter.notifyDataSetChanged();
            }
        }
        students = new ArrayList&lt;&gt;();
    }

    public void orderStudents(View view) {
        Collections.sort( students, new Comparator&lt;Students&gt;() {
            @Override
            public int compare( Students o1, Students o2 ) {
                return o1.name.compareTo( o2.name );
            }
        });
        recyclerAdapter.notifyDataSetChanged();

    }

}

RecyclerAdapter.java

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.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&lt;RecyclerAdapter.ViewHolder&gt; {
    private OnItemClickListener mListener;
    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }
    private static final String Tag = &quot;RecyclerView&quot;;
    private Context mContext;
    private ArrayList&lt;Students&gt; studentsArrayList;

    public RecyclerAdapter(Context mContext, ArrayList&lt;Students&gt; 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.activity_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);

// if (studentsArrayList.get(position).get) { //check if you need the buttons or not
// holder..setVisibility(View.VISIBLE);
// holder.checkBox2.setVisibility(View.VISIBLE);
// } else {
// holder.checkBox.setVisibility(View.GONE);
// holder.checkBox2.setVisibility(View.GONE);
// }
    }
    @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();

                    }
                }
            });
        }
    }
}

activity_view_students.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.ViewStudents&quot;&gt;

    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/recyclerView&quot;
        android:layout_width=&quot;409dp&quot;
        android:layout_height=&quot;729dp&quot;
        android:layout_marginEnd=&quot;1dp&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; &gt;

    &lt;/androidx.recyclerview.widget.RecyclerView&gt;

    &lt;Button
        android:id=&quot;@+id/addStudentButton&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;50dp&quot;
        android:text=&quot;Add Students&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;


    &lt;Button
        android:id=&quot;@+id/orderStudents&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:onClick=&quot;orderStudents&quot;
        android:text=&quot;Order Students&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

daily_grading.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.DailyGrading&quot;&gt;

    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/recyclerView&quot;
        android:layout_width=&quot;409dp&quot;
        android:layout_height=&quot;729dp&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

activity_student_item.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    tools:context=&quot;.DailyGrading&quot;&gt;


    &lt;androidx.cardview.widget.CardView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginTop=&quot;50dp&quot;
        android:foreground=&quot;?android:attr/selectableItemBackground&quot;
        app:cardElevation=&quot;2dp&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;

    &lt;/androidx.cardview.widget.CardView&gt;

    &lt;RelativeLayout
        android:id=&quot;@+id/relativeLayout2&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:padding=&quot;5dp&quot;

        tools:layout_editor_absoluteX=&quot;0dp&quot;
        tools:layout_editor_absoluteY=&quot;52dp&quot;&gt;

        &lt;ImageView
            android:id=&quot;@+id/imageView&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;200dp&quot;
            android:layout_marginTop=&quot;50dp&quot;
            android:paddingTop=&quot;20dp&quot;
            android:scaleType=&quot;centerCrop&quot; /&gt;

        &lt;TextView
            android:id=&quot;@+id/textView&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_below=&quot;@+id/imageView&quot;
            android:layout_margin=&quot;10dp&quot;
            android:textSize=&quot;16sp&quot; /&gt;

        &lt;CheckBox
            android:id=&quot;@+id/passc&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_below=&quot;@id/textView&quot;
            android:text=&quot;PASS&quot; /&gt;

        &lt;CheckBox
            android:id=&quot;@+id/failc&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_below=&quot;@id/textView&quot;
            android:layout_toRightOf=&quot;@+id/passc&quot;
            android:text=&quot;FAIL&quot; /&gt;

    &lt;/RelativeLayout&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

GradingRecyclerAdapter

package com.example.studenttracker;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class GradingRecyclerAdapter extends RecyclerView.Adapter&lt;GradingRecyclerAdapter.ViewHolder&gt; {
    private OnItemClickListener mListener;


    public interface OnItemClickListener {
        void onItemClick(int position);
    }
    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }
    private static final String Tag = &quot;RecyclerView&quot;;
    private Context mContext;
    private ArrayList&lt;Students&gt; studentsArrayList;

    public GradingRecyclerAdapter(Context mContext, ArrayList&lt;Students&gt; studentsArrayList) {
        this.mContext = mContext;
        this.studentsArrayList = studentsArrayList;
    }

    @NonNull
    @Override
    public GradingRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.activity_grading_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;
        Button passButton;
        Button failButton;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.imageView);
            textView = itemView.findViewById(R.id.textView);
            passButton = itemView.findViewById(R.id.PASS);
            failButton = itemView.findViewById(R.id.FAIL);

//            passButton.setVisibility(View.GONE);
//            failButton.setVisibility(View.GONE);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        int position = getAdapterPosition();

                    }
                }
            });
        }
    }
}

activity_grading_student_item

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    tools:context=&quot;.DailyGrading&quot;&gt;


    &lt;androidx.cardview.widget.CardView
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginTop=&quot;50dp&quot;
        android:foreground=&quot;?android:attr/selectableItemBackground&quot;
        app:cardElevation=&quot;2dp&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;

    &lt;/androidx.cardview.widget.CardView&gt;

    &lt;RelativeLayout
        android:id=&quot;@+id/relativeLayout2&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        android:padding=&quot;5dp&quot;

        tools:layout_editor_absoluteX=&quot;0dp&quot;
        tools:layout_editor_absoluteY=&quot;52dp&quot;&gt;

        &lt;ImageView
            android:id=&quot;@+id/imageView&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;200dp&quot;
            android:layout_marginTop=&quot;50dp&quot;
            android:paddingTop=&quot;20dp&quot;
            android:scaleType=&quot;centerCrop&quot; /&gt;

        &lt;TextView
            android:id=&quot;@+id/textView&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_below=&quot;@+id/imageView&quot;
            android:layout_margin=&quot;10dp&quot;
            android:textSize=&quot;16sp&quot; /&gt;

        &lt;CheckBox
            android:id=&quot;@+id/PASS&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_below=&quot;@id/textView&quot;
            android:text=&quot;PASS&quot; /&gt;

        &lt;CheckBox
            android:id=&quot;@+id/FAIL&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_below=&quot;@id/textView&quot;
            android:layout_toRightOf=&quot;@+id/PASS&quot;
            android:text=&quot;FAIL&quot; /&gt;

    &lt;/RelativeLayout&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案1

得分: 1

你可以简单地使用不同的适配器,
创建另一个名为 "activity_view_student_item.xml" 的布局文件,并从其中移除复选框。
为该 RecyclerView 创建另一个适配器,然后将以下代码中的:

View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.activity_student_item, parent, false);

在新的适配器中替换为:

View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.activity_view_student_item, parent, false);

然后在 "ViewStudents" 活动中将 RecyclerView 的适配器设置为新的适配器。

英文:

You can simply use different adapters
create another activity_student_item.xml let's say activity_view_student_item.xml and remove the checkboxes from that one
create another adapter for that recyclerView but change

View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.activity_student_item,parent,false);

in the new adapter to

View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.activity_view_student_item,parent,false);

and in the ViewStudents activity set the recycler's Adapter to that new adapter

huangapple
  • 本文由 发表于 2020年7月27日 06:09:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63106250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定