gpt4 book ai didi

java - Firebase 回收适配器,带有使用 searchview 的过滤器

转载 作者:行者123 更新时间:2023-12-01 18:24:37 25 4
gpt4 key购买 nike

我在 firebase 数据库中有一个项目列表,它使用 firebaserecycleradapter 在 Recyclerview 中列出。效果很好。我还有一个搜索 View 来根据搜索查询过滤列表。但适配器不显示过滤列表。当我在搜索 View 中键入查询时,回收器 View 变为空白。任何人都可以突出显示我的编码中的错误并纠正我以获得所需的结果。我在下面给出了编码。

public class HomeActivity extends AppCompatActivity {

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("Projects");
}

@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) {
pbar.setVisibility(View.GONE);
String proj_key = getRef(position).getKey();
holder.prname.setText(model.getPname());
holder.prlocation.setText(model.getPlocation());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent detactivity = new Intent(HomeActivity.this, InfoActivity.class);
detactivity.putExtra("ProjID", proj_key);
startActivity(detactivity);
}
});
}

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
final MenuItem search = menu.findItem(R.id.search);
final SearchView searchView = (SearchView) search.getActionView();
searchView.setQueryHint("Search Project");
if (searchView != null) {
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
fbsearch(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
fbsearch(newText);
return false;
}
});
}
return true;
}

private void fbsearch (String searchText){
String pquery = searchText.toLowerCase();
Query sQuery = mDatabase.orderByChild("pname").equalTo(pquery);
FirebaseRecyclerOptions<project> soptions = new FirebaseRecyclerOptions.Builder<project>()
.setQuery(sQuery, project.class).build();
FBRA = new FirebaseRecyclerAdapter<project, ProjViewHolder>(soptions) {
@Override
protected void onBindViewHolder(@NonNull ProjViewHolder holder, int position, @NonNull project model) {
pbar.setVisibility(View.GONE);
String proj_key = getRef(position).getKey();
holder.prname.setText(model.getPname());
holder.prlocation.setText(model.getPlocation());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent detactivity = new Intent(HomeActivity.this, InfoActivity.class);
detactivity.putExtra("ProjID", proj_key);
startActivity(detactivity);
}
});
}

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

PList.setAdapter(FBRA);
FBRA.startListening();
}

public class ProjViewHolder extends RecyclerView.ViewHolder {
TextView prname, prlocation, prservice, prvalue, practual;
public ProjViewHolder(@NonNull View itemView) {
super(itemView);
prname = itemView.findViewById(R.id.FPName);
prlocation = itemView.findViewById(R.id.FPLocation);
}
}
@Override
protected void onStop() {
super.onStop();
FBRA.stopListening();
}
}

最佳答案

是的,我能够识别代码中的问题。由于查询区分大小写,因此未显示搜索列表。我创建了一个新字段来存储小写的项目名称。然后我对新字段进行查询。更正后的代码如下。

  private void fbsearch (String searchText){
String pquery = searchText.toLowerCase();
Query sQuery = mDatabase.orderByChild("pnamelowcase").startAt(pquery).endAt(pquery + "\uf8ff");
FirebaseRecyclerOptions<project> options = new FirebaseRecyclerOptions.Builder<project>()
.setQuery(sQuery, project.class).setLifecycleOwner(this).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.prname.setText(model.getPname());
holder.prlocation.setText(model.getPlocation());
holder.prservice.setText(model.getPservice());
holder.prvalue.setText(model.getPvalue());
holder.practual.setText(model.getPactual());

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent detactivity = new Intent(HomeActivity.this, InfoActivity.class);
detactivity.putExtra("ProjID", proj_key);
startActivity(detactivity);
}
});
}

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

}
};
PList.setAdapter(FBRA);
}

我注意到,查询仅适用于startAtendAt,如果我更改为任何其他查询,例如equalTo em>,这不起作用。

我还注意到一个问题,即搜索 View 关闭后不会显示正常列表。如果有人在我的代码中发现问题,请突出显示它并告诉我为什么其他查询不起作用以及如何在搜索 View 关闭后返回原始列表。

关于java - Firebase 回收适配器,带有使用 searchview 的过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60246241/

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