Search code examples
androiddatabaselistviewuser-interfacesimplecursoradapter

How to change background color of list items based on their values?


I have a list of items in a database, which have values of either 0,1 or 2. I have used a simplecursor adapter and an activity which has a listview for the display. Now I want to change the background color of each item to reflect its value i.e I want the background of items with value of 0 to be green, those with value of 1 to be yellow and those with value of 2 to be red. How do i go about this? My adapter and activity are currently having black background.


Solution

  • You need to create a XML layout file for single list item for list view. then create a adapter class. in which inflate that xml view. and inside that adpter in getView() method check for your logic.

    here's my adpter class getView()..

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ViewHolder holder;
          if (convertView == null) {
    
           convertView = mInflater.inflate(R.layout.home_list_item, null);
           holder = new ViewHolder();
    
           holder.mTxtContactName = (TextView) convertView.findViewById(R.id.txtContactName);
           holder.mTxtContactNumber = (TextView) convertView.findViewById(R.id.txtContactNumber);
           holder.mTxtContactAdd = (TextView) convertView.findViewById(R.id.txtContactAddress);
           holder.mImgCall = (ImageButton) convertView.findViewById(R.id.imgCall);
           holder.mImgSms = (ImageButton) convertView.findViewById(R.id.imgSms);
    
           convertView.setTag(holder);
          } else {
           holder = (ViewHolder) convertView.getTag();
          }
    
            holder.mTxtContactName.setText(list.get(position).getContactName());
            holder.mTxtContactNumber.setText(list.get(position).getContactNumber());
            holder.mTxtContactAdd.setText(list.get(position).getContactAdd());
    
            holder.mImgSms.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Log.d("con", "SMS");
                    Intent smsIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+list.get(position).getContactNumber())); 
                    context.startActivity(smsIntent);
                }
            });
    
            holder.mImgCall.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel://"+list.get(position).getContactNumber())); 
                    context.startActivity(callIntent);
                }
            });
    
    
         return convertView;
         }
    
         static class ViewHolder {
          TextView mTxtContactName;
          TextView mTxtContactNumber;
          TextView mTxtContactAdd;
          ImageButton mImgCall;
          ImageButton mImgSms;
         }