Create Custom Dialog in an Adapter - java

I'm trying to display a Custom Dialog tapping a Card (I use recyclerview and firebase). I have an Adapter in which I have the onClick method. In there I want to put my custom dialog in order to open it when I click the card.
Here is the ArticoloAdapter.java
public class ArticoloAdapter extends RecyclerView.Adapter<ArticoloAdapter.VersionVH> {
private List<Articolo> listaArticoli;
private Context mCtx;
public ArticoloAdapter(Context mCtx, List<Articolo> listaArticoli){
this.mCtx = mCtx;
this.listaArticoli = listaArticoli;
}
#NonNull
#Override
public ArticoloAdapter.VersionVH onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.articolo_stock, parent, false);
return new VersionVH(view);
}
#Override
public void onBindViewHolder(#NonNull final ArticoloAdapter.VersionVH holder, int position) {
final Articolo articolo = listaArticoli.get(position);
holder.nome.setText(articolo.getNome());
if(articolo.getQuantita() == 1){
String quantita = Integer.toString(articolo.getQuantita()) + " Disponibile";
holder.quantita.setText(quantita);
} else {
String quantita = Integer.toString(articolo.getQuantita()) + " Disponibili";
holder.quantita.setText(quantita);
}
Picasso.get().load(articolo.getImg()).into(holder.img);
}
public int getItemCount(){
return listaArticoli.size();
}
public class VersionVH extends RecyclerView.ViewHolder {
private CardView cardView;
private ImageView img;
private TextView nome, quantita;
public VersionVH(View itemView){
super(itemView);
nome = itemView.findViewById(R.id.nomeArticolo);
quantita = itemView.findViewById(R.id.quantita);
img = itemView.findViewById(R.id.immagine);
cardView = itemView.findViewById(R.id.cardView);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//here i want to put my custom_dialog
}
});
}
}
}
Here is my StockFragment (in which I have my card retrieved from Firebase):
public class StockFragment extends Fragment {
private RecyclerView recyclerView;
private List<Articolo> listaArticoli;
private ArticoloAdapter articoloAdapter;
private DatabaseReference query;
private LinearLayoutManager mLayoutManager;
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_stock, container, false);
mLayoutManager = new LinearLayoutManager(getActivity());
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
recyclerView = root.findViewById(R.id.recycleViewStock);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(articoloAdapter);
listaArticoli = new ArrayList<>();
query = FirebaseDatabase.getInstance().getReference("Articoli");
query.addListenerForSingleValueEvent(valueEventListener);
return root;
}
ValueEventListener valueEventListener = new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
listaArticoli.clear();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
Articolo articolo = snapshot.getValue(Articolo.class);
listaArticoli.add(articolo);
}
articoloAdapter = new ArticoloAdapter(getActivity(), listaArticoli);
recyclerView.setAdapter(articoloAdapter);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {}
};
}
and here is my custom_dialog.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="350dp"
android:layout_height="650dp"
android:background="#drawable/dialog_bg"
tools:ignore="UseSwitchCompatOrMaterialXml">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06999999"
app:srcCompat="#android:drawable/sym_def_app_icon">
</ImageView>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="146dp"
android:layout_marginEnd="146dp"
android:text="Nome Prodotto"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.19999999">
</TextView>
<com.google.android.material.textview.MaterialTextView
android:id="#+id/materialTextView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:gravity="center_horizontal"
android:inputType="textMultiLine"
android:text="Ciao questo รจ un testo di prova per daniele e vedere se gli piace o meno come esce"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3">
</com.google.android.material.textview.MaterialTextView>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="32dp"
android:hint="Nome"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/materialTextView"
app:layout_constraintVertical_bias="0.0"
android:layout_marginLeft="50dp"
android:layout_marginRight="32dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/textNome"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout2"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="32dp"
android:layout_marginRight="32dp"
android:hint="Cognome"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout"
app:layout_constraintVertical_bias="0.0">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/textCognome"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout3"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="32dp"
android:hint="Numero Di Telefono"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout2"
app:layout_constraintVertical_bias="0.0"
android:layout_marginLeft="50dp"
android:layout_marginRight="32dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/textNumero"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.textfield.TextInputLayout>
<EditText
android:id="#+id/testEnabeld"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Prova"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/switchGaranzia"
app:layout_constraintVertical_bias="0.0" />
<Switch
android:id="#+id/switchGaranzia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginBottom="2dp"
android:text="Garanzia"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout3"
app:layout_constraintVertical_bias="0.0" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="49dp"
android:layout_height="328dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.925">
<ImageView
android:layout_width="35dp"
android:layout_height="54dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="1dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="16dp"
app:srcCompat="#drawable/icons8_utente_24" />
<ImageView
android:layout_width="35dp"
android:layout_height="54dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="16dp"
app:srcCompat="#drawable/icons8_utente_24" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Use the code below and add a child with match_parent attribute width and height inside the CardView and add the listener to the child view instead of the CardVeiw
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//here i want to put my custom_dialog
Dialog dialog = new Dialog(mContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog);
dialog.getWindow().setLayout(ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.WRAP_CONTENT);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
}
});

