package
android.example.view;
import
java.util.ArrayList;
import
com.example.utaite.R;
import
android.content.Context;
import
android.util.Log;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseExpandableListAdapter;
import
android.widget.ImageView;
import
android.widget.TextView;
public
class
MyListAdapter
extends
BaseExpandableListAdapter {
private
Context context;
private
ArrayList<Continent> continentList;
private
ArrayList<Continent> originalList;
public
MyListAdapter(Context context, ArrayList<Continent> continentList) {
this
.context = context;
this
.continentList =
new
ArrayList<Continent>();
this
.continentList.addAll(continentList);
this
.originalList =
new
ArrayList<Continent>();
this
.originalList.addAll(continentList);
}
@Override
public
Object getChild(
int
groupPosition,
int
childPosition) {
ArrayList<Country> countryList = continentList.get(groupPosition).getCountryList();
return
countryList.get(childPosition);
}
@Override
public
long
getChildId(
int
groupPosition,
int
childPosition) {
return
childPosition;
}
@Override
public
View getChildView(
int
groupPosition,
int
childPosition,
boolean
isLastChild, View view, ViewGroup parent) {
Country country = (Country) getChild(groupPosition, childPosition);
if
(view ==
null
) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.child_row,
null
);
}
TextView code = (TextView) view.findViewById(R.id.cv_group1);
TextView name = (TextView) view.findViewById(R.id.cv_group2);
code.setText(country.getCode().trim());
name.setText(country.getName().trim());
return
view;
}
@Override
public
int
getChildrenCount(
int
groupPosition) {
ArrayList<Country> countryList = continentList.get(groupPosition).getCountryList();
return
countryList.size();
}
@Override
public
Object getGroup(
int
groupPosition) {
return
continentList.get(groupPosition);
}
@Override
public
int
getGroupCount() {
return
continentList.size();
}
@Override
public
long
getGroupId(
int
groupPosition) {
return
groupPosition;
}
@Override
public
View getGroupView(
int
groupPosition,
boolean
isLastChild, View view, ViewGroup parent) {
Continent continent = (Continent) getGroup(groupPosition);
if
(view ==
null
) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.group_row,
null
);
}
ImageView heading0 = (ImageView) view.findViewById(R.id.iv_image);
heading0.setImageResource(continent.getIcon());
TextView heading1 = (TextView) view.findViewById(R.id.tv_group1);
heading1.setText(continent.getName1().trim());
TextView heading2 = (TextView) view.findViewById(R.id.tv_group2);
heading2.setText(continent.getName2().trim());
return
view;
}
@Override
public
boolean
hasStableIds() {
return
true
;
}
@Override
public
boolean
isChildSelectable(
int
groupPosition,
int
childPosition) {
return
true
;
}
public
void
filterData(String query) {
query = query.toLowerCase();
Log.v(
"MyListAdapter"
, String.valueOf(continentList.size()));
continentList.clear();
if
(query.isEmpty()) {
continentList.addAll(originalList);
}
else
{
for
(Continent continent : originalList) {
ArrayList<Country> countryList = continent.getCountryList();
ArrayList<Country> newList =
new
ArrayList<Country>();
for
(Country country : countryList) {
if
(country.getCode().toLowerCase().contains(query)
|| country.getName().toLowerCase().contains(query)) {
newList.add(country);
}
}
if
(newList.size() >
0
) {
Continent nContinent =
new
Continent(continent.getIcon(), continent.getName1(),
continent.getName2(), newList);
continentList.add(nContinent);
}
}
}
Log.v(
"MyListAdapter"
, String.valueOf(continentList.size()));
notifyDataSetChanged();
}
}