Custom Listew Filtreleme(SQLite)
18.10.2017 - 03:19
merhaba,
android'de custom listview içindeki verilerin filtrelenmesi ile ilgili bir tutorial buldum. onu kullandım. Kodunu birebir kullandım.Onunki çalışmış benimki çalışmadı. sizden ricam vereceğim linkteki sayfadaki kodlarla benim kodları karşılaştırmanız. Aradaki fark ne, ben neyi yanlış yapıyorum?
ilgili link:https://androidjson.com/android-add-search-box-filter-sqlite/
benim kodlarım:
public class UrunAdapter extends ArrayAdapter <Urun>{
private Context c;
private ArrayList<Urun> originalList;
private ArrayList<Urun> filteredList;
private UrunAdapter.SubjectDataFilter myFilter;
public UrunAdapter(@NonNull Context context,int textViewResourceId,ArrayList<Urun> urun) {
super(context, textViewResourceId);
this.c=context;
this.originalList=new ArrayList<>();
this.originalList.addAll(urun);
this.filteredList=new ArrayList<>();
this.filteredList.addAll(urun);
}
@Override
public void add(@Nullable Urun object) {
super.add(object);
filteredList.add(object);
}
@Override
public int getCount() {
return filteredList.size();
}
@Nullable
@Override
public Urun getItem(int position) {
return filteredList.get(position);
}
@NonNull
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v=layoutInflater.inflate(R.layout.satir3,parent,false);
TextView tv1=v.findViewById(R.id.textView26);
TextView tv2=v.findViewById(R.id.textView27);
TextView tv3=v.findViewById(R.id.textView28);
TextView tv4=v.findViewById(R.id.textView29);
TextView tv5=v.findViewById(R.id.textView30);
Urun provider= this.getItem(position);
if (provider != null) {
tv1.setText(String.valueOf(provider.getId()));
tv2.setText(provider.getBarkodNo());
tv3.setText(provider.getAdi());
tv4.setText(String.valueOf(provider.getAlis()));
tv5.setText(String.valueOf(provider.getSatis()));
tv1.setVisibility(View.INVISIBLE);
}
return v;
}
public void clear(){
filteredList.clear();
}
public void guncelle(){UrunAdapter.this.notifyDataSetChanged();}
public class SubjectDataFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
charSequence = charSequence.toString().toLowerCase();
FilterResults filterResults = new FilterResults();
if (charSequence.toString().length() > 0) {
ArrayList<Urun> arrayList1 = new ArrayList<>();
for (int i = 0, l = originalList.size(); i < l; i++) {
Urun subject = originalList.get(i);
if (subject.toString().toLowerCase().contains(charSequence))
arrayList1.add(subject);
}
filterResults.count = arrayList1.size();
filterResults.values = arrayList1;
} else {
synchronized (this) {
filterResults.values = originalList;
filterResults.count = originalList.size();
}
}
return filterResults;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
filteredList = (ArrayList<Urun>) filterResults.values;
notifyDataSetChanged();
for (int i = 0, l = filteredList.size(); i < l; i++)
add(filteredList.get(i));
notifyDataSetInvalidated();
}
}
@NonNull
@Override
public Filter getFilter() {
if (myFilter == null) {
myFilter = new UrunAdapter.SubjectDataFilter();
}
return myFilter;
}
}
101
Görüntülenme
0 Beğeni