无法使用RecyclerView在Android Studio中显示联系人列表

jhkqcmku  于 2023-01-09  发布在  Android
关注(0)|答案(1)|浏览(183)

我正在尝试显示我的移动的应用程序的联系人列表。我已经连接了数据库,我认为数据库没有问题,因为我可以添加新的联系人,但我无法显示列表。我使用RecyclerView,我已经为它创建了适配器类、项目布局和模型。

  • 适配器类-contAdapter.java
  • 项目布局- item.xml
  • 型号-接触
  • 回收站视图布局-联系人列表. xml
  • contactList -将recyclerview与适配器绑定

我希望看到添加到数据库的联系人列表显示在屏幕上,但我已经在这里停留了相当长的一段时间,因为我不能弄清楚问题是什么:(
下面是我的联系方式:

public Contact() {

}

public Contact(String contName, String contNumber) {
this.contName = contName;
this.contNumber = contNumber;
}

public String getContName() {
return contName;
}

public void setContName(String contName) {
this.contName = contName;
}

public String getContNumber() {
return contNumber;
}

public void setContNumber(String contNumber) {
this.contNumber = contNumber;
}

contactList.xml

<?xml version="1.0" encoding="utf-8"?>

        <androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
        android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_margin="25dp" tools:context=".MainActivity">
    
        <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/contList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
        </androidx.constraintlayout.widget.ConstraintLayout>

contactList.java

package com.example.lucentproj;

    import androidx.annotation.NonNull; 
    import androidx.appcompat.app.AppCompatActivity; 
    import androidx.recyclerview.widget.LinearLayoutManager; 
    import androidx.recyclerview.widget.RecyclerView;
    import android.os.Bundle;
    import com.google.firebase.auth.FirebaseAuth; 
    import com.google.firebase.auth.FirebaseUser; 
    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 contactList extends AppCompatActivity {
    RecyclerView recyclerView;
    DatabaseReference reference;
    contAdapter adapter;
    ArrayList<Contact> list;
    FirebaseUser user;

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

    user = FirebaseAuth.getInstance().getCurrentUser();
    recyclerView = findViewById(R.id.contList);
    reference = FirebaseDatabase.getInstance().getReference("Contact");
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    list = new ArrayList<>();
    adapter = new contAdapter(this, list);
    recyclerView.setAdapter(adapter);

    reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {

    for (DataSnapshot dataSnapshot : snapshot.getChildren()){

    Contact contact = dataSnapshot.getValue(Contact.class);
    list.add(contact);

    }
    adapter.updateList(list);
  }

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

   }
});
}
}

item.xml

<?xml version="1.0" encoding="utf-8"?>

        <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="wrap_content"
        xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_margin="16dp"
        app:cardElevation="8dp" app:cardCornerRadius="8dp">
    
    
        <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:orientation="vertical"/>
    
        <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="horizontal" />
    
        <TextView
        android:id="@+id/contname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/amaranth"
        android:text="Name"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="8dp"
        android:textSize="20sp"
        android:textStyle="bold" />
    
        <TextView
        android:id="@+id/contno"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:fontFamily="@font/amaranth_italic"
        android:paddingLeft="10dp"
        android:text="Number"
        android:textSize="20dp"
        android:textStyle="italic" />
    
        <ImageView
        android:id="@+id/cCall"
        android:layout_width="38dp"
        android:layout_height="30dp"
        android:layout_marginLeft="280dp"
        android:layout_marginTop="15dp"
        android:clickable="true"
        android:src="@drawable/call" />
    
        <ImageView
        android:id="@+id/cEdit"
        android:layout_width="45dp"
        android:layout_height="30dp"
        android:layout_marginLeft="320dp"
        android:layout_marginTop="15dp"
        android:clickable="true"
        android:src="@drawable/edit" />
    
    </androidx.cardview.widget.CardView>

contAdapter

package com.example.lucentproj;

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; import android.widget.TextView; 
import androidx.annotation.NonNull; 
import androidx.recyclerview.widget.RecyclerView; 
import java.util.ArrayList;

public class contAdapter extends RecyclerView.Adapter<contAdapter.ViewHolder> {Context context;

    ArrayList<Contact> list;

public contAdapter(Context context, ArrayList<Contact> list) {
    this.context = context;
    this.list = list;
}

@NonNull
@Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(context).inflate(R.layout.item,parent,false);
    return new ViewHolder(v);
}

@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    Contact cont = list.get(position);
    holder.cname.setText(cont.getContName());
    holder.cnum.setText(cont.getContNumber());

}

@Override
    public int getItemCount() {
    return list.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {

    TextView cname, cnum;

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

        cname = itemView.findViewById(R.id.contName);
        cnum =  itemView.findViewById(R.id.contNumber);
    }
}
    public void updateList(ArrayList<Contact> list){
    this.list=list;
    notifyDataSetChanged();
}
}
klr1opcd

klr1opcd1#

    • 您没有更新适配器中的列表**
  • 请在适配器类中添加此方法-*
public void updateList(ArrayList<Contact> list){
   this.list=list;
   notifyDataSetChanged();
}

并将该行从您的contactList活动替换为**adapter. notifyDataSetChanged(); * *--〉适配器.更新列表(列表);

    • 联系人列表. java**
package com.example.lucentproj;

    import androidx.annotation.NonNull; 
    import androidx.appcompat.app.AppCompatActivity; 
    import androidx.recyclerview.widget.LinearLayoutManager; 
    import androidx.recyclerview.widget.RecyclerView;
    import android.os.Bundle;
    import com.google.firebase.auth.FirebaseAuth; 
    import com.google.firebase.auth.FirebaseUser; 
    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 contactList extends AppCompatActivity {
    RecyclerView recyclerView;
    DatabaseReference reference;
    contAdapter adapter;
    ArrayList<Contact> list;
    FirebaseUser user;

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

    user = FirebaseAuth.getInstance().getCurrentUser();
    recyclerView = findViewById(R.id.contList);
    reference = FirebaseDatabase.getInstance().getReference("Contact");
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    list = new ArrayList<>();

    myRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
         Contact contact = dataSnapshot.getValue(Contact.class);
         list.add(contact);
      }
    }

    adapter = new contAdapter(this, list);
    recyclerView.setAdapter(adapter);

    reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {

    for (DataSnapshot dataSnapshot : snapshot.getChildren()){

    Contact contact = dataSnapshot.getValue(Contact.class);
    list.add(contact);

    }
    adapter.updateList(list);
  }

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

   }
  });
 }
}

相关问题