简体   繁体   中英

getView ListView selected item color change

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    
     }
}

您应该按照以下所有条件更新背景色;

 listviewTitles.setBackgroundColor(isItemSelected.equals("true") ? selectedColor : unSelectedColor);

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

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.

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
    }

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