我的Listview可以正常工作,但是我正在使用SearchView进行搜索以在listview中查找项目。当我在列表视图中过滤没有问题,但是当我在列表视图中单击时,那会给出错误的位置。我不明白为什么这样做。你有什么想法吗?
ArrayList<String> StokIDFromParse;
ArrayList<String> StokKoduFromParse;
ArrayList<String> StokAdiFromParse;
ArrayList<String> StokFiyatiFromParse;
ArrayList<String> UrunArama;
ArrayList<Boolean> KullanimYeriFromParse;
StokDatas stokDatas;
ArrayAdapter adapter;
final Context context = this;
EditText stokID,stokKodu,stokAdi,fiyati;
SearchView urunArama;
RadioButton petrolCihazlari,yikamaEkipmanlari;
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_stok_olusturr);
stokID=findViewById(R.id.stokOlusturTextStokID);
stokKodu = findViewById(R.id.stokOlusturTextStokKodu);
stokAdi = findViewById(R.id.stokOlusturTextUrunAdi);
fiyati = findViewById(R.id.stokOlusturTextUrunFiyati);
petrolCihazlari=findViewById(R.id.stokOlusturRadioPetrolCihazlari);
yikamaEkipmanlari=findViewById(R.id.stokOlusturRadioYikamaEkipmanlari);
urunArama = findViewById(R.id.stokOlusturTextUrunArama);
listView = findViewById(R.id.stokOlusturmaListStokListesi);
UrunArama = new ArrayList<>();
StokIDFromParse = new ArrayList<>();
StokKoduFromParse = new ArrayList<>();
StokAdiFromParse = new ArrayList<>();
StokFiyatiFromParse = new ArrayList<>();
KullanimYeriFromParse = new ArrayList<>();
stokDatas = new StokDatas(StokIDFromParse,StokKoduFromParse,StokAdiFromParse,StokFiyatiFromParse, this);
adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,StokAdiFromParse);
listView.setAdapter(stokDatas);
VeriGetir();
urunArama.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
if(!urunArama.toString().equals("")){
listView.setAdapter(adapter);
adapter.getFilter().filter(s);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
stokKodu.setEnabled(false);
stokID.setText(StokIDFromParse.get(i));
stokKodu.setText(StokKoduFromParse.get(i));
stokAdi.setText(StokAdiFromParse.get(i));
fiyati.setText(StokFiyatiFromParse.get(i));
if (KullanimYeriFromParse.set(i,true)){
petrolCihazlari.setChecked(true);
}
else if(KullanimYeriFromParse.set(i,false)) {
yikamaEkipmanlari.setChecked(true);
}
}
});
}
if(urunArama.toString().equals("")) {
listView.setAdapter(stokDatas);
stokDatas.notifyDataSetChanged();
}
return false;
}
});
// ListView Onclick侦听器
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
stokKodu.setEnabled(false);
stokID.setText(StokIDFromParse.get(i));
stokKodu.setText(StokKoduFromParse.get(i));
stokAdi.setText(StokAdiFromParse.get(i));
fiyati.setText(StokFiyatiFromParse.get(i));
if (KullanimYeriFromParse.set(i,true)){
petrolCihazlari.setChecked(true);
}
else if(KullanimYeriFromParse.set(i,false)) {
yikamaEkipmanlari.setChecked(true);
}
}
});
}
//接收数据
public void VeriGetir(){
ParseQuery<ParseObject> query = ParseQuery.getQuery("tableParcalar");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e!=null){
Toast.makeText(getApplicationContext(),e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
}
else {
if (objects.size()>0)
{
for(final ParseObject object : objects){
StokIDFromParse.add(object.getObjectId());
StokAdiFromParse.add(object.getString("StokAdi"));
StokKoduFromParse.add(object.getString("StokKodu"));
StokFiyatiFromParse.add(object.getString("Fiyati"));
KullanimYeriFromParse.add(object.getBoolean("KullanimAlani"));
stokDatas.notifyDataSetChanged();
adapter.notifyDataSetChanged();
}
}
}
}
});
}
编辑1:
我已经尝试过了,但是我不能帮忙我的代码吗?
This is new Screenshot on my try
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
textlenght=urunArama.toString().indexOf(textlenght);
for (int j =0; j < StokAdiFromParse.size(); j++){
if (textlenght==StokAdiFromParse.get(j).length()){
if(urunArama.toString().equalsIgnoreCase((String)StokAdiFromParse.get(i).subSequence(0,textlenght))){
int z = j;
StokID_SUB.add(StokIDFromParse.get(j));
StokName_SUB.add(StokAdiFromParse.get(j));
StokKodu_SUB.add(StokKoduFromParse.get(j));
StokFiyati_SUB.add(StokFiyatiFromParse.get(j));
KullanimYeri_SUB.add(KullanimYeriFromParse.get(j));
}
}
}
stokKodu.setEnabled(false);
stokID.setText(String.valueOf(StokID_SUB));
stokKodu.setText(String.valueOf(StokKodu_SUB));
stokAdi.setText(String.valueOf(StokName_SUB));
fiyati.setText(String.valueOf(StokFiyati_SUB));
}
});
}
答案 0 :(得分:0)
我遇到了同样的问题,解决方案here很有帮助。
主要问题是点击监听器正在与原始列表进行交互,但与已过滤的列表进行交互。 因此,问题在于,如果已过滤元素位于第三位置,但位于过滤列表的第一位置,则单击将与原始(未过滤)列表中第一位置的元素进行交互。
希望这种解释足以理解这个想法。