Related

RecyclerView only lists first item despite layout changes

I understand this might be a duplicate, but every other solution here didn't work in my case, such as changing the layout_height to wrap_content instead of match_parent.
My Firestore database has 2 values stored inside an array, and I want to display both of them (more in the future) in my RecyclerView, however, it only displays the first value, at position 0.
Here is my Activity where the Recycler and the Adapter are created:
ActivityOs
private FirestoreAdapter listAdapter;
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference osRef = db.collection("cliente");
private DocumentReference docRef = osRef.document("clienteTeste");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_os);
RecyclerView recyclerView = findViewById(R.id.recyclerViewOs);
/*docRef.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.e("RESULTADO","result:" + document.toString());
}
}
});*/
Query query = osRef.orderBy("serviceOrder",
Query.Direction.ASCENDING);
FirestoreRecyclerOptions<ServiceDocument> options =
new FirestoreRecyclerOptions.Builder<ServiceDocument>()
.setQuery(osRef, ServiceDocument.class)
.build();
listAdapter = new FirestoreAdapter(options);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(listAdapter);
listAdapter.notifyDataSetChanged();
}
#Override
protected void onStart() {
super.onStart();
listAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
listAdapter.stopListening();
}
The commented code shows me that the database does indeed return the 2 values stored in it.
Here is my Adapter class:
public class FirestoreAdapter extends FirestoreRecyclerAdapter<ServiceDocument, FirestoreAdapter.ViewHolder> {
public FirestoreAdapter(#NonNull FirestoreRecyclerOptions<ServiceDocument> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull ServiceDocument model) {
holder.osIdItem.setText(String.valueOf(model.serviceOrderArray.get(position).getId()));
holder.osClientItem.setText(model.serviceOrderArray.get(position).getClient().getName());
holder.osDateItem.setText(model.serviceOrderArray.get(position).getDate());
holder.osValueItem.setText(String.valueOf(model.serviceOrderArray.get(position).getTotalValue()));
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.layout_os_item, parent, false);
return new FirestoreAdapter.ViewHolder(view);
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView osIdItem;
private TextView osClientItem;
private TextView osDateItem;
private TextView osValueItem;
public ViewHolder(View itemView) {
super(itemView);
osIdItem = itemView.findViewById(R.id.osIDItem);
osClientItem = itemView.findViewById(R.id.osClientNameItem);
osDateItem = itemView.findViewById(R.id.osDateItem);
osValueItem = itemView.findViewById(R.id.osValueItem);
}
}
}
And here are my layouts, activity_os.xml where my RecyclerView is created, and layout_os_item.xml where the layouts for each item of the recycler are made.
Activity_os.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/activity_os"
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"
android:layout_marginBottom="0dp"
android:background="#color/white"
tools:context="com.leonardomaito.autocommobile.activities.OsActivity">
<include
android:id="#+id/include"
layout="#layout/layout_header_search" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/glRecyclerLimit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="124dp" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewOs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="650dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/glRecyclerLimit"
app:layout_constraintVertical_bias="0.0"
tools:listitem="#layout/layout_os_item" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/btNewOs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Inserir OS"
android:onClick="createNewOs"
android:src="#drawable/custom_plus_icon"
app:backgroundTint="#color/autocom_blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.98"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/glRecyclerLimit"
app:layout_constraintVertical_bias="0.988"
app:rippleColor="#color/autocom_blue" />
</androidx.constraintlayout.widget.ConstraintLayout>
Layout_os_item.xml
<androidx.cardview.widget.CardView
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"
tools:showIn="#layout/activity_os"
android:background="#drawable/custom_card_view">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:background="#drawable/custom_card_view">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/custom_os_icon"
android:paddingLeft="5dp"/>
<TextView
android:id="#+id/osIDItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="N 00000"
android:textColor="#color/autocom_blue"
android:textStyle="bold"
app:layout_constraintEnd_toStartOf="#+id/osDateItem"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/osDateItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/autocom_blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.943"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.222" />
<TextView
android:id="#+id/osClientNameItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:textColor="#color/autocom_blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView"
app:layout_constraintTop_toBottomOf="#+id/osIDItem"
app:layout_constraintVertical_bias="0.111" />
<TextView
android:id="#+id/osValueItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:text="R$ 10,000"
android:textColor="#color/autocom_blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.791"
app:layout_constraintStart_toEndOf="#+id/osClientNameItem"
app:layout_constraintTop_toBottomOf="#+id/osDateItem"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Any help is welcome!
Edit
Current Result:
Expected Result
Basically, Item 0 or the first item is correct, however, the second item in my database should also appear in Item 1.

