/*
Swisscom Safe Connect
Copyright (C) 2014 Swisscom
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.swisscom.safeconnect.adapter;
import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import com.swisscom.safeconnect.R;
import com.swisscom.safeconnect.json.ResourceJsonParser;
/**
* class that shows faq and other similar stuff
* @author Vadym Uvin
*/
public class JsonListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<ResourceJsonParser.Elem> list = new LinkedList<ResourceJsonParser.Elem>();
/**
* @param rawResId id of the json under res/raw folder
*/
public JsonListAdapter(Context context, int rawResId) {
this.context = context;
// parse json
list = new ResourceJsonParser(context, rawResId).getListResult();
}
@Override
public int getGroupCount() {
return list.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return list.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return list.get(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
/**
* to avoid instantiation of objects due to list scrolling, we use this class
* @author Vadym Uvin
*
*/
static class ViewHolder {
TextView text;
TextView descr;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ResourceJsonParser.Elem elem = list.get(groupPosition);
View rowView = convertView;
// helps to reuse objects and avoid allocating a memory
if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.list_row_faq_header, parent, false);
ViewHolder h = new ViewHolder();
h.text = (TextView) rowView.findViewById(R.id.item_text);
rowView.setTag(h);
}
ViewHolder h = (ViewHolder) rowView.getTag();
h.text.setText(elem.title);
return rowView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
ResourceJsonParser.Elem elem = (ResourceJsonParser.Elem) getChild(groupPosition, childPosition);
View rowView = convertView;
// helps to reuse objects and avoid allocating a memory
if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.list_row_faq_content, parent, false);
ViewHolder h = new ViewHolder();
h.text = (TextView) rowView.findViewById(R.id.item_text);
// ensures clickable hyperlinks
h.text.setMovementMethod(LinkMovementMethod.getInstance());
rowView.setTag(h);
}
ViewHolder h = (ViewHolder) rowView.getTag();
h.text.setText(elem.content);
return rowView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}