根据内容/变量更改listitem背景

时间:2016-04-14 17:43:02

标签: android listview

我有一个带有自定义适配器的列表,并希望根据列表项或变量的内容设置背景颜色。 该列表包含带有日期和一些信息的文本视图。

我需要在显示新日期时更改背景颜色..

第1项:2。2016年3月 - >蓝色

第2项:2。2016年3月 - >蓝色

第3项:3。2016年3月 - >灰色

item4:4。2016年3月 - >蓝色

...

我可以设置一个可以在listAdapter中使用的变量(isNewDate)吗?

我的列表适配器:

    public class HistoryListViewAdapter extends ArrayAdapter<HistoryListItem> {

        public HistoryListViewAdapter(Context context, List<HistoryListItem> items) {
            super(context, R.layout.history_list_item, items);
        }

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

            if(convertView == null) {
                // inflate the ListView item layout
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(R.layout.history_list_item, parent, false);
                //different color if new date

                if (newdate == 0) //How to find if it is a "new date"
                    //togle color

                convertView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark));

                // initialize the view holder
                viewHolder = new ViewHolder();
                viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
                viewHolder.tvTextfield = (TextView) convertView.findViewById(R.id.tvTextfield);
                viewHolder.tvNote = (ImageView) convertView.findViewById(R.id.tvNote);
                convertView.setTag(viewHolder);
            } else {
                // recycle the already inflated view
                viewHolder = (ViewHolder) convertView.getTag();
            }

            // update the item view
            HistoryListItem item = getItem(position);
            viewHolder.ivIcon.setImageDrawable(item.icon);
            viewHolder.tvTextfield.setText(item.textfield);
            viewHolder.tvNote.setImageDrawable(item.note);

            return convertView;
        }

        private static class ViewHolder {
            ImageView ivIcon;
            TextView tvTextfield;
            ImageView tvNote;
        }
    }

1 个答案:

答案 0 :(得分:0)

您需要在 HistoryListItem 模型类中添加varible来存储日期。 然后您的适配器类将如下所示。

 public class HistoryListViewAdapter extends ArrayAdapter<HistoryListItem> {

//Varible to STORE current Date value
String previousDate = null ;

        public HistoryListViewAdapter(Context context, List<HistoryListItem> items) {
            super(context, R.layout.history_list_item, items);
        }

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

            if(convertView == null) {
                // inflate the ListView item layout
                LayoutInflater inflater = LayoutInflater.from(getContext());
                convertView = inflater.inflate(R.layout.history_list_item, parent, false);
                //different color if new date

                //This is how you How to find if it is a "new date"
                if(null== previousDate || !item.dateField.equalIgnorecase(previousDate))
{
//Store current date as previous date
previousDate = item.dateField ;

 //TODO add togle color logic here
}


                convertView.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark));

                // initialize the view holder
                viewHolder = new ViewHolder();
                viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
                viewHolder.tvTextfield = (TextView) convertView.findViewById(R.id.tvTextfield);
                viewHolder.tvNote = (ImageView) convertView.findViewById(R.id.tvNote);
                convertView.setTag(viewHolder);
            } else {
                // recycle the already inflated view
                viewHolder = (ViewHolder) convertView.getTag();
            }

            // update the item view
            HistoryListItem item = getItem(position);
            viewHolder.ivIcon.setImageDrawable(item.icon);
            viewHolder.tvTextfield.setText(item.textfield);
            viewHolder.tvNote.setImageDrawable(item.note);

            return convertView;
        }

        private static class ViewHolder {
            ImageView ivIcon;
            TextView tvTextfield;
            ImageView tvNote;
        }
    }