如何引用firebase recycler适配器中的子记录中的firebase子项以在recyclerview中显示

brjng4g3  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(306)

我已经编写了代码,列出联系人在回收视图。但它不会在回收器视图中显示值。代码如下。
家庭活动.java

private DatabaseReference mDatabase;
private RecyclerView PList;
private FirebaseRecyclerAdapter<project, ProjViewHolder> FBRA;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
PList = findViewById(R.id.ProjList);
PList.setHasFixedSize(true);
PList.setLayoutManager(new LinearLayoutManager(this));
mDatabase = FirebaseDatabase.getInstance().getReference().child("Contacts");
}

@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<project> options = new FirebaseRecyclerOptions.Builder<project>().setQuery(mDatabase, project.class).build();

FBRA = new FirebaseRecyclerAdapter<project, ProjViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ProjViewHolder holder, int position, @NonNull project model) {
String proj_key = getRef(position).getKey();
holder.cpname.setText(model.getCname());
holder.cplocation.setText(model.getCcity());
holder.ccname.setText(model.getPcontact());
holder.ccdesig.setText(model.getPdesig());
}

@Override
public ProjViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.proj_row, parent, false);
return new ProjViewHolder(view);
}
};
PList.setAdapter(FBRA);
FBRA.startListening();
}

public class ProjViewHolder extends RecyclerView.ViewHolder {
TextView cpname, cplocation, ccname, ccdesig;
ProjViewHolder(@NonNull View itemView) {
super(itemView);
cpname = itemView.findViewById(R.id.FCName);
cplocation = itemView.findViewById(R.id.FCLocation);
ccname = itemView.findViewById(R.id.FPName);
ccdesig = itemView.findViewById(R.id.FPDesig);
}
}

@Override
protected void onStop() {
super.onStop();
FBRA.stopListening();
}

formactivity.java格式

public class FormActivity extends AppCompatActivity {

private EditText CName, PContact,PDesig, CCity;
private FirebaseDatabase mDatabase;
private Button Fbtn;

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

CName = findViewById(R.id.CompName);
PContact = findViewById(R.id.PContact);
PDesig = findViewById(R.id.PDesig);
CCity = findViewById(R.id.CCity);

mDatabase = FirebaseDatabase.getInstance();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.save, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {

String dCName = CName.getText().toString().trim();
String dPContact = PContact.getText().toString().trim();
String dPDesig = PDesig.getText().toString().trim();
String dCCity = CCity.getText().toString().trim();

String name = CName.getText().toString().trim();
DatabaseReference mDbRef = mDatabase.getReference().child("Contacts").child(name).push();
mDbRef.child("cname").setValue(dCName);
mDbRef.child("pcontact").setValue(dPContact);
mDbRef.child("pdesig").setValue(dPDesig);
mDbRef.child("ccity").setValue(dCCity);
Toast.makeText(FormActivity.this, "Contact Submitted Successfully", Toast.LENGTH_LONG).show();
Intent intent = new Intent(FormActivity.this, HomeActivity.class);
startActivity(intent);
finish();
}
}
return super.onOptionsItemSelected(item);
}
}

项目类

public class project {

   String cname, pcontact, pdesig, ccity;

   public project(){

   }

    public project(String cname, String pcontact, String pdesig, String ccity) {
        this.cname = cname;
        this.pcontact = pcontact;
        this.pdesig = pdesig;
        this.ccity = ccity;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

   public String getPcontact() {
        return pcontact;
    }

    public void setPcontact(String pcontact) {
        this.pcontact = pcontact;
    }

    public String getPdesig() {
        return pdesig;
    }

    public void setPdesig(String pdesig) {
        this.pdesig = pdesig;
    }

    public String getCcity() {
        return ccity;
    }

    public void setCcity(String ccity) {
        this.ccity = ccity;
    }

    }

家庭活动.xml

<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:orientation="vertical"
    tools:context=".HomeActivity">

<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:id="@+id/ProjList"
/>
</RelativeLayout>

projrow.xml文件

<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="2dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>

<TextView
android:id="@+id/FCName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:elegantTextHeight="true"
android:textColor="@color/colorBlue"
android:maxLength="25"
android:paddingTop="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="16dp"
android:textStyle="bold"
android:layout_marginTop="10dp"
/>
<TextView
android:id="@+id/FCLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_alignParentRight="true"
android:textAllCaps="true"
android:elegantTextHeight="true"
android:textColor="@color/colorDark"
android:layout_marginTop="13dp"
android:paddingTop="5dp"
android:textSize="14dp"
/>
<TextView
android:id="@+id/FPName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/FCName"
android:layout_alignParentLeft="true"
android:elegantTextHeight="true"
android:textStyle="bold"
android:textColor="@color/colorText"
android:paddingTop="5dp"
android:paddingRight="3dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textSize="15dp"
/>
<TextView
android:id="@+id/FPDesig"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/FCName"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="5dp"
android:padding="5dp"
android:textSize="14dp"
android:textColor="@color/colorNext"
android:elegantTextHeight="true"
/>
</RelativeLayout>

</androidx.cardview.widget.CardView>

代码运行良好,没有错误。但并没有从数据库中获取预期的值。如果 push() 已从中删除 DatabaseReference mDbRef = mDatabase.getReference().child("Contacts").child(name).push(); ,效果非常好。但它会覆盖现有数据,同时为相同的“名称”添加新数据。在添加push之后,数据被存储在子记录中。请指导我如何在recycleview适配器中引用和获取数据。
在添加之前,每个记录都存储在“联系人”下 push() 但在为同一公司添加人员时会覆盖。

添加后 push() 每个记录都存储在公司名称下,并具有唯一的密钥。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题