Reputation: 1060
I have a problem. My listView contains items which are divided in 2 groups:
if (datas.get(position).getStatus().equalsIgnoreCase("after")) {
viewHolder.postTimeView.setTextColor(getResources().getColor(R.color.text_not_highlighted));
viewHolder.postNameView.setTextColor(getResources().getColor(R.color.text_not_highlighted));
} else {
viewHolder.postTimeView.setTextColor(getResources().getColor(R.color.white));
viewHolder.postNameView.setTextColor(getResources().getColor(R.color.white));
}
It looks like:
the amount of "white" fields differs every time due to current time.
And the problem is I need to increase textSize
and make Bold
the LAST white field (see marked field).
How can I do that from custom adapter?
What I tried:
private int state = 0;
....
if (datas.get(position).getStatus().equalsIgnoreCase("after")) {
if (state == 0) {
//Make parent placeholder Bold here
parentHolder = (ViewHolder) parent.getTag();
parentHolder.postNameView.setTypeface(null, Typeface.BOLD);
parentHolder.postTimeView.setTypeface(null, Typeface.BOLD);
parentHolder.postNameView.setTextSize(24);
parentHolder.postTimeView.setTextSize(24);
state++;
}
viewHolder.postTimeView.setTextColor(getResources().getColor(R.color.text_not_highlighted));
viewHolder.postNameView.setTextColor(getResources().getColor(R.color.text_not_highlighted));
} else {
viewHolder.postTimeView.setTextColor(getResources().getColor(R.color.white));
viewHolder.postNameView.setTextColor(getResources().getColor(R.color.white));
}
so state will be 0 only for once. But parentHolder = (ViewHolder) parent.getTag();
doesn't work. It is null
Also I tried to find use of this post.
EDIT:
The amount of White and Dirty white items varies every time adapter is created since .getStatus()
changes on server due to current time. It can be "before" (if starting time < current time) and "after" (current time < starting time).
And I need to Mark exactly that item, which is kind of "now" i.e. the first one "after".
The entire adapter (UPDATED):
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = myContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.channel_listview_item, null);
viewHolder = new ViewHolder();
viewHolder.postNameView = (TextView) convertView.findViewById(R.id.channel_listview_item_name);
viewHolder.postTimeView = (TextView) convertView.findViewById(R.id.channel_listview_item_time);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (datas.get(position).getStatus().equalsIgnoreCase("before")) {
viewHolder.postTimeView.setTextColor(getResources().getColor(R.color.white));
viewHolder.postNameView.setTextColor(getResources().getColor(R.color.white));
} else {
viewHolder.postTimeView.setTextColor(getResources().getColor(R.color.text_not_highlighted));
viewHolder.postNameView.setTextColor(getResources().getColor(R.color.text_not_highlighted));
//perform this 'if' only once at this position
if (/*need such condition that occurs only for ONCE*/) {
viewHolder.postNameView.setTypeface(null, Typeface.BOLD);
viewHolder.postTimeView.setTypeface(null, Typeface.BOLD);
viewHolder.postNameView.setTextSize(24);
viewHolder.postTimeView.setTextSize(24);
viewHolder.postTimeView.setTextColor(getResources().getColor(R.color.white));
viewHolder.postNameView.setTextColor(getResources().getColor(R.color.white));
}
}
viewHolder.postNameView.setText(datas.get(position).getTitle());
viewHolder.postTimeView.setText(datas.get(position).getStart());
return convertView;
}
}
static class ViewHolder {
TextView postTimeView;
TextView postNameView;
}
Upvotes: 1
Views: 2211
Reputation: 6215
I have suggested code/design that I think should work eventually. It may be similar to your existing code design but I don't see code where datas list is setting the status for "before" or "after". Regardless I am using your terminology of "after" and try avoiding to change much code.
Suggested code:
int afterTimePosition = 0;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
if (convertView == null) {
...
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (position > afterTimePosition) {
// Re-evaluate the AFTER line, MAY need to change it
int currentTime = Server.getTime(); // replace getTime with a proper method call
// Check time only after the AFTER line.
// Need datas to save the date/time.
if (datas.get(afterTimePosition).getTime() < currentTime) {
// Need to change the AFTER line by evaluating time in the ArrayList against current time
for (int i= afterTimePosition; i <= position; i++) {
if (datas.get(i).getTime() >= currentTime) {
afterTimePosition = i;
break; // found the new AFTER line
}
}
} // if datas...
}
if (afterTimePosition < position) {
// BEFORE border line
viewHolder.postTimeView.setTextColor(getResources().getColor(R.color.white));
viewHolder.postNameView.setTextColor(getResources().getColor(R.color.white));
} else {
// AFTER border line
viewHolder.postTimeView.setTextColor(getResources().getColor(R.color.text_not_highlighted));
viewHolder.postNameView.setTextColor(getResources().getColor(R.color.text_not_highlighted));
viewHolder.postNameView.setTypeface(null, Typeface.BOLD);
viewHolder.postTimeView.setTypeface(null, Typeface.BOLD);
}
Notes:
afterTimePosition
is the AFTER line. It will be checked upon in code if (afterTimePosition < position)
for determining when to highlight text. I try to be simple in design so that I won't get easily confused.for (int i= afterTimePosition...
, signifies that the AFTER line or row items needs reevaluation. I think code is simple to understand.Keep us posted...
Upvotes: 1