我有一个片段,即ScheduledFragment,在该片段中,我有一个自定义的listview。正在从服务器获取值并将这些值插入列表项。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_scheduled, container, false);
final ArrayList<CoursePojo> searchResults = GetSearchResults();
final ListView lv1 = (ListView) rootView.findViewById(R.id.lvList2);
lv1.setAdapter(new MyCustomCourseList(getActivity(), searchResults));
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
String currentid = ids.get(position);
String currentcourse = cs.get(position);
CoursePojo fullObject = (CoursePojo) o;
}
});
return rootView;}
正在将值添加到数组列表中。MyCustomCourseList适配器类getView()如下所示
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
ViewHolder holder;
if (convertView == null) {
holder.relativeLayout=(RelativeLayout)convertView.findViewById(R.id.rel1);
else {
holder = (ViewHolder) convertView.getTag();
}
holder.relativeLayout.setBackgroundColor(convertView.getResources().getColor(R.color.textColorPrimary));
在这里为我的整个自定义列表设置颜色。但是我真正需要的是从服务器获取一些日期,我需要为某些日期更改列表项的背景颜色。在比较我从服务器获得的日期和当前日期之后,我需要更改那些列表项的颜色。当前日期之前的那些日期必须以背景色“主色”显示,当前日期之后的那些日期必须以“副色”显示。
在getView中进行一些日期比较,并根据该颜色为您的布局上色
if(validateDate(new SimpleDateFormat("MM-dd-yyyy", Locale.getDefault()).format(c.getTime()),rowItem.getApiDate))
holder.relativeLayout.setBackgroundColor(convertView.getResources().getColor(R.color.textColorPrimary));
else
holder.relativeLayout.setBackgroundColor(convertView.getResources().getColor(R.color.textColorSecondary));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句