gpt4 book ai didi

android - 使用 Firestore 时如何为 RecyclerView 添加搜索过滤器?

转载 作者:行者123 更新时间:2023-11-29 01:04:22 26 4
gpt4 key购买 nike

所以,我正在尝试创建显示作者列表(图片、姓名、简历)的应用程序,但问题是我在实现搜索功能时遇到了问题。我使用的 RecyclerView 使用指南是 here .

代码:MainActivity

public class MainActivity extends AppCompatActivity {

private FirebaseFirestore db;
private FirestoreRecyclerAdapter adapter;
private LinearLayoutManager linearLayoutManager;//nodrosina kada veida recycle attelos
private List<Klasiki> list;

@BindView(R.id.imageRecycleView)
RecyclerView klasikiList; //tas pats kas find by ID

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

ButterKnife.bind(this);
linearLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false);
klasikiList.setLayoutManager(linearLayoutManager);
db = FirebaseFirestore.getInstance();
getList();


}

//atgriez ierakstus no datubazes un nodrosina atteloshanu recycle view
private void getList(){
Query query = db.collection("klasiki");
FirestoreRecyclerOptions<Klasiki> response = new FirestoreRecyclerOptions.Builder<Klasiki>()
.setQuery(query, Klasiki.class)
.build();

adapter = new FirestoreRecyclerAdapter<Klasiki, klasikiHolder>(response) {
@Override
protected void onBindViewHolder(final klasikiHolder holder, int position, final Klasiki model) {
holder.name.setText(model.getName());
holder.shortBio.setText(model.getShortBio());
Glide.with(getApplicationContext()).load(model.getImage()).into(holder.image);


holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, BioActivity.class);

//Parnes mainigos uz bio activity
Bundle extras = new Bundle();
extras.putString("imageInfo", model.getImage());
extras.putString("nameInfo", model.getName());
extras.putString("bioInfo", model.getFullBio());
intent.putExtras(extras);
startActivity(intent);

}
});

}

@Override
public klasikiHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.klasiki_layout, parent, false);
return new klasikiHolder(view);
} //nodrosina jauna ieraksta returnu

@Override
public void onError(FirebaseFirestoreException e) {
Log.e("error", e.getMessage());
}

};

adapter.notifyDataSetChanged();
klasikiList.setAdapter(adapter);

}

//izsauc mainigos
public class klasikiHolder extends RecyclerView.ViewHolder{
@BindView(R.id.name)
TextView name;
@BindView(R.id.klasikiImage)
ImageView image;
@BindView(R.id.shortBio)
TextView shortBio;


public klasikiHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);

}
}

@Override
protected void onStart() {
super.onStart();
adapter.startListening();

}

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

构造函数

public class Klasiki {
private String Name;
private String Image;
private String shortBio;
private String fullBio;

public Klasiki(){}

public Klasiki(String name, String image, String shortBio, String fullBio) {
Name = name;
Image = image;
this.shortBio = shortBio;
this.fullBio = fullBio;
}

public String getName() {
return Name;
}

public void setName(String name) {
Name = name;
}

public String getImage() {
return Image;
}

public void setImage(String image) {
Image = image;
}

public String getShortBio() {
return shortBio;
}

public void setShortBio(String shortBio) {
this.shortBio = shortBio;
}

public String getFullBio() {
return fullBio;
}

public void setFullBio(String fullBio) {
this.fullBio = fullBio;
}

我想我必须使用其他 RecyclerView 而不是 FirestoreRecyclerOptions 但我不确定,对 android 编程来说真的很新。 :)

更新:所以我做了这样的事情:

    @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_menu, menu);
// Retrieve the SearchView and plug it into SearchManager
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
querySearch = db.collection("klasiki").whereEqualTo("name", text);
if (!text.trim().isEmpty()){
getList(querySearch);
adapter.startListening();
}
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
if (newText.trim().isEmpty()){
getList(queryFinal);
adapter.startListening();
}
return false;
}
});

return true;
}

可能不是最好的解决方案,但现在我遇到了不同的问题。有没有办法为 Firestore 进行子字符串搜索?像 db.collection("klasiki").whereEqualTo("name", Ja); 它显示以 Ja 开头的名称。

最佳答案

为了搜索作者,您需要创建一个查询

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query nameOfTheWritterQuery = rootRef.collection("writters")
.whereEqualTo("writterName", "Eidemanis Roberts");

看到你的Klasiki课,我要告诉你,你不尊重Java Naming Conventions所有字段的规则,反序列化时可能会遇到问题。您的模型类应如下所示:

public class Klasiki {
private String name, image, shortBio, fullBio;

public Klasiki( ){}

public Klasiki(String name, String image, String shortBio, String fullBio) {
this.name = name;
this.image = image;
this.shortBio = shortBio;
this.fullBio = fullBio;
}

public String getName() {return name;}
public void setName(String name) {this.name = name;}

public String getImage() {return image;}
public void setImage(String image) {this.image = image;}

public String getShortBio() {return shortBio;}
public void setShortBio(String shortBio) {this.shortBio = shortBio;}

public String getFullBio() {return fullBio;}
public void setFullBio(String fullBio) {this.fullBio = fullBio;}
}

请现在查看字段。一切开始都会小写。

关于android - 使用 Firestore 时如何为 RecyclerView 添加搜索过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48433687/

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