Home » Java » java – Search View in list of data Recycle View Android-Exceptionshub

java – Search View in list of data Recycle View Android-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to implement a search by date option in my data, but I dont know how to do. My array list contains multiple data:

The picture is for understanding what kind of data i am showing

My Constructors Class

package com.example.marquee;

public class DataStore {

    public DataStore() {
    }

    private String cname;
    private String date;
    private String time;
    private Long foodrate;
    private Long advance;
    private String remaingbalance;
    private String person;
    private String cookingType;
    private String fooddescrip;
    private String rate;
    private  String soundbill;
    private  String flobill;
    private String Tax;
    private String eventsuc;
    private String Phone;
    private String cnic;
    private String discount;
    private String fitotal;
    private String totalamount;

    public String getDiscount() {
        return discount;
    }
    public void setDiscount(String discount) {
        this.discount = discount;
    }
    public String getFitotal() {
        return fitotal;
    }
    public void setFitotal(String fitotal) {
        this.fitotal = fitotal;
    }
    public String getSoundbill() {
        return soundbill;
    }
    public void setSoundbill(String soundbill) {
        this.soundbill = soundbill;
    }
    public String getFlobill() {
        return flobill;
    }
    public void setFlobill(String flobill) {
        this.flobill = flobill;
    }
    public String getCnic() {
        return cnic;
    }
    public void setCnic(String cnic) {
        this.cnic = cnic;
    }


    public DataStore(String cname, String date, String time, Long foodrate, Long advance, String remaingbalance, String person, String cookingType, String fooddescrip, String rate, String tax, String eventsuc, String totalamount) {
        this.cname = cname;
        this.date = date;
        this.time = time;
        this.foodrate = foodrate;
        this.advance = advance;
        this.remaingbalance = remaingbalance;
        this.person = person;
        this.cookingType = cookingType;
        this.fooddescrip = fooddescrip;
        this.rate = rate;
        Tax = tax;
        this.eventsuc = eventsuc;
        this.totalamount = totalamount;
    }

    public void setPhone(String phone) {Phone = phone;}
    public String getPhone() {return Phone; }
    public String getEventsuc() {return eventsuc; }
    public void setEventsuc(String eventsuc) { this.eventsuc = eventsuc; }
    public String getTotalamount() { return totalamount; }
    public void setTotalamount(String totalamount) { this.totalamount = totalamount; }
    public String getRate() {
        return rate;
    }
    public void setRate(String rate) {
        this.rate = rate;
    }
    public String getTax() { return Tax; }
    public void setTax(String tax) {
        Tax = tax;
    }
    public String getPerson() {
        return person;
    }
    public void setPerson(String person) {
        this.person = person;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public Long getFoodrate() { return foodrate; }
    public void setFoodrate(Long foodrate) {
        this.foodrate = foodrate;
    }
    public Long getAdvance() {
        return advance;
    }
    public void setAdvance(Long advance) {
        this.advance = advance;
    }
    public String getRemaingbalance() {
        return remaingbalance;
    }
    public void setRemaingbalance(String remaingbalance) {
        this.remaingbalance = remaingbalance;
    }
    public String getCname() {
        return cname;
    }
    public void setCname(String cname) {
        this.cname = cname;
    }
    public String getTime() {
        return time;
    }
    public void setTime(String time) {
        this.time = time;
    }
    public String getCookingType() {
        return cookingType;
    }
    public void setCookingType(String cookingType) {
        this.cookingType = cookingType;
    }
    public String getFooddescrip() {
        return fooddescrip;
    }
    public void setFooddescrip(String fooddescrip) {
        this.fooddescrip = fooddescrip;
    }


}

My Adapter Code

public class RecycleView_Config {

    private Context mContext;
    private DataAdapter mcustomeradapter;




    public void setconfig(RecyclerView recyclerView,Context context,List<DataStore> dataStores,List<String>keys)
    {
       mContext=context;
       mcustomeradapter=new DataAdapter(dataStores,keys);
       recyclerView.setLayoutManager(new LinearLayoutManager(context));
       recyclerView.setAdapter(mcustomeradapter);
    }

    class cdataitemview extends RecyclerView.ViewHolder
    {

