简体   繁体   中英

How to fix 'Incorrect Onclick' with ListView in Android Studio

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

            }
        });



    }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM