getView ListView selected item color change

491 views Asked by At

I am looking to set the selected item in getView(), is working however every item in my list is selected. I have tested with toasts and the correct is displayed so the condition is working. The condition checks to see if an entry from a DB for the specific item is set to true (thus being selected).

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
    if(isItemSelected.equals("true")){
          listviewTitles.setBackgroundColor(0xAAAAFFFF);
     }
     else if (isItemSelected.equals("false")){
     // Default color    
     }
}
4

There are 4 answers

0
oyenigun On

You should update your background colors every conditions like below;

 listviewTitles.setBackgroundColor(isItemSelected.equals("true") ? selectedColor : unSelectedColor);
0
Apoorv Mehrotra On

Try this and make these changes in your code hope it works out.

   @Override
 public View getView(int position, View convertView, ViewGroup parent) {  
        if (convertView == null) {    
        if (isItemSelected.equals("true")){
listviewTitles.setBackgroundColor(0xAAAAFFFF);
          }
          else if (isItemSelected.equals("false")){
            // Default color    
          }
            }else{

         if (isItemSelected.equals("true")){

    listviewTitles.setBackgroundColor(0xAAAAFFFF);
           }
          else if (isItemSelected.equals("false")){
             // Default color    
          } 
        }

please try this

1
Bethan On

I believe I have a solution for your requirement,

Paste this code in your view file

SparseBooleanArray singleChecked;

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        if (position != singleListSelectedPosition) {
            singleListSelectedPosition = position;
            int totalCount = lvSingleSelect.getCount();
            for (int i = 0; i < totalCount; i++) {
                if (i == position) {
                    boolean stat = singleChecked.get(position, false);
                    singleChecked.put(position, !stat);
                } else {
                    singleChecked.put(i, true);
                }
            }
             adapter.setChecked(singleChecked);
        }
    }

And this is your adapter class code:

public void setChecked(SparseBooleanArray ch) {
        singleChecked = ch;
        notifyDataSetChanged();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (singleChecked.get(position, false)) {
        convertView.setBackgroundColor(getResources()
                    .getColor(R.color.titlebar_background_color));
    } else {
        convertView.setBackgroundColor(
                    getResources().getColor(R.color.emphasis_color));
    }

Please let me know if you have any trouble with this, always happy to help.

0
priyanka kataria On

I think you should try adding an extra value like boolean while adding data in your arraylist. true for selected and false for not selected. Initially add false with all. And then when you click on listviewTitles

int positionClicked;

listviewTitles.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           for (int i = 0; i < bean_List.size(); i++) {
           if (i == position) {
                positionClicked = position;
                bean_List.get(position).setIsClicked(true);
                notifyDataSetChanged();
                //Do your task here...
           } else {
                bean_List.get(i).setIsClicked(false);
                notifyDataSetChanged();
           }
     }
});

And in getView() use this:-

    if (bean_List.get(position).getIsClicked() == true) {
        listviewTitles.setBackgroundColor(0xAAAAFFFF);
        //change color accordingly
    } else {
        listviewTitles.setBackgroundColor(0xAAAAFFFF);
        //change color accordingly
    }