我做了一个应用程序,其中RecyclerView
我在卡片视图中显示患者姓名。我正在从火力基地的数据库中检索患者姓名。我需要向我的回收商视图添加一个搜索选项,以便我可以按患者姓名搜索所需的卡。
我需要帮助,因为我不知道该怎么做,而且我找不到任何关于此的教程。以下是我的CardView
课:
public class MedicalData extends AppCompatActivity {
private RecyclerView patientdata;
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_medical_data);
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#009688")));
setTitle("Patient Directory");
mDatabase = FirebaseDatabase.getInstance().getReference().child("Patients");
patientdata = (RecyclerView) findViewById(R.id.patientdata);
patientdata.setHasFixedSize(true);
patientdata.setLayoutManager(new LinearLayoutManager(this));
}
@Override
public void onStart() {
super.onStart();
final FirebaseRecyclerAdapter<PatientRequest, DataViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<PatientRequest, DataViewHolder>(
PatientRequest.class,
R.layout.datatype,
DataViewHolder.class,
mDatabase
) {
@Override
public void populateViewHolder(DataViewHolder viewHolder, PatientRequest model, final int position) {
final String getid = getRef(position).getKey();
viewHolder.setName(model.getPatientName());
viewHolder.setAge(model.getPatientAge());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MedicalData.this, getid, Toast.LENGTH_SHORT).show();
Intent int1 = new Intent(MedicalData.this, ShowPatientDetails.class);
int1.putExtra("name7", getid);
startActivity(int1);
}
});
}
};
patientdata.setAdapter(firebaseRecyclerAdapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public static class DataViewHolder extends RecyclerView.ViewHolder {
View mView;
public DataViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name) {
TextView pname = (TextView) mView.findViewById(R.id.pname1);
pname.setText(name);
}
public void setAge(String age) {
TextView page = (TextView) mView.findViewById(R.id.page1);
page.setText(age);
}
}
}
如果你想从加载的数据中搜索,可以
创建自己的Recylerview及其适配器怎么样
假设您有这样的查询
mDatabase.child("TripSummry_CM").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getChildrenCount() != 0)
{
for (DataSnapshot child : dataSnapshot.getChildren()) {
YOURMODEL yourmodel = child.getValue(YOURMODEL.class);
YOURMODELList.add(yourmodel);}
Adapter.notifyDataSetChanged();
}
}
其中 YOURMODELList 是 A 列表和
适配器是您自己的适配器
当然,您有自己的回收器视图
现在,您只需在对象列表中搜索即可
使用此功能进行搜索
public List<Doctor_UI_model> findDoctors(List<Doctor_UI_model> categories, String name)
{
List<Doctor_UI_model> models = new ArrayList<>();
for(Doctor_UI_model cat : categories) //assume categories isn't null.
{
if(cat.getName().contains(name))
//assumes name isn't null.
{
models.add(cat);
}
}
return models;
}
并在您的搜索按钮或服务视图中
List<Doctor_UI_model> search_result = findDoctors(Doc_List_Server_All_data, newText);
if (search_result.size() != 0) {
YourList.clear();
YourList.addAll(search_result);
Adapter.notifyDataSetChanged();
}
注意力
最好将加载的数据保留在列表中,然后将其添加到另一个列表中用于适配器以及当您在加载的数据列表中开始搜索时清除另一个并在其中添加过滤的数据