0

我已经提出了一个应用程序,在回收者视图中,我显示患者名称卡视图。我从火灾基地的数据库中检索患者姓名。我需要在我的回收商视图中添加一个搜索选项,以便我可以通过患者姓名搜索所需的卡。搜索选项与cardviews 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); 
     } 

    } 
} 
+0

你想从服务或加载数据搜索吗? – Elsunhoty

+0

从已加载的数据(已经可以看到卡片形式) –

回答

0

OK,如果你wanne从加载的数据

怎么样创建自己的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是列表 和

适配器是你自己的适配器

,并ofcourse你有自己的Recyclerview

现在简单的,你可以在你的对象

使用这种机能的研究列表搜索来搜索

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; 
} 

并在您的搜索按钮或serchview

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

注意

它`更好地保持你的加载的数据列表,然后将其添加到另一个 这是在适配器 使用,当你开始在加载数据列表中搜索 清除其他一个,并在其中添加过滤数据

+0

所以您要说我需要先将所有想要显示的名称保存在firebase列表中,然后将其与对象/卡片数据进行比较。对? –

+0

正确并在装载数据列表中搜索 – Elsunhoty

+0

我已经有我的recyclerview和适配器,我应该使用这些还是我需要制作一个新的? –