我应该在自定义适配器或活动中更改ListView行的背景

时间:2014-02-19 02:01:52

标签: android android-layout android-listview

我有一个ListView,我使用自定义适配器填充。我想检查ListView布局中的条目,如果匹配,则将整行的背景颜色更改为不同的颜色。

是否建议在自定义适配器本身或主要活动中进行更改?

我目前正在更改自定义适配器中的背景颜色,并且由于某种原因,多行的背景颜色正在发生变化,如果我向上和向下滚动ListView行,背景颜色也会切换到其他行。< / p>

如果需要,我可以将代码发布到我的自定义适配器。

我的CustomAdapter类:

package com.test.testing;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class SetRowsCustomAdapter extends ArrayAdapter<SetRows> {
    Context context;
   int layoutResourceId;
   ArrayList<SetRows> data=new ArrayList<SetRows>();
   DateFormat df = new SimpleDateFormat("EEEEE, LLLL d", Locale.US);
   String[] suspendedDates = {
            "Tuesday, January 1",
            "Monday, January 20",
            "Friday, January 31",
            "Wednesday, February 12",
            "Monday, February 17",
            "Wednesday, March 5",
            "Sunday, March 16",
            "Tuesday, April 15",
            "Wednesday, April 16",
            "Thursday, April 17",
            "Friday, April 18",
            "Monday, April 21",
            "Tuesday, April 22",
            "Monday, May 26",
            "Thursday, May 29",
            "Wednesday, June 4",
            "Thursday, June 5",
            "Friday, July 4",
            "Monday, July 28",
            "Tuesday, July 29",
            "Wednesday, July 30",
            "Friday, August 15",
            "Monday, September 1",
            "Thursday, September 25",
            "Friday, September 26",
            "Saturday, October 4",
            "Sunday, October 5",
            "Monday, October 6",
            "Thursday, October 9",
            "Friday, October 10",
            "Monday, October 13",
            "Thursday, October 16",
            "Friday, October 17",
            "Thursday, October 23",
            "Saturday, November 1",
            "Tuesday, November 4",
            "Tuesday, November 11",
            "Thursday, November 27",
            "Monday, December 8",
            "Thursday, December 25"
    };
   public SetRowsCustomAdapter(Context context, int layoutResourceId, ArrayList<SetRows> data) {
       super(context, layoutResourceId, data);
       this.layoutResourceId = layoutResourceId;
       this.context = context;
       this.data = data;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
       View row = convertView;
       ImageHolder holder = null;

       if(row == null)
       {
           LayoutInflater inflater = ((Activity)context).getLayoutInflater();
           row = inflater.inflate(layoutResourceId, parent, false);

           holder = new ImageHolder();
           holder.txtTitle = (TextView)row.findViewById(R.id.tvDateVal);
           holder.txtDate = (TextView)row.findViewById(R.id.tvDateNum);
           holder.txtID = (TextView)row.findViewById(R.id.tvReasonVal);
           row.setTag(holder);
       }
       else
       {
           holder = (ImageHolder)row.getTag();
       }

       SetRows myImage = data.get(position);
       int inReason = myImage.name.indexOf(",");
       String strR = myImage.name.substring(0, inReason);
       Spannable WordToSpan = new SpannableString(strR);
       WordToSpan.setSpan(new ForegroundColorSpan(Color.parseColor("#4787ED")), 0, WordToSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
       String strRNext = myImage.name.substring(inReason, myImage.name.length());
       Spannable WordToSpan1 = new SpannableString(strRNext);
       WordToSpan1.setSpan(new ForegroundColorSpan(R.color.dateholiday), 0, WordToSpan1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

       holder.txtTitle.setText(TextUtils.concat(WordToSpan, WordToSpan1));
       holder.txtID.setText(myImage.id);
       holder.txtDate.setText(myImage.date);
       if (myImage.name.contains(df.format(Calendar.getInstance(Locale.US).getTime()))) {
           Toast.makeText(getContext(), String.valueOf(myImage.name.contains(df.format(Calendar.getInstance(Locale.US).getTime()))), 2000).show();
           row.setBackgroundColor(Color.parseColor("#F0DEEDF3"));
       }
       return row;
   }

   static class ImageHolder
   {
       TextView txtTitle;
       TextView txtID;
       TextView txtDate;
   }
}

正如您所看到的,我正在使用IF条件,如果满足该条件,则只允许一行具有背景颜色。

图片:http://i59.tinypic.com/2wmhtht.png

图片:http://i62.tinypic.com/9i7pjn.png

2 个答案:

答案 0 :(得分:1)

如果您打算更改单个行,我会通过适配器类调整颜色。否则,请根据需要使用“活动/片段”更改整个背景。

答案 1 :(得分:1)

Multiple changes occur because u probably do this in ur code. if(condition){// set bg}.

    // you need to do
    if(condition){// set bg}
    else
    {
    // set bg to default
    }

if (myImage.name.contains(df.format(Calendar.getInstance(Locale.US).getTime()))) {
           Toast.makeText(getContext(), String.valueOf(myImage.name.contains(df.format(Calendar.getInstance(Locale.US).getTime()))), 2000).show();
           row.setBackgroundColor(Color.parseColor("#F0DEEDF3"));
       }
else
{
row.setBackgroundColor(Color.parseColor("#ur original color hex code here"));
}