My ImageView disappears when I try to set the image from my Firebase storage

I want to set a picture from my storage by Picasso, however it doesnt work. Initially, there is an icon in my ImageView, but it doesnt show as well after I get to the Activity.
here's code
public class ProfileFragment extends Fragment {
TextView fullNameText, emailText, dateBirthText, phoneText;
TextView addressText;
FloatingActionButton profileEditBtn;
TextView uploadImgBtn;
ImageView profileImg;
String phone, link;
private DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference().child("image");
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Bundle bundle = this.getArguments();
phone = bundle.getString("phone");
View myView = inflater.inflate(R.layout.fragment_profile, container, false);
fullNameText = myView.findViewById(R.id.full_name_text);
emailText = myView.findViewById(R.id.email_text);
dateBirthText = myView.findViewById(R.id.date_birth_text);
addressText = myView.findViewById(R.id.address_text);
phoneText = myView.findViewById(R.id.phone_text);
profileImg = myView.findViewById(R.id.profile_img);
uploadImgBtn = myView.findViewById(R.id.upload_img_btn);
uploadImgBtn.setOnClickListener(view -> {
Intent intent = new Intent(getContext(), UploadProfileImage.class);
intent.putExtra("phone", phone);
startActivity(intent);
});
return myView;
}
#Override
public void onStart() {
super.onStart();
dbRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
link = dataSnapshot.getValue(String.class);
// this is the link to the image in firebase
System.out.println(link);
Picasso.get()
.load(link)
.fit()
.centerCrop()
.into(profileImg);
profileImg.setVisibility(View.VISIBLE);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
my 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" >
<TextView
android:id="#+id/profile_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="My personal data"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.434" />
<TextView
android:id="#+id/full_name_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="Full Name: "
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/profile_title"
app:layout_constraintVertical_bias="0.053" />
<TextView
android:id="#+id/email_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="Email: "
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/profile_title"
app:layout_constraintVertical_bias="0.175" />
<TextView
android:id="#+id/date_birth_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="Date of birth: "
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/profile_title"
app:layout_constraintVertical_bias="0.416" />
<TextView
android:id="#+id/address_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="Address: "
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/profile_title"
app:layout_constraintVertical_bias="0.527" />
<TextView
android:id="#+id/phone_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:text="Phone: "
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/profile_title"
app:layout_constraintVertical_bias="0.293" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/profile_edit_btn"
android:layout_width="64dp"
android:layout_height="64dp"
android:clickable="true"
android:contentDescription="Edit profile"
app:fabCustomSize="64dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.953"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/address_text"
app:layout_constraintVertical_bias="0.198"
app:srcCompat="#drawable/ic_edit" />
<TextView
android:id="#+id/upload_img_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Upload profile image"
android:textColor="#03A9F4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/profile_edit_btn"
app:layout_constraintHorizontal_bias="0.079"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/address_text"
app:layout_constraintVertical_bias="0.288" />
<ImageView
android:id="#+id/profile_img"
android:layout_width="230dp"
android:layout_height="230dp"
app:layout_constraintBottom_toTopOf="#+id/profile_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.702"
tools:ignore="ImageContrastCheck"
android:src="#drawable/ic_android"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I ve tried to use Glide instead of picasso, but it didnt work anyways. I'm also pretty sure that my link to file is correct.
realtime db
{
"Users" : {
"123456789" : {
"address" : "",
"date" : "2022-03-27",
"email" : "admin#gmail.com",
"fullName" : "me",
"password" : "qwerty12345",
"phone" : "123456789"
}
},
"image" : "gs://myproj-8984f.appspot.com/images/gachiava.png"
}
Please check whether the Picasso can load with another url by manally adding a url.
Picasso.get().load("your link here").fit().centerCrop().into(profileImg);
And make sure you have added the correct internet permission to Android manifest.

Android Java Cannot find local variable

My MainActivity
public class MainActivity extends AppCompatActivity {//My MainActivity
ArrayList<Response> responses = new ArrayList<>();
private String BASE_URL = "https://covid-193.p.rapidapi.com/";
RecyclerView recyclerView;
MyRecycleViewAdapter myRecycleViewAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycleViewCovid);
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create());//this is convert json
Retrofit retrofit = builder.build();
Covid19WebApi covid19WebApi = retrofit.create(Covid19WebApi.class);
Call<Covid19Model> call = covid19WebApi.getData();//this is call api interfacee method
call.enqueue(new Callback<Covid19Model>() {
#Override
public void onResponse(Call<Covid19Model> call, retrofit2.Response<Covid19Model> response) {
responses = (ArrayList<Response>) response.body().getResponse();//This is my Api response 232 items
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
myRecycleViewAdapter = new MyRecycleViewAdapter(responses);//My Adapter passing data in my Adapter
recyclerView.setAdapter(myRecycleViewAdapter);
}
#Override
public void onFailure(Call<Covid19Model> call, Throwable t) {
Toast.makeText(MainActivity.this,t.getLocalizedMessage(),Toast.LENGTH_LONG).show();//this is toast message failure
}
});
}
My Adapter
public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder>
{
private ArrayList<Response> responseArrayList;
public MyRecycleViewAdapter ( ArrayList<Response> responseArrayList) {
this.responseArrayList = responseArrayList;//Response data items in My Adapter
}
#NonNull
#Override
public MyRecycleViewAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());//Api passing data added this layout
View view = layoutInflater.inflate(R.layout.row_layout,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyRecycleViewAdapter.ViewHolder holder, int position) {
holder.setData(responseArrayList.get(position));//Api Items List
}
#Override
public int getItemCount() {
return responseArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView continent;
TextView country;
TextView population;
TextView casesNew;
TextView caseActive;
TextView caseCritical;
TextView caseRecovered;
TextView caseM_pop;
TextView caseTotal;
TextView deathsNew;
TextView deathsM_pop;
TextView deathsTotal;
TextView testsM_pop;
TextView testsTotal;
TextView day;
TextView time;
TextView Deaths;
TextView Tests;
TextView Cases;
public ViewHolder(#NonNull View itemView) {//My ViewHolder Connect Api data and View Layout TextView
super(itemView);
continent = itemView.findViewById(R.id.continentName);//My layout Textview id
country = itemView.findViewById(R.id.countryName);
population =itemView.findViewById(R.id.populationName);
casesNew = itemView.findViewById(R.id.CaseNewName);
caseActive = itemView.findViewById(R.id.casesActiveName);
caseCritical=itemView.findViewById(R.id.criticalName);
caseRecovered=itemView.findViewById(R.id.recoveredName);
caseM_pop = itemView.findViewById(R.id.M_popName);
caseTotal = itemView.findViewById(R.id.totalCases);
deathsNew = itemView.findViewById(R.id.deathsNewName);
deathsM_pop = itemView.findViewById(R.id.deaths1M_pop);
deathsTotal = itemView.findViewById(R.id.totalDeaths);
testsM_pop = itemView.findViewById(R.id.tests1M_pop);
testsTotal = itemView.findViewById(R.id.testsTotal);
day = itemView.findViewById(R.id.dayName);
time = itemView.findViewById(R.id.timeName);
Cases = itemView.findViewById(R.id.Cases);
Deaths = itemView.findViewById(R.id.Deaths);
Tests = itemView.findViewById(R.id.Tests);
}
private void setData(Response response){
Response res = response;
continent.setText(res.getContinent());//Debugging this TextView Api data is null
country.setText(res.getCountry());
population.setText(res.getPopulation());
casesNew.setText(res.getCases().getNew());
caseActive.setText(res.getCases().getActive());
caseCritical.setText(res.getCases().getCritical());
caseRecovered.setText(res.getCases().getRecovered());
caseM_pop.setText(res.getCases().get1MPop());
caseTotal.setText(res.getCases().getTotal());
deathsM_pop.setText(res.getDeaths().get1MPop());
deathsNew.setText(res.getDeaths().getNew());
deathsTotal.setText(res.getDeaths().getTotal());
testsM_pop.setText(res.getTests().get1MPop());
testsTotal.setText(res.getTests().getTotal());
day.setText(res.getDay());
time.setText(res.getTime());
Tests.setText(res.getTests().toString());
Cases.setText(res.getCases().toString());
Deaths.setText(res.getDeaths().toString());
}
}
}
My Problems
Please Help me What is the Problem?
I pull the data with the Api, but when I debug, I see that the data comes to My MainActivity and is added to the Adapter from there, but when the data reaches the Viewholder, it becomes null when set to the TextView. I don't understand what the problem is.
My Layout
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#8BC34A"
android:orientation="vertical">
<TextView
android:id="#+id/continentName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="continent"
android:textColor="#color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/countryName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="country"
android:textColor="#color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/continentName" />
<TextView
android:id="#+id/populationName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="population"
android:textColor="#color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/countryName" />
<TextView
android:id="#+id/dayName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="day"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/populationName" />
<TextView
android:id="#+id/timeName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/dayName" />
<TextView
android:id="#+id/CaseNewName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="new"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/timeName" />
<TextView
android:id="#+id/casesActiveName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="active"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/CaseNewName" />
<TextView
android:id="#+id/criticalName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="critical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/casesActiveName" />
<TextView
android:id="#+id/recoveredName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="recovered"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/criticalName" />
<TextView
android:id="#+id/M_popName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="1M_pop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recoveredName" />
<TextView
android:id="#+id/totalCases"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="total"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/M_popName" />
<TextView
android:id="#+id/deaths1M_pop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="1M_pop(deaths)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/totalCases" />
<TextView
android:id="#+id/deathsNewName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="new(Deaths)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/deaths1M_pop" />
<TextView
android:id="#+id/totalDeaths"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="total(deaths)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/deathsNewName" />
<TextView
android:id="#+id/tests1M_pop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="1M_pop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/totalDeaths" />
<TextView
android:id="#+id/testsTotal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:hint="total(tests)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tests1M_pop" />
<TextView
android:id="#+id/Deaths"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="Deaths"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/testsTotal" />
<TextView
android:id="#+id/Tests"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="Tests"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Deaths" />
<TextView
android:id="#+id/Cases"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:hint="Cases"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Tests" />
</LinearLayout>"

