user3423728
user3423728

Reputation: 51

I select one but two items get selected in android

I have a list View and I listen to this. I checked one selection but another one gets automatically selected, so total two items get selected. One I selected and another one gets selected automatically. I want the item I clicked on to be selected only. How can I fix it?

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = mContext.getLayoutInflater().inflate(
                    R.layout.brandslayout, parent, false);

        TextView UrunAdi = (TextView) convertView
                .findViewById(R.id.BrandAdi);
        final TextView UrunId = (TextView) convertView
                .findViewById(R.id.BrandId);

        final CheckBox chkSelect = (CheckBox) convertView
                .findViewById(R.id.checkBox1);

        final EditText edtText = (EditText) convertView
                .findViewById(R.id.edtOran);

        Button btnSelect = (Button) convertView
                .findViewById(R.id.btnSeciliBrand);

        final TextView tvOran = (TextView) convertView
                .findViewById(R.id.tvRateIcon);

        UrunAdi.setText(((Brands)Markalar.get(position)).BrandName);
        UrunId.setText(((Brands)Markalar.get(position)).BrandId);
        chkSelect.setVisibility(((Brands)Markalar.get(position)).deger1);
        edtText.setVisibility(((Brands)Markalar.get(position)).deger2);
        btnSelect.setVisibility(((Brands)Markalar.get(position)).deger3);
        tvOran.setVisibility(((Brands)Markalar.get(position)).deger4);

        convertView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(YeniSahaActivity2.this, "Ürünler Aranıyor...", Toast.LENGTH_SHORT).show();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Spinner spnProductGroup = (Spinner)findViewById(R.id.spnBrandProduct);


                if(chkSelect.isChecked())
                {

                    ProductGroupId=UrunId.getText().toString();
                    RackPercentage= edtText.getText().toString();
                    ExistsOnRack = "1";
                    AddProducts(Integer.parseInt( spnProductGroup.getSelectedItem().toString().split("#")[1]),Integer.parseInt(UrunId.getText().toString()));
                    //Button btnSave = (Button) findViewById(R.id.btnMarkaGir);
                    //btnSave.setText("");

                }
                else
                {
                    ExistsOnRack = "0";
                    //Toast.makeText(YeniSahaActivity2.this, "Lütfen ürün grubu seçiniz...", Toast.LENGTH_SHORT).show();
                    Extras e = new Extras();
                    e.ViewAlert("Lütfen ürün grubu seçiniz...", YeniSahaActivity2.this);


                }
            }
        });

        return convertView;

    }
}

Upvotes: 0

Views: 77

Answers (3)

IAmGroot
IAmGroot

Reputation: 13855

You have to be careful, because listviews re-use elements, as you scroll. You need to set them checked/unchecked accordingly in the getview method. Else your checked one will re-show as you scroll, for an item that shouldnt be checked.

I assume your list by default starts off, as nothing being checked? As I see no code that sets any checkboxes.

You need a section that is basically setting/unsetting as the view is created/recycled. (next to setVisibility section.)

if(<Item should be checked>) 
   chkSelect.setChecked(true);
else 
   chkSelect.setChecked(false);

or in short:

chkSelect.setChecked(<Item should be checked>);

You would there for need a way, to know which items have been checked or not, Which i think your AddProducts method is doing? So check that the related item is in the product list.

Upvotes: 1

ravindra.kamble
ravindra.kamble

Reputation: 1023

I have modified your code. The clickListener need to registered when it is created first time.

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = mContext.getLayoutInflater().inflate(
                    R.layout.brandslayout, parent, false);
            convertView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(YeniSahaActivity2.this, "Ürünler Araniyor...", Toast.LENGTH_SHORT).show();
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Spinner spnProductGroup = (Spinner)findViewById(R.id.spnBrandProduct);


                if(chkSelect.isChecked())
                {

                    ProductGroupId=UrunId.getText().toString();
                    RackPercentage= edtText.getText().toString();
                    ExistsOnRack = "1";
                    AddProducts(Integer.parseInt( spnProductGroup.getSelectedItem().toString().split("#")[1]),Integer.parseInt(UrunId.getText().toString()));
                    //Button btnSave = (Button) findViewById(R.id.btnMarkaGir);
                    //btnSave.setText("");

                }
                else
                {
                    ExistsOnRack = "0";
                    //Toast.makeText(YeniSahaActivity2.this, "Lütfen ürün grubu seçiniz...", Toast.LENGTH_SHORT).show();
                    Extras e = new Extras();
                    e.ViewAlert("Lütfen ürün grubu seçiniz...", YeniSahaActivity2.this);


                }
            }
        });
        }
        TextView UrunAdi = (TextView) convertView
                .findViewById(R.id.BrandAdi);
        final TextView UrunId = (TextView) convertView
                .findViewById(R.id.BrandId);

        final CheckBox chkSelect = (CheckBox) convertView
                .findViewById(R.id.checkBox1);

        final EditText edtText = (EditText) convertView
                .findViewById(R.id.edtOran);

        Button btnSelect = (Button) convertView
                .findViewById(R.id.btnSeciliBrand);

        final TextView tvOran = (TextView) convertView
                .findViewById(R.id.tvRateIcon);

        UrunAdi.setText(((Brands)Markalar.get(position)).BrandName);
        UrunId.setText(((Brands)Markalar.get(position)).BrandId);
        chkSelect.setVisibility(((Brands)Markalar.get(position)).deger1);
        edtText.setVisibility(((Brands)Markalar.get(position)).deger2);
        btnSelect.setVisibility(((Brands)Markalar.get(position)).deger3);
        tvOran.setVisibility(((Brands)Markalar.get(position)).deger4);



        return convertView;

    }
}

Upvotes: 0

maddy d
maddy d

Reputation: 1536

Use view holder in your adapter for batter performance and it will also resolve some this kind of issues. see this.

Upvotes: 0

Related Questions