0

My Listview works properly, but i am searching using a SearchView to look for an item within the listview. When I filtering in listview not have problem but when I onclick in listview thats give a false position. I dont understand why does that. Do you have any think?

above writed rulman is my object in my database. I searched that and I onclicked that. But my textbox readed other is my object in my database. You can look at this

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 Listener

    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);
            }
        }
    });
}

//receive data

 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();

                            }

                    }

                }
            }
        });
    }

Edit 1:

Im tryed this but I couldn't Can you help me with my code?

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));

            }
        });



    }
Hamit
  • 15
  • 5

1 Answers1

0

I had the same problem and solution here was helpful.

The main problem is that on click listener is interacting with the original list, but not with the filtered one. So the problem is, that if filtered element is in the third position, but first position in filtered list, the click will interact with the element on the first position in the original (unfiltered) list.

Hope that this explanation is enough to get the idea.

Blind Kai
  • 514
  • 5
  • 14
  • I understanding now. I'm directly try now. Thank you for help. God bless you – Hamit Aug 08 '19 at 09:23
  • @Hamit, sorry, read that wrong. If the problem is still actual I would be able to write a fast solution. – Blind Kai Aug 09 '19 at 11:52
  • Im added edit can you look this? the problem is still actual. – Hamit Aug 09 '19 at 11:56
  • @Hamit, I had the same problem, but I was using `RecyclerView` because it provides better functionality than list view. And then I simply was implementing `onClick` in `ViewHolder` and providing the logic I need via interface. Google it. For example: [link](https://stackoverflow.com/questions/28296708/get-clicked-item-and-its-position-in-recyclerview/28304517#28304517) – Blind Kai Aug 09 '19 at 12:03