简体   繁体   English

单击时更改listview项目的背景颜色 - 并记住它

[英]Changing background colour of listview item on click - and remembering it

I am populating a listview with strings from an ArrayList. 我用来自ArrayList的字符串填充listview。 When a listview item is clicked i would like to change the background colour to green. 当单击列表视图项时,我想将背景色更改为绿色。 <- Issue number one because I cannot change the item which is clicked. < - 问题一,因为我无法更改被点击的项目。

After an item is clicked, I am adding its index to the list of items the user has selected, when the listview is first loaded I need it to set the background colour of all the listview items which have been selected to green too. 单击一个项目后,我将其索引添加到用户选择的项目列表中,当首次加载listview时,我需要它将所有被选中的listview项目的背景色也设置为绿色。 <- Issue number 2 - I have been trying to do this with a for loop but do not know how to refer to a specific item in the listview to set the background colour! < - 问题2 - 我一直试图用for循环来做这个但不知道如何引用listview中的特定项来设置背景颜色!

Essentially, i think if someone can help me in how to change the colour of a selected listview item, i should be able to do the same thing but in a loop for all the userFoodPref which are saved? 本质上,我认为如果有人可以帮助我如何更改所选列表视图项目的颜色,我应该能够做同样的事情,但是循环保存的所有userFoodPref是?

    animalsNameList = new ArrayList<String>();
    userFoodPref = new ArrayList<Integer>();
    getUserSelection();
    getAnimalNames();
    // Create The Adapter with passing ArrayList as 3rd parameter
    ArrayAdapter<String> arrayAdapter =
            new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, animalsNameList);
    // Set The Adapter
    animalList.setAdapter(arrayAdapter);




    // register onClickListener to handle click events on each item
    animalList.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        // argument position gives the index of item which is clicked
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            if(userFoodPref.contains(i)
            ){
                userFoodPref.remove(i);}
                else {
                userFoodPref.add(i);
                View item = animalList.getSelectedView();
                item.setBackgroundColor(0x0000FF00);
            }

            String selectedAnimal=animalsNameList.get(i);
            Toast.makeText(getApplicationContext(), "Animal Selected : "+selectedAnimal,   Toast.LENGTH_LONG).show();
        }
    });
}

If I understand well, the problem is that when you set the backround of the item and then by example scroll the list and comeback to the previous position, it doens't remember that the backround is green for this specifix item. 如果我很好理解,问题是当您设置项目的背景,然后通过示例滚动列表并返回到上一个位置时,它不记得此特定项目的背景为绿色。

I have faced this problem and to solve it easily : 我遇到了这个问题并且很容易解决:

Create a list a string for your name and a boolean (true = green, false = not green) and create an adapter for it and simply add if (list.get(position).getBoolean) { Currentitem.setBackgroundColor(0x0000FF00)} 为您的名称创建一个字符串字符串和一个布尔值(true =绿色,false =不绿色),并为其创建适配器,然后简单地添加if(list.get(position).getBoolean){Currentitem.setBackgroundColor(0x0000FF00)}

And when you click on a item simply set the boolean of the item position to true and call notifydatasetchanged() 当你点击一个项目时,只需将项目位置的布尔值设置为true,并调用notifydatasetchanged()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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