Android spinner in a Dialog, when we clik on a item listview doesn't work

I start on android. In an activity, I create a listview with a custom view, when I click I open a dialog with several spinners and text views. However, my spinner doesn't work. The list of the spinner appears but when I click nothing happens and there is no default value displayed.
Thanks in advance for your help!
Here's a drive link to see what's going on: https://drive.google.com/file/d/1t2omOA7gJQJNtTVV6Wig5EShYo5cwj0v/view?usp=sharing
Here is my code:
TeamActivity.java
public class TeamActivity extends AppCompatActivity{
ArrayList<Team> teams;
static String[] selected;
static int id_selected;
Spinner s_p1;
ArrayAdapter ad_sp1;
Spinner s_p2;
Spinner s_p3;
//Constructeur
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_team);
/*Team creation*/
teams = new ArrayList<Team>();
int id = 1;
for (int i = 0; i < participants.size(); i = i + 3) {
teams.add(new Team(id, participants.get(i), participants.get(i + 1), participants.get(i + 2)));
id++;
}
/*list view*/
final ListView listView = (ListView) findViewById(R.id.listView_team);
// Create the adapter to convert the array to views
final TeamAdapter adapter = new TeamAdapter(this, teams);
// Attach the adapter to a ListView
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, final View view, int i, long l) {
id_selected = i;
AlertDialog.Builder d_builder = new AlertDialog.Builder(listView.getContext());
View d_order = getLayoutInflater().inflate(R.layout.dialog_order,null);
s_p1 = (Spinner) d_order.findViewById(R.id.spinner_p1);
selected = new String[3];
ad_sp1 = new ArrayAdapter<String>(listView.getContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.value));
ad_sp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s_p1.setAdapter(ad_sp1);
d_builder.setPositiveButton("Validez", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
adapter.notifyDataSetChanged();
dialog.dismiss();
}
});
d_builder.setView(d_order);
AlertDialog dialog = d_builder.create();
dialog.show();
}
});
}
}
layout/dialog_order.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="#FFFFFF">
<Spinner
android:id="#+id/spinner_p3"
android:layout_width="84dp"
android:layout_height="41dp"
android:layout_marginStart="6dp"
android:layout_marginTop="116dp"
android:layout_toEndOf="#+id/textView_fname_p3"
android:backgroundTint="#000000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView_name_p1"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="20dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="25dp" />
<TextView
android:id="#+id/textView_fname_p1"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="268dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="276dp" />
<TextView
android:id="#+id/textView_name_p2"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="21dp"
android:layout_marginTop="63dp"
android:text="TextView"
android:textSize="30sp"
tools:layout_editor_absoluteX="25dp"
tools:layout_editor_absoluteY="66dp" />
<TextView
android:id="#+id/textView_fname_p2"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="268dp"
android:layout_marginTop="65dp"
android:text="TextView"
android:textSize="30sp"
tools:layout_editor_absoluteX="276dp"
tools:layout_editor_absoluteY="66dp" />
<TextView
android:id="#+id/textView_name_p3"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="20dp"
android:layout_marginTop="112dp"
android:text="TextView"
android:textSize="30sp"
tools:layout_editor_absoluteX="25dp"
tools:layout_editor_absoluteY="116dp" />
<TextView
android:id="#+id/textView_fname_p3"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="268dp"
android:layout_marginTop="113dp"
android:text="TextView"
android:textSize="30sp"
tools:layout_editor_absoluteX="276dp"
tools:layout_editor_absoluteY="116dp" />
<Spinner
android:id="#+id/spinner_p1"
android:spinnerMode="dialog"
android:entries="#array/value"
android:layout_width="84dp"
android:layout_height="41dp"
android:layout_marginStart="7dp"
android:layout_marginTop="16dp"
android:layout_toEndOf="#+id/textView_fname_p1"
android:backgroundTint="#000000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="#+id/spinner_p2"
android:layout_width="84dp"
android:layout_height="41dp"
android:layout_marginStart="7dp"
android:layout_marginTop="64dp"
android:layout_toEndOf="#+id/textView_fname_p2"
android:backgroundTint="#000000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
```
**values/order_value.xml**
```<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="value">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources>
TeamAdapter.java
public class TeamAdapter extends ArrayAdapter<Team> {
// View lookup cache
private static class ViewHolder {
TextView name_p1;
TextView name_p2;
TextView name_p3;
TextView fname_p1;
TextView fname_p2;
TextView fname_p3;
TextView level;
TextView id_team;
}
public TeamAdapter(Context context, ArrayList<Team> t) {
super(context, R.layout.item_team, t);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Team t = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
// If there's no view to re-use, inflate a brand new view for row
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_team, parent, false);
viewHolder.name_p1 = (TextView) convertView.findViewById(R.id.textView_name_p1);
viewHolder.name_p2 = (TextView) convertView.findViewById(R.id.textView_name_p2);
viewHolder.name_p3 = (TextView) convertView.findViewById(R.id.textView_name_p3);
viewHolder.fname_p1 = (TextView) convertView.findViewById(R.id.textView_fname_p1);
viewHolder.fname_p2 = (TextView) convertView.findViewById(R.id.textView_fname_p2);
viewHolder.fname_p3 = (TextView) convertView.findViewById(R.id.textView_fname_p3);
viewHolder.level = (TextView) convertView.findViewById(R.id.textView_nb_lvl);
viewHolder.id_team = (TextView) convertView.findViewById(R.id.textView_id_team);
// Cache the viewHolder object inside the fresh view
convertView.setTag(viewHolder);
} else {
// View is being recycled, retrieve the viewHolder object from tag
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data from the data object via the viewHolder object
// into the template view.
viewHolder.name_p1.setText(t.getParticipants().get(0).getName());
viewHolder.name_p2.setText(t.getParticipants().get(1).getName());
viewHolder.name_p3.setText(t.getParticipants().get(2).getName());
viewHolder.fname_p1.setText(t.getParticipants().get(0).getFirstName());
viewHolder.fname_p2.setText(t.getParticipants().get(1).getFirstName());
viewHolder.fname_p3.setText(t.getParticipants().get(2).getFirstName());
viewHolder.level.setText(String.valueOf(t.getLevel()));
viewHolder.id_team.setText(String.valueOf(t.getId()));
// Return the completed view to render on screen
return convertView;
}
}
item_team.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView_name_p1"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="206dp"
android:layout_marginTop="7dp"
android:text="TextView"
android:textSize="30sp" />
<TextView
android:id="#+id/textView_name_p2"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="206dp"
android:layout_marginTop="52dp"
android:text="TextView"
android:textSize="30sp" />
<TextView
android:id="#+id/textView_name_p3"
android:layout_width="237dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="206dp"
android:layout_marginTop="97dp"
android:text="TextView"
android:textSize="30sp" />
<TextView
android:id="#+id/textView_fname_p1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="461dp"
android:layout_marginTop="7dp"
android:text="TextView"
android:textSize="30sp" />
<TextView
android:id="#+id/textView_fname_p2"
android:layout_width="251dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="461dp"
android:layout_marginTop="52dp"
android:text="TextView"
android:textSize="30sp" />
<TextView
android:id="#+id/textView_fname_p3"
android:layout_width="252dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="461dp"
android:layout_marginTop="97dp"
android:text="TextView"
android:textSize="30sp" />
<ImageView
android:id="#+id/imageViewP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="728dp"
android:layout_marginTop="11dp"
android:src="#drawable/pf" />
<ImageView
android:id="#+id/imageViewP2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="730dp"
android:layout_marginTop="55dp"
android:src="#drawable/ps" />
<ImageView
android:id="#+id/imageViewP3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="728dp"
android:layout_marginTop="101dp"
android:src="#drawable/pt" />
<TextView
android:id="#+id/textView_lvl_team"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="36dp"
android:layout_marginTop="6dp"
android:text="Niveau :"
android:textSize="30sp" />
<TextView
android:id="#+id/textView_nb_lvl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="153dp"
android:layout_marginTop="7dp"
android:textSize="30sp" />
<TextView
android:id="#+id/textView_id_team"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="4dp"
android:layout_marginTop="12dp"
android:textSize="20sp" />
</RelativeLayout>
Team.java
public class Team {
private ArrayList<Participant> _team;
private int _level;
private int _id;
/*Builder*/
public Team(int id,ArrayList<Participant>team, int lvl){
this._team = team;
this._level=lvl;
this._id=id;
}
public Team(int id,Participant p1, Participant p2,Participant p3,int lvl){
ArrayList<Participant> team = new ArrayList<Participant>();
team .add(p1);
team.add(p2);
team.add(p3);
this._team = team;
this._level=lvl;
this._id=id;
}
public Team(int id,Participant p1, Participant p2,Participant p3){
ArrayList<Participant> team = new ArrayList<Participant>();
team.add(p1);
team.add(p2);
team.add(p3);
this._team = team;
this._level=p1.getLevel()+p2.getLevel()+p3.getLevel();
this._id=id;
}
/*Getter & Setter*/
public void setParticipants(ArrayList<Participant> team){
this._team=team;
}
public void setParticipants(Participant p1, Participant p2,Participant p3){
ArrayList<Participant> team = new ArrayList<Participant>();
team .add(p1);
team.add(p2);
team.add(p3);
this._team = team;
}
public ArrayList<Participant> getParticipants(){
return _team;
}
public void setLevel(int lvl){
this._level=lvl;
}
public int getLevel(){
return _level;
}
public void setId(int id){
this._id=id;
}
public int getId(){
return _id;
}
}

