1

I am using an ArrayAdapter

ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(this,
                android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);

The problem is when I select first item from listview it select another item automatically.

enter image description here

I didn't select item 12

enter image description here

All code

final ListView listView1 = (ListView) findViewById(R.id.listView);

Product[] items = {
        new Product(1, "Milk", 21.50),
        new Product(2, "Butter", 15.99),
        new Product(3, "Yogurt", 14.90),
        new Product(4, "Toothpaste", 7.99),
        new Product(5, "Ice Cream", 10.00),
        new Product(6, "Milk", 21.50),
        new Product(7, "Butter", 15.99),
        new Product(8, "Yogurt", 14.90),
        new Product(9, "Toothpaste", 7.99),
        new Product(10, "Ice Cream", 10.00),
        new Product(11, "Milk", 21.50),
        new Product(12, "Butter", 15.99),
        new Product(13, "Yogurt", 14.90),
        new Product(14, "Toothpaste", 7.99),
        new Product(15, "Ice Cream", 10.00),
        new Product(16, "Milk", 21.50),
        new Product(17, "Butter", 15.99),
        new Product(18, "Yogurt", 14.90),
        new Product(19, "Toothpaste", 7.99),
        new Product(20, "Ice Cream", 10.00),
};

ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(this,
            android.R.layout.simple_list_item_1, items);

listView1.setAdapter(adapter);

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

         String item = ((TextView) view).getText().toString();
         view.setSelected(true);
         listView1.setItemChecked(position, true);
         view.setBackgroundColor(Color.GREEN);
      }
});
antoniom
  • 3,143
  • 1
  • 37
  • 53
Recep Yesil
  • 148
  • 13

3 Answers3

1

It is because your adapter reuses the inflated views.

One solution is to override your ArrayAdapter's getView() method in order to properly render your views and (even better) implement a ViewHolder so that your don't loose any performance by inflating again and again!

antoniom
  • 3,143
  • 1
  • 37
  • 53
  • Well, remove the `relativeLayout.setOnClickListener` .... code inside the `getView`. Keep the `listView1.setOnItemClickListener()` of your original post. Everytime `setOnItemClickListener` is called keep in an array of booleans the click state. When your `getView` is called check the array of booleans an properly render RED or GREEN – antoniom Feb 01 '16 at 10:30
  • 1
    That's for sure that your recycled view is not being reset. When you click the first item, this view will also be used for the 11th item as well. that's why your 11th item is also green. – Mustansar Saeed Feb 01 '16 at 10:33
  • @MustansarSaeed Totally agree – antoniom Feb 01 '16 at 10:35
  • @Mustansar Saeed absolutely – Recep Yesil Feb 01 '16 at 10:36
1

In CustomArrayAdapter shared at link http://paste.ubuntu.com/14848762/ , you are changing color of relativeLayout on Click Listener, when you scroll or view is recycled, you should reset this layout and should enable color for the item you clicked. You can for example use this approach.

relativeLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int color;
                    if (tt8.getText().toString().equals("1")){
                        color = Color.RED;
                        relativeLayout.setBackgroundColor(color);
                    }else {
                        color = Color.GREEN;                        
                        relativeLayout.setBackgroundColor(color);
                    }
                    p.setColor(color);

                }
            });

relativeLayout.setBackgroundColor(p.getColor());

Hope this helps.

Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46
-1

Try to make this change

listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {

            String item = listView1.getItemAtPosition(position);
            Log.d("position", position+", item");


        }
    });

Update the value you got in logcat in the comment below so that i can suggest a better code.

Mohammed Atif
  • 4,383
  • 7
  • 28
  • 57
  • The problem of OP is different. Issue is related to the view recycling and resetting. – Mustansar Saeed Feb 01 '16 at 10:34
  • But your View view will provide the reference to the current item selected, if the problem was not of the output, even then it must not jump to unexpected index. I feel the problem is with setting the adapter for the listview – Mohammed Atif Feb 01 '16 at 10:40
  • Its not jumping to the unexpected index. The thing is when it selects the 1st item, this view is being used for the 11th item as well, that's why 2 items are being show green. This recycling can be managed by implementing custom arrayadapter and getView – Mustansar Saeed Feb 01 '16 at 10:43
  • when i do some up and down in listview mix items for example selected items are no longer selected ...... – Recep Yesil Feb 01 '16 at 10:44
  • Why dont you you prefer item selected listener – Mohammed Atif Feb 01 '16 at 10:49