Reputation: 225
First time here and pretty new to Android. I have an issue with my background color in my "events page".
The background of the time is supposed to alternate between dark brown and lighter brown when a new time is displayed, but for some reason, it works sometimes and doesn't work other times.
When the app displays it incorrectly, it either will stay on the same shade of brown when the time changes or it will change the shade of brown when it should stay the same. I am not sure what is causing it.
The data comes from a json file hosted on a server. The data is loaded in an AsyncTask class and the app will wait for the data to be downloaded before the user reaches the events page. A message will display saying "Please wait for the data to be downloaded" if the user clicks events.
Here is an example of what one of the entries looks like:
{
"title": "Some_Title",
"description": "Some_Description\n",
"times": [
{
"startTime": "2014-09-20 09:30:00",
"endTime": "2014-09-20 10:30:00"
},
{
"startTime": "2014-09-20 11:00:00",
"endTime": "2014-09-20 12:00:00"
},
{
"startTime": "2014-09-20 13:00:00",
"endTime": "2014-09-20 14:00:00"
}
],
"locationTitle": "Some_Location",
"locationLat": ##.######,
"locationLng": -###.######,
"typeX": 0,
"typeY": true,
"typeZ": false,
"typeA": false,
"typeB": false,
"typeC": false,
"SpreadsheetName": "Some_Name\n(A_Location)"
},
The colors are handled in an Adapter class. Here is the code that handles the background color and a screenshot of the events page working as well as the Log:
public class EventScheduleAdapter extends BaseAdapter {
private final ArrayList<Event> events;
private final Context context;
private final ArrayList<Integer> holders;
public EventScheduleAdapter(Context context, boolean[] types, Calendar day) {
this.context = context;
this.events = EventList.getInstance().getFilteredEvents(day, types);
holders = new ArrayList<Integer>();
}
@Override
public int getCount() {
return events.size();
}
@Override
public Object getItem(int i) {
return events.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
Event event = events.get(position);
View vi = convertView;
ViewHolder holder;
//ImageView image = new ImageView(this.context);
//FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
//params.setMargins(20, 20, 20, 20);
if (convertView == null) {
vi = View.inflate(context, R.layout.event_list_item, null);
holder = new ViewHolder();
holder.time = (TextView) vi.findViewById(R.id.timeNumber);
holder.am = (TextView) vi.findViewById(R.id.timeAM);
holder.title = (TextView) vi.findViewById(R.id.title);
holder.location = (TextView) vi.findViewById(R.id.location);
//holder.description = (TextView) vi.findViewById(R.id.description);
holder.color = (LinearLayout) vi.findViewById(R.id.color);
holder.timeBackground = (RelativeLayout) vi.findViewById(R.id.time);
//holder.image = (ImageView) vi.findViewById(R.id.tag_A);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
if(event.getStartTime() != null && (position == 0 || !EventList.isSameTime(event.getStartTime(), events.get(position - 1).getStartTime()))) {
holder.time.setText(String.format("%tl:%tM", event.getStartTime(), event.getStartTime()));
holder.am.setText(String.format("%Tp", event.getStartTime()));
} else {
holder.time.setText("");
holder.am.setText("");
}
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/main.ttf");
holder.title.setText(event.getTitle());
holder.title.setTypeface(font);
holder.location.setText(event.getLocationTitle());
holder.location.setTypeface(font);
//holder.description.setText(event.getDescription());
//holder.description.setTypeface(Typeface.createFromAsset(context.getAssets(), "fonts/description.ttf"));
holder.color.removeAllViews();
if(event.isTypeA()) {
holder.color.addView(newColorView(R.drawable.tag_A));
} if(event.isTypeB()) {
holder.color.addView(newColorView(R.drawable.tag_B));
} if(event.isTypeC()) {
holder.color.addView(newColorView(R.drawable.tag_C));
} if(event.isTypeD()) {
holder.color.addView(newColorView(R.drawable.tag_D));
} if(event.isTypeE()) {
holder.color.addView(newColorView(R.drawable.tag_E));
}
if (position != 0) {
Log.i("EventScheduleAdapter", "position " + position + "event @ position: " + event.getTitle());
if (!EventList.isSameTime(event.getStartTime(), events.get(position - 1).getStartTime())) {
Log.i("EventScheduleAdapter", "Same time is FALSE");
if(holders.get(position - 1) == R.color.background_event_dark) {
Log.i("EventScheduleAdapter", "Different Color LIGHT");
holder.timeColor = R.color.background_event_light;
holders.add(holder.timeColor);
Log.i("EventScheduleAdapter", "position Color" + holders.get(position));
} else {
Log.i("EventScheduleAdapter", "Different Color DARK");
holder.timeColor = R.color.background_event_dark;
holders.add(holder.timeColor);
Log.i("EventScheduleAdapter", "position Color" + holders.get(position));
}
} else {
Log.i("EventScheduleAdapter", "Same time is TRUE");
holder.timeColor = holders.get(position - 1);
// holder.
//holder.timeColor = holder.get(position-1)
if (holder.timeColor == R.color.background_event_dark)
Log.i("EventScheduleAdapter", "SAME COLOR IS DARK");
if (holder.timeColor == R.color.background_event_light)
Log.i("EventScheduleAdapter", "SAME COLOR IS LIGHT");
holders.add(holder.timeColor);
Log.i("EventScheduleAdapter", "position Color" + holders.get(position));
}
} else {
Log.i("EventScheduleAdapter", "Position is 0");
holder.timeColor = R.color.background_event_dark;
holders.add(holder.timeColor);
}
holder.timeBackground.setBackgroundColor(context.getResources().getColor(holder.timeColor));
return vi;
}
/* Circle newColorView(int color) {
Circle layout = new Circle(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
params.setMargins(5, 5, 5, 5);
layout.setLayoutParams(params);
layout.setColor(color);
return layout;
} */
ImageView newColorView(int drawable) {
ImageView layout = new ImageView(context); // put drawable in here
LinearLayout.LayoutParams params;
if (drawable == R.drawable.tag_cp) {
params = new LinearLayout.LayoutParams(78, 50);
}
else if (drawable == R.drawable.tag_slo) {
params = new LinearLayout.LayoutParams(100, 50);
}
else {
params = new LinearLayout.LayoutParams(50, 50);
}
params.setMargins(5, 5, 5, 5);
layout.setLayoutParams(params);
layout.setImageResource(drawable);
return layout;
}
class ViewHolder {
int timeColor;
RelativeLayout timeBackground;
TextView time;
TextView am;
TextView title;
TextView location;
//TextView description;
LinearLayout color;
}
}
I can't post images since I am a new member :( But I will as soon as I can
Upvotes: 1
Views: 641
Reputation: 2611
You keep adding to your holders
arrays with each item you create. So if you are asked to draw item0 3 times and item1 3 times, you end up having 6 entries in the array. Consider using a sparse array
or a int[]
.
And for example when you are asked to create the view for position X, you do:
holders[X] = myColorId;
Upvotes: 1