我正在使用本教程enter link description here来搜索自定义ListView for SQLite 但我的搜索工作并不奏效 我做了所有的事情并复制了一些代码。
我做错了什么?
本教程适用于使用活动,我在片段中使用它。
可能是因为这个吗?
我的CustomAdapter
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);
}
@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 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;
clear();
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;
}
我的Pojo
public class Urun {
int id;
String barkodNo,adi;
double alis,satis;
public Urun(){
}
public Urun(int id, String barkodNo, String adi, double alis, double satis) {
this.id = id;
this.barkodNo = barkodNo;
this.adi = adi;
this.alis = alis;
this.satis = satis;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBarkodNo() {
return barkodNo;
}
public void setBarkodNo(String barkodNo) {
this.barkodNo = barkodNo;
}
public String getAdi() {
return adi;
}
public void setAdi(String adi) {
this.adi = adi;
}
public double getAlis() {
return alis;
}
public void setAlis(double alis) {
this.alis = alis;
}
public double getSatis() {
return satis;
}
public void setSatis(double satis) {
this.satis = satis;
}
UrunListesi.fragment
public class UrunListesi extends Fragment implements Tanimla {
ListView liste;
View v;
Helper helper;
SQLiteDatabase db;
UrunAdapter adapter;
Cursor c;
int id;
String barkod,adi;
double alis,satis;
Urun bilgi;
TextView t0;
String s0;
EditText barkodaGore,adaGore;
ArrayList<Urun> list;
public UrunListesi() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v=inflater.inflate(R.layout.fragment_diger, container, false);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
init();
//listeYukle();
}
private void listeYukle() {
helper = new Helper(getActivity());
db = helper.getWritableDatabase();
String query = "Select * from tblUrunler";
c = db.rawQuery(query, null);
while(c.moveToNext()){
id=c.getInt(c.getColumnIndex("id_urunler"));
barkod=c.getString(c.getColumnIndex("urun_barkodNo"));
adi=c.getString(c.getColumnIndex("urun_adi"));
alis=c.getDouble(c.getColumnIndex("urun_alis"));
satis=c.getDouble(c.getColumnIndex("urun_satis"));
bilgi = new Urun();
bilgi.setId(id);
bilgi.setBarkodNo(barkod);
bilgi.setAdi(adi);
bilgi.setAlis(alis);
bilgi.setSatis(satis);
list.add(bilgi);
adapter.add(bilgi);
}
liste.setAdapter(adapter);
}
@Override
public void init() {
liste=v.findViewById(R.id.liste);
list=new ArrayList<>();
adapter = new UrunAdapter(getActivity(), R.layout.satir3,list);
barkodaGore=v.findViewById(R.id.barkodaGore);
adaGore=v.findViewById(R.id.adaGore);
liste.isTextFilterEnabled();
barkodaGore.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if ((keyEvent.getAction() == KeyEvent.ACTION_DOWN) &&
(i == KeyEvent.KEYCODE_ENTER)) {
adapter.getFilter().filter(barkodaGore.getText().toString());
adapter.guncelle();
return true;
}
return false;
}
});
adaGore.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
adapter.getFilter().filter(charSequence);
adapter.guncelle();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
liste.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
t0=view.findViewById(R.id.textView26);
s0=t0.getText().toString();
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Material_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(getActivity());
}
builder.setTitle("Ne Yapmak istersiniz?")
.setMessage("Seçiniz")
.setPositiveButton("Sil", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
sil();
adapter.clear();
listeYukle();
adapter.guncelle();
}
})
.setNegativeButton("Silme", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
return true;
}
});
}
private void sil() {
helper=new Helper(getActivity());
db=helper.getWritableDatabase();
db.delete("tblUrunler","id_urunler="+Integer.parseInt(s0),null);
}
@Override
public void onResume() {
super.onResume();
listeYukle();
}