Created
February 27, 2015 14:34
-
-
Save extmkv/b07edec65f9853b652d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.locaweb.eventos.android.adapter; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.List; | |
import android.content.Context; | |
import android.graphics.Color; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseExpandableListAdapter; | |
import android.widget.ImageView; | |
import android.widget.LinearLayout; | |
import android.widget.TextView; | |
import br.com.locaweb.eventos.android.data.MyItem; | |
import br.com.locaweb.eventos.android.helper.MIContext; | |
import br.com.locaweb.eventos.android.R; | |
import br.com.locaweb.eventos.android.util.Utils; | |
public class MIPersonalAdapter extends BaseExpandableListAdapter { | |
// The parent context | |
private Context _activityContext; | |
// The inflater used to create views | |
final LayoutInflater _inflater; | |
private List<MyItem> _myitems; | |
private ArrayList<String> _categories; | |
private int _filterSelected; | |
public MIPersonalAdapter(Context context, List<MyItem> myitems, ArrayList<String> categories, int filterSelected) { | |
_inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
try | |
{ | |
_myitems = myitems; | |
_activityContext = context; | |
_categories = categories; | |
_filterSelected = filterSelected; | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public Object getChild(int groupPosition, int childPosition) { | |
if(_filterSelected == 0) { | |
int aux = 0; | |
for(int i=0; i<_myitems.size(); i++) { | |
if(_myitems.get(i).calendarStartDate.equals(_categories.get(groupPosition))) { | |
if(childPosition == aux) return _myitems.get(i); | |
aux++; | |
} | |
} | |
} | |
else { | |
return _myitems.get(childPosition); | |
} | |
return null; | |
} | |
@Override | |
public long getChildId(int groupPosition, int childPosition) { | |
return childPosition; | |
} | |
@Override | |
public View getChildView(int groupPosition, int childPosition, | |
boolean isLastChild, View convertView, ViewGroup parent) { | |
View mainContainer = convertView; | |
MyItem item = (MyItem) getChild(groupPosition, childPosition); | |
if(mainContainer == null) { | |
if(_filterSelected == 0 || _filterSelected == 3) | |
mainContainer = _inflater.inflate(R.layout.layout_generic_list_item, null); | |
else mainContainer = _inflater.inflate(R.layout.layout_oneline_list_item, null); | |
} | |
final LinearLayout mainLayout = (LinearLayout) mainContainer.findViewById(R.id.mainLayout); | |
final TextView txtTitle = (TextView) mainContainer.findViewById(R.id.txtTitle); | |
final TextView txtEventHours = (TextView) mainContainer.findViewById(R.id.txtEventHours); | |
final TextView txtEventLocal = (TextView) mainContainer.findViewById(R.id.txtEventLocal); | |
final TextView txtDescription = (TextView) mainContainer.findViewById(R.id.txtDescription); | |
final ImageView imgArrow = (ImageView) mainContainer.findViewById(R.id.imgRightArrow); | |
final ImageView imgStar = (ImageView)mainContainer.findViewById(R.id.imgStar); | |
txtTitle.setTextColor(Utils.getColor(((MIContext)_activityContext.getApplicationContext()).getStyle(MIContext.STYLE_LIST_ITEM_COLOR))); | |
txtEventHours.setTextColor(Utils.getColor(((MIContext)_activityContext.getApplicationContext()).getStyle(MIContext.STYLE_HIGHLIGHT_COLOR))); | |
txtEventLocal.setTextColor(Utils.getColor(((MIContext)_activityContext.getApplicationContext()).getStyle(MIContext.STYLE_MAIN_COLOR))); | |
txtDescription.setTextColor(Utils.getColor(((MIContext)_activityContext.getApplicationContext()).getStyle(MIContext.STYLE_NEUTRAL_COLOR))); | |
imgStar.setVisibility(View.VISIBLE); | |
imgStar.setTag(groupPosition + MIL2ItemGeneralAdapter.SEPARATOR + childPosition); | |
if(item.target_screen == null || item.target_screen.trim().equals("")) | |
imgArrow.setVisibility(View.INVISIBLE); | |
else imgArrow.setVisibility(View.VISIBLE); | |
mainLayout.setVisibility(View.VISIBLE); | |
txtTitle.setVisibility(View.VISIBLE); | |
txtTitle.setMaxLines(2); | |
txtTitle.setText(item.name); | |
//Calendar events | |
if(_filterSelected == 0 || _filterSelected == 3) { | |
if(item.calendarStartTime!= null | |
&& item.calendarEndTime!= null) { | |
txtEventHours.setText(item.calendarStartTime + " - " + item.calendarEndTime); | |
} | |
else if(item.calendarStartTime!= null) { | |
String stStartTime = item.calendarStartTime; | |
txtEventHours.setText(stStartTime); | |
} | |
else if(item.calendarStartTime == null && item.calendarEndTime == null) { | |
txtEventHours.setVisibility(View.GONE); | |
txtEventHours.setText(_activityContext.getString(R.string.allDay)); | |
} | |
if(_filterSelected == 0) { | |
//Local | |
txtEventLocal.setText(item.local); | |
//Organizer | |
String text = item.organizer.length()>150 ? item.organizer.substring(0, 150) : item.organizer; | |
txtDescription.setText(text); | |
} | |
else if(_filterSelected == 3) { | |
if(item.calendarStartDate != null) { | |
Date d = new Date(); | |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); | |
try { | |
d = dateFormat.parse(item.calendarStartDate); | |
} catch (ParseException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
SimpleDateFormat datePtFormat = new SimpleDateFormat("d-M-yyyy"); | |
txtEventLocal.setText(datePtFormat.format(d)); | |
//txtEventLocal.setText(item.calendarStartDate); | |
} | |
} | |
} | |
//Notif. Description | |
if(_filterSelected == 3) { | |
if(item.local!=null) | |
txtDescription.setText(item.local); | |
} | |
return mainContainer; | |
} | |
@Override | |
public int getChildrenCount(int groupPosition) { | |
//return _myitems.size(); | |
if(_filterSelected == 0) { | |
int aux = 0; | |
for(int i=0; i<_myitems.size(); i++) { | |
if(_myitems.get(i).calendarStartDate.equals(_categories.get(groupPosition))) { | |
aux++; | |
} | |
} | |
return aux; | |
} | |
else { | |
return _myitems.size(); | |
} | |
} | |
@Override | |
public Object getGroup(int groupPosition) { | |
if(_filterSelected == 0) return _categories.get(groupPosition); | |
return ""; | |
} | |
@Override | |
public int getGroupCount() { | |
if(_filterSelected == 0) return _categories.size(); | |
return 1; | |
} | |
@Override | |
public long getGroupId(int groupPosition) { | |
return groupPosition; | |
} | |
@Override | |
public View getGroupView(int groupPosition, boolean isExpanded, | |
View convertView, ViewGroup parent) { | |
final View mainContainer; | |
if(_filterSelected == 0) { | |
if(convertView == null) // if it's not recycled, initialize some attributes | |
{ | |
mainContainer = _inflater.inflate(R.layout.layout_l2_list_category, null); | |
} | |
else { // The view was already initialized. Make sure the data is right, since only the view will be recycled | |
mainContainer = convertView; | |
} | |
final TextView txtLabel = (TextView) mainContainer.findViewById(R.id.txtLabel); | |
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); | |
SimpleDateFormat newDateFormat = new SimpleDateFormat("d MMMM"); | |
try { | |
Date d = dateFormat.parse(_categories.get(groupPosition)); | |
txtLabel.setText(newDateFormat.format(d)); | |
} catch (ParseException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
txtLabel.setBackgroundColor(Color.parseColor("#a5a6ab")); | |
} | |
else { | |
return new View(_activityContext); | |
} | |
return mainContainer; | |
} | |
@Override | |
public boolean hasStableIds() { | |
// TODO Auto-generated method stub | |
return false; | |
} | |
@Override | |
public boolean isChildSelectable(int groupPosition, int childPosition) { | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment