gpt4 book ai didi

android - 回收 View 仅显示 1 个元素

转载 作者:行者123 更新时间:2023-11-29 14:44:30 25 4
gpt4 key购买 nike

我正在使用 Firebase 在我的应用程序上实现一些逻辑。

我需要使用回收 View 和自定义适配器为我的用户设置一些信息,问题是每次我执行 prepareData() 时,只显示 1 个元素而不是我的所有数组,我认为它可以有一些东西与我的布局有关,因为在适配器上我收到了所有信息。

所以在我的主要 Activity 中我有这个:

package com.esmad.pdm.friendlymanager;

import android.content.Intent;
import android.icu.text.MessagePattern;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.esmad.pdm.friendlymanager.model.Event;
import com.esmad.pdm.friendlymanager.model.EventViewModel;
import com.esmad.pdm.friendlymanager.model.Participant;
import com.esmad.pdm.friendlymanager.other.EventGameAdapter;
import com.esmad.pdm.friendlymanager.other.Participants;
import com.google.firebase.auth.FirebaseAuth;
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.ValueEventListener;

import java.util.ArrayList;

public class MatchInvitation extends AppCompatActivity {

FirebaseDatabase database;
DatabaseReference myRef;
DatabaseReference user;
private FirebaseAuth auth;
String id;
String username;
RecyclerView ps;
Participants participantAdapter;
TextView title1;
ArrayList<Participant> participants;
boolean exists;
String matchId;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_invitation);

participants = new ArrayList<>();
title1 = (TextView)findViewById(R.id.textView3);
database = FirebaseDatabase.getInstance();
myRef = database.getReference("Matches");
user = database.getReference("Users");
auth = FirebaseAuth.getInstance();
id = auth.getCurrentUser().getUid();

Intent i = getIntent();
Bundle extras = i.getExtras();
if (extras != null) {
if (extras.containsKey("matchId")) {
matchId = getIntent().getStringExtra("matchId");
}
}



ps = (RecyclerView)findViewById(R.id.participants);

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);

ps = (RecyclerView)findViewById(R.id.participants);
ps.setHasFixedSize(true);
ps.setLayoutManager(linearLayoutManager);

participantAdapter = new Participants(getApplicationContext(), participants);
ps.setAdapter(participantAdapter);

loadParticipants();
//loadDataTeamA();
//loadDataTeamB();
}

public void remove(View view){
myRef.child(matchId).child("Participants")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if(snapshot.getKey().equals(id)){
for(Participant p: participants){
if(p.getUserId().equals(id)){
participants.remove(p);
}
}
snapshot.getRef().setValue(null);
}
}
participantAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("failed", "here");
}

});
}

public void add(View view){
getUser();
}


public void loadParticipants(){
myRef.child(matchId).child("Participants")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Participant x = new Participant(snapshot.getKey(),snapshot.getValue().toString());
participants.add(x);
}
participantAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("failed", "here");
}

});
}

public void participantExist(final String id){
myRef.child("Participants").child("matchId")
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if(snapshot.getKey().equals(id)){
exists = true;
}
}
participantAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("failed", "here");
}

});
}


public void getUser() {
user.child(id).addListenerForSingleValueEvent(new ValueEventListener() {

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getKey().equals(id)) {
username = dataSnapshot.child("username").getValue().toString();
myRef.child(matchId).child("Participants").child(id).setValue(username);
Participant x = new Participant(id,username);
participants.add(x);
Log.d("arrqwe",String.valueOf(participants.size()));
}
if(participants.size() > 0){
title1.setVisibility(View.VISIBLE);
}
participantAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("failed", "here");
}

});

}
}

然后在我的 customAdapter 上我有这个:

