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和適配器,我應該使用這些還是我需要製作一個新的? –