它应该显示的数据和过滤器时,我搜索,但它没有显示任何数据。我以为我写了一些相当不错的代码,但我找不到我的错误。
我有一个模型班,有职位、公司、经验、年龄、薪水和类型。我制作了一个单独的布局xml文件。以下是firebase实时数据库结构。这是模型课。
下面是java代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_showjobs);
ref = FirebaseDatabase.getInstance().getReference("jobs");
searchbtn = findViewById(R.id.searchjobbtn);
searchtxt = findViewById(R.id.searchjobtxt);
result = (RecyclerView) findViewById(R.id.results);
result.setHasFixedSize(true);
result.setLayoutManager(new LinearLayoutManager(this));
searchbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String searchedText = searchtxt.getText().toString();
firebasesearch(searchedText);
}
});
}
private void firebasesearch(String searchtxt) {
Query firebaseSearchQuery=ref.orderByChild("jobTitle").startAt(searchtxt).endAt(searchtxt+"\uf8ff");
FirebaseRecyclerOptions<jobclass> options = new FirebaseRecyclerOptions.Builder<jobclass>().setQuery(firebaseSearchQuery, jobclass.class).build();
FirebaseRecyclerAdapter<jobclass, jobviewholder> recycleradaptor = new FirebaseRecyclerAdapter<jobclass, jobviewholder>(options) {
@Override
protected void onBindViewHolder(@NonNull jobviewholder holder, int position, @NonNull jobclass model) {
holder.setDetails(model.getJobTitle(), model.getCompany(), model.getPay(), model.getType(), model.getExperience(), model.getAge());
}
@NonNull
@Override
public jobviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.listlayout, parent, false);
jobviewholder jv = new jobviewholder(v);
return jv;
}
};
result.setAdapter(recycleradaptor);
Toast.makeText(showjobs.this, "Searching", Toast.LENGTH_LONG).show();
}
/* @Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<jobclass> options = new FirebaseRecyclerOptions.Builder<jobclass>().setQuery(ref, jobclass.class).build();
Toast.makeText(showjobs.this, "Searching", Toast.LENGTH_LONG).show();
FirebaseRecyclerAdapter<jobclass, jobviewholder> recycleradaptor = new FirebaseRecyclerAdapter<jobclass, jobviewholder>(options) {
@Override
protected void onBindViewHolder(@NonNull jobviewholder holder, int position, @NonNull jobclass model) {
holder.setDetails(model.getJobTitle(), model.getCompany(), model.getPay(), model.getType(), model.getExperience(), model.getAge());
}
@NonNull
@Override
public jobviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.listlayout, parent, false);
jobviewholder jv = new jobviewholder(v);
return jv;
}
};
result.setAdapter(recycleradaptor);
}*/
public class jobviewholder extends RecyclerView.ViewHolder {
View v;
public jobviewholder(View itemView){
super(itemView);
v=itemView;
}
public void setDetails(String jobn, String companyn, String pay, String Ag, String xreq, String jtyp){
TextView jobtitle=findViewById(R.id.jname);
TextView companyname=findViewById(R.id.cname);
TextView payment=findViewById(R.id.sal);
TextView age=findViewById(R.id.agereq);
TextView exprequired=findViewById(R.id.reqex);
TextView jobtype=findViewById(R.id.type);
jobtitle.setText(jobn);
companyname.setText(companyn);
payment.setText(pay);
age.setText(Ag);
exprequired.setText(xreq);
jobtype.setText(jtyp);
}
}
}
谢谢你抽出时间。我希望有人能帮我找到问题。
2条答案
按热度按时间vsikbqxv1#
为了这个
recycleradaptor
你也得开始听。你得在家里听onStart()
所以你可以jutyujz02#
这是使用firebase数据库进行搜索的工作代码`