        public TextView CustomerNameread,bdateread,bookingtimeread
                ,foodrate,flower,sound,person,tax,cooktyperead,fooddescriread, eventsuc,advancread,Balanceread,totlamu,rate,event,phno,cnicc,disco,ftotl;
        private String key;
        public cdataitemview(ViewGroup parent)
        {
            super(LayoutInflater.from(mContext).inflate(R.layout.rowdata,parent,false));

            CustomerNameread=itemView.findViewById(R.id.CustomerNameread);
            bdateread=itemView.findViewById(R.id.bdateread);
            bookingtimeread=itemView.findViewById(R.id.bookingtimeread);
            cooktyperead=itemView.findViewById(R.id.cooktyperead);
            fooddescriread=itemView.findViewById(R.id.fooddescriread);
            person=itemView.findViewById(R.id.perstex);
            foodrate=itemView.findViewById(R.id.foodrate);
            advancread=itemView.findViewById(R.id.advancread);
            Balanceread=itemView.findViewById(R.id.Balanceread);
            tax=itemView.findViewById(R.id.textex);
            totlamu=itemView.findViewById(R.id.totaltex);
            rate=itemView.findViewById(R.id.ratetext);
            event=itemView.findViewById(R.id.Event);
            phno=itemView.findViewById(R.id.Customerphno);
            cnicc=itemView.findViewById(R.id.cnicedt);
            disco=itemView.findViewById(R.id.discad);
            ftotl=itemView.findViewById(R.id.finaltad);
            flower=itemView.findViewById(R.id.flowerread);
            sound=itemView.findViewById(R.id.soundread);




            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(mContext, updatedele.class);
                    intent.putExtra("key",key);

                    intent.putExtra("CustomerName",CustomerNameread.getText().toString());
                    intent.putExtra("Eventdate",bdateread.getText().toString());
                    intent.putExtra("phone",phno.getText().toString());
                    intent.putExtra("cnic",cnicc.getText().toString());
                    intent.putExtra("eventtime",bookingtimeread.getText().toString());
                    intent.putExtra("persons",person.getText().toString());
                    intent.putExtra("foodfrom",cooktyperead.getText().toString());
                    intent.putExtra("fooddescription",fooddescriread.getText().toString());
                    intent.putExtra("foodrate",foodrate.getText().toString());
                    intent.putExtra("taxes",tax.getText().toString());
                    intent.putExtra("totalamount",totlamu.getText().toString());
                    intent.putExtra("advance",advancread.getText().toString());
                    intent.putExtra("balance",Balanceread.getText().toString());
                    intent.putExtra("rateperhead",rate.getText().toString());
                    intent.putExtra("Remainblc",Balanceread.getText().toString());
                    intent.putExtra("discount",disco.getText().toString());
                    intent.putExtra("finaltotal",totlamu.getText().toString());
                    intent.putExtra("soundb",sound.getText().toString());
                    intent.putExtra("flowerb",flower.getText().toString());
                    intent.putExtra("Eventsucess",event.getText().toString());

                    mContext.startActivity(intent);

                }


            });
        }

        public void bind( DataStore dataStore,String key)
        {
            CustomerNameread.setText(dataStore.getCname());
            bdateread.setText(dataStore.getDate());
            bookingtimeread.setText(dataStore.getTime());
            foodrate.setText(dataStore.getFoodrate().toString());
            person.setText(dataStore.getPerson());
            tax.setText(dataStore.getTax());
            advancread.setText(dataStore.getAdvance().toString());
            Balanceread.setText(dataStore.getRemaingbalance());
            totlamu.setText(dataStore.getTotalamount());
            cooktyperead.setText(dataStore.getCookingType());
            fooddescriread.setText(dataStore.getFooddescrip());
            rate.setText(dataStore.getRate());
            Balanceread.setText(dataStore.getRemaingbalance());
            event.setText(dataStore.getEventsuc());
            phno.setText(dataStore.getPhone());
            cnicc.setText(dataStore.getCnic());
            ftotl.setText(dataStore.getFitotal());
            disco.setText(dataStore.getDiscount());
            flower.setText(dataStore.getFlobill());
            sound.setText(dataStore.getSoundbill());
            this.key=key;

        }
    }

    class DataAdapter extends RecyclerView.Adapter<cdataitemview> {
        private List<DataStore> mcustomerlist;
        private List<String> mkeys;

        public DataAdapter(List<DataStore> mcustomerlist, List<String> mkeys) {
            this.mcustomerlist = mcustomerlist;
            this.mkeys = mkeys;
        }

        @NonNull
        @Override
        public cdataitemview onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new cdataitemview(parent);
        }

        @Override
        public void onBindViewHolder(@NonNull cdataitemview holder, int position) {
            holder.bind(mcustomerlist.get(position), mkeys.get(position));

        }

        @Override
        public int getItemCount() {
            return mcustomerlist.size();
        }


    }
}

My Main class Code

public class Events extends AppCompatActivity {

    private RecyclerView rcv;
    private List<DataStore> mcustomerlist;
    private ProgressDialog Dialog;
    private RecycleView_Config adapter;
    private TextView emptyView;
    EditText editTextSearch;
    DataStore data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_events);
        rcv = (RecyclerView) findViewById(R.id.recyclewiev);
        emptyView = (TextView)findViewById(R.id.empty_view);
        editTextSearch = (EditText) findViewById(R.id.editTextSearch)

        Dialog = new ProgressDialog(Events.this,R.style.MyAlertDialogStyle);
        Dialog.setMessage("Wait Loading Events...");
        Dialog.show();
        Dialog.setCanceledOnTouchOutside(false);

        new FirebaseDatebasehelper().readCdata(new FirebaseDatebasehelper.DataStatus()
        {
            @Override
            public void DataisLoaded(List<DataStore> dataStores, List<String> keys) {

                new RecycleView_Config().setconfig(rcv, Events.this, dataStores, keys);
                Dialog.hide();
                Dialog.setCanceledOnTouchOutside(true);

                if(dataStores.isEmpty())
                {
                    rcv.setVisibility(View.GONE);
                    emptyView.setVisibility(View.VISIBLE);
                }

                else {
                    rcv.setVisibility(View.VISIBLE);
                    emptyView.setVisibility(View.GONE);
                }

            }

            @Override
            public void DataisInserted() {

            }

            @Override
            public void DataisUpdated() {

            }

            @Override
            public void DataisDeleted() {

            }
        });

    }
How to&Answers:

Store your data in ArrayList<ModelClass> then you can filter result of this arrayList

Your global var

ArrayList<CustomerData> dataList = new ArrayList<>();
ArrayList<CustomerData> filteredList = new ArrayList<>();

assuming you have received data in your dataList now you can add TextWatcher() in your EditText

searchET.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            filteredList = indiaList.stream().filter(
                article -> article.getDate()
                                  .contains(s.toString()))
                                  .collect(Collectors.toList());
            adapter = new DataAdapter(filteredIndiaList);
            recyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

I m using JAVA 8, and this method only works at Android N +

You can modify this

Hope this will help!