public class Participants extends RecyclerView.Adapter<Participants.ViewHolder> {


private ArrayList<Participant> participants;
private Context context;
private View cv;

public Participants(Context context, ArrayList<Participant> participants) {
this.participants = participants;
this.context = context;
}

@Override
public Participants.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.username_list, viewGroup, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(final Participants.ViewHolder viewHolder, final int i) {
Log.d("participante",participants.get(i).getUsername());
viewHolder.name.setText(participants.get(i).getUsername());

}

@Override
public int getItemCount() {
if (participants != null) {
return participants.size();
} else {
return 0;
}
}

public class ViewHolder extends RecyclerView.ViewHolder{
private TextView name;
public ViewHolder(View view) {
super(view);
cv = view;
name = (TextView)view.findViewById(R.id.username);
}
}

}

我的单品行

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

<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

最后是我的 Activity 布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.esmad.pdm.friendlymanager.MatchInvitation">

<android.support.v7.widget.RecyclerView
android:id="@+id/teamA"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintRight_toLeftOf="@+id/teamB"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
android:layout_marginStart="7dp"
app:layout_constraintBottom_toBottomOf="@+id/teamB"
android:layout_marginEnd="13dp"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/teamB">

</android.support.v7.widget.RecyclerView>

<android.support.v7.widget.RecyclerView
android:id="@+id/teamB"
android:layout_width="174dp"
android:layout_height="0dp"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="11dp"
app:layout_constraintTop_toBottomOf="@+id/textView4"
android:layout_marginBottom="15dp">

</android.support.v7.widget.RecyclerView>

<android.support.v7.widget.RecyclerView
android:id="@+id/participants"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="34dp"
android:layout_marginEnd="17dp"
android:layout_marginStart="17dp"
android:layout_marginTop="34dp"
app:layout_constraintBottom_toBottomOf="@+id/textView5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button6"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="participants"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:visibility="invisible"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
android:layout_marginTop="6dp"
app:layout_constraintTop_toBottomOf="@+id/button5"
app:layout_constraintLeft_toLeftOf="@+id/button5" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:text="TeamB"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:visibility="invisible"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
android:layout_marginEnd="65dp"
app:layout_constraintRight_toRightOf="@+id/participants"
android:layout_marginTop="17dp"
app:layout_constraintTop_toBottomOf="@+id/participants"
app:layout_constraintLeft_toLeftOf="@+id/teamB" />

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="61dp"
android:layout_marginStart="58dp"
android:text="TeamA"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:visibility="invisible"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintRight_toRightOf="@+id/teamA"
android:layout_marginTop="288dp"
app:layout_constraintLeft_toLeftOf="@+id/participants"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.487" />

<ImageButton
android:id="@+id/button5"
android:layout_width="44dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:onClick="add"
android:src="@drawable/ic_check_black_24dp"
android:tint="@color/emerald"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintRight_toLeftOf="@+id/button6"
tools:layout_constraintRight_creator="1"
app:layout_constraintBottom_toBottomOf="@+id/button6"
android:layout_marginEnd="15dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/button6" />

<ImageButton
android:id="@+id/button6"
style="@android:style/Widget.DeviceDefault.ImageButton"
android:layout_width="44dp"
android:layout_height="0dp"
android:layout_marginEnd="75dp"
android:layout_marginStart="75dp"
android:onClick="remove"
android:src="@drawable/ic_clear_black_24dp"
android:tint="@color/red"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintRight_toLeftOf="@+id/teamB"
app:layout_constraintBottom_toBottomOf="@+id/button7"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/button7" />

<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginEnd="137dp"
android:layout_marginStart="137dp"
android:layout_marginTop="32dp"
android:text="best teams"
android:visibility="invisible"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent" />

<Button
android:id="@+id/button8"
android:layout_width="88dp"
android:layout_height="48dp"
android:text="random"
android:visibility="invisible"
app:layout_constraintBaseline_toBaselineOf="@+id/button7"
tools:layout_constraintBaseline_creator="1"
tools:layout_constraintLeft_creator="1"
android:layout_marginStart="11dp"
app:layout_constraintLeft_toRightOf="@+id/button7" />
</android.support.constraint.ConstraintLayout>

最佳答案

在您的单个项目行中,将 LinearLayout android:layout_height 设置为 wrap_contentandroid:layout_height="match_parent" 导致每个项目占据整个屏幕。

关于android - 回收 View 仅显示 1 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45268370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com