Send data inside recyclerview with button in Fragment

I have 2 layouts where first layout for Fragment and second layout for RecyclerView. I have created CardView in RecyclerView, every CardView has some data. And I have created button below RecyclerView where the function of button for sending Data on CardView. My problem is I don't know how to send data when i click button in Fragment.
Fragment Layout :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="#+id/container_daftar_alamat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:scrollbars="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".ui.menu.menu_signin.menu.MenuAddressFragment">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<Button
android:id="#+id/buttonMoveTambahAlamat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:background="#null"
android:drawableStart="#drawable/ic_tambah_alamat"
android:drawablePadding="10dp"
android:text="Tambah Alamat"
android:textAllCaps="false"
android:textColor="#color/colorRed"
android:textSize="14sp"
android:textStyle="normal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:text="Daftar Alamat"
android:textColor="#color/colorRed"
android:textSize="14sp"
android:textStyle="bold" />
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rc_tambah_alamat"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/gunakanAlamat"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="100dp"
android:background="#drawable/bg_button_red"
android:text="Gunakan Alamat"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="16sp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</FrameLayout>
RecyclerView Layout :
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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"
android:layout_marginLeft="15dp"
android:layout_marginTop="12dp"
android:layout_marginRight="15dp"
android:layout_marginBottom="12dp"
android:orientation="vertical"
app:cardBackgroundColor="#color/colorWhite"
app:cardCornerRadius="4dp"
app:cardElevation="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/radioButton">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/nameUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:justificationMode="inter_word"
android:text="Muhammad Rafi Bahrur Rizki"
android:textColor="#color/colorBlack"
android:textSize="14sp"
android:textStyle="bold"
tools:ignore="UnusedAttribute" />
<TextView
android:id="#+id/addressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:maxLines="1"
android:text="(Alamat Kantor)"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/streetName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:justificationMode="inter_word"
android:text="Mangga Dua Square Lantai 1 Jakarta,"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp"
tools:ignore="UnusedAttribute" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/blokAddressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="Blok C no. 148 - 150"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/cityAddressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="DKI Jakarta - 15025"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/countryAddressUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="DKI Jakarta - 15025"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<TextView
android:id="#+id/phoneUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:text="0812951825"
android:textColor="#color/textColorSaksFifthAvenue"
android:textSize="14sp" />
</TableRow>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp">
<Button
android:id="#+id/buttonEdit"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginStart="8dp"
android:drawableStart="#drawable/ic_edit"
android:text="Edit"
android:textAllCaps="false"
android:textSize="12sp" />
<Button
android:id="#+id/buttonDelete"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_toEndOf="#id/buttonEdit"
android:drawableStart="#drawable/ic_delete"
android:text="Hapus"
android:textAllCaps="false"
android:textSize="12sp" />
</RelativeLayout>
</TableLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
When i select RadioButton, only data in RadioButton will be obtained. And when i click button, the data will be send to another activity.
Adapter RecyclerView :
public class AdapterGetAddress extends RecyclerView.Adapter<AdapterGetAddress.ViewHolder> {
Context context;
private List<ModelGetAddress> modelGetAddressList;
private BaseApiService baseApiService;
private int previousSelected = -1;
public AdapterGetAddress(Context context, List<ModelGetAddress> modelGetAddressList) {
this.context = context;
this.modelGetAddressList = modelGetAddressList;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_address, parent, false);
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
final ModelGetAddress adapterAddress = modelGetAddressList.get(position);
}
#Override
public int getItemCount() {
return modelGetAddressList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView nameUser, addressUser, streetName, blokAddressUser, cityAddressUser, countryUser, phoneUser;
private RadioButton radioButton;
public ViewHolder(#NonNull View itemView) {
super(itemView);
nameUser = itemView.findViewById(R.id.nameUser);
addressUser = itemView.findViewById(R.id.addressUser);
streetName = itemView.findViewById(R.id.streetName);
countryUser = itemView.findViewById(R.id.countryAddressUser);
blokAddressUser = itemView.findViewById(R.id.blokAddressUser);
cityAddressUser = itemView.findViewById(R.id.cityAddressUser);
phoneUser = itemView.findViewById(R.id.phoneUser);
radioButton = itemView.findViewById(R.id.radioButton);
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
previousSelected = getAdapterPosition();
notifyItemRangeChanged(0, modelGetAddressList.size());
// I want this All String will be send when i click button in Fragment
String getNameUser = nameUser.getText().toString();
String getAddressUser = streetName.getText().toString();
String getCountryUser = countryUser.getText().toString();
String getBlokUser = blokAddressUser.getText().toString();
String getCityUser = cityAddressUser.getText().toString();
String getPhoneUser = phoneUser.getText().toString();
}
});
}
}
}
Code in Fragment :
public class MenuAddressFragment extends Fragment {
private Button gunakanAlamat;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_menu_address, container, false);
gunakanAlamat = view.findViewById(R.id.gunakanAlamat);
gunakanAlamat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Here's were i'm stuck
}
});
return view;
}
}
You can solve this via an interface.
public interface ItemClickedCallback{
void onItemClicked(String nameUser //, the other data you wanna pass );
}
pass it through the constructor of your adapter.
Use this adapter constructor
ItemClickedCallback callback;
public AdapterGetAddress(Context context, List<ModelGetAddress> modelGetAddressList, ItemClickedCallback callback) {
this.context = context;
this.modelGetAddressList = modelGetAddressList;
this.callback= callback;
}
When checking a radio button of any item; call the interface method, like this.
radioButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
previousSelected = getAdapterPosition();
notifyItemRangeChanged(0, modelGetAddressList.size());
// I want this All String will be send when i click button in Fragment
String getNameUser = nameUser.getText().toString();
String getAddressUser = streetName.getText().toString();
String getCountryUser = countryUser.getText().toString();
String getBlokUser = blokAddressUser.getText().toString();
String getCityUser = cityAddressUser.getText().toString();
String getPhoneUser = phoneUser.getText().toString();
callback.onItemClicked(getNameUser //, the rest..);
}
});
Now, when you initialize the adapter in the fragment, create the new interface via the constructor and assign the data to class variables like this.
public class MenuAddressFragment extends Fragment {
private Button gunakanAlamat;
private String nameUser_;
// the rest...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_menu_address, container, false);
AdapterGetAddress adapter = new AdapterGetAddress(getActivity, list, new ItemClickedCallback() {
#Override
public void onItemClicked(String userName //, the rest..) {
// assign to the class variable like this
nameUser_ = userName;
// the rest..
}
}););
gunakanAlamat = view.findViewById(R.id.gunakanAlamat);
gunakanAlamat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Use the data as you want..
}
});
return view;
}
}

Categories