I am working on a custom array adapter. I have an expandable list view to which I am adding the list. I also have a search view
in which users should be able to search any item. Below is my code for my custom adapter and my fragment
public class ThreeLevelListAdapter extends BaseExpandableListAdapter {
String[] parentHeaders;
List<String[]> secondLevel;
private Context context;
List<LinkedHashMap<String, String[]>> data;
/**
* Constructor
* @param context
* @param parentHeader
* @param secondLevel
* @param data
*/
public ThreeLevelListAdapter(Context context, String[] parentHeader, List<String[]> secondLevel, List<LinkedHashMap<String, String[]>> data) {
this.context = context;
this.parentHeaders = parentHeader;
this.secondLevel = secondLevel;
this.data = data;
}
@Override
public int getGroupCount() {
return parentHeaders.length;
}
@Override
public int getChildrenCount(int groupPosition) {
// no idea why this code is working
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public Object getChild(int group, int child) {
return child;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_first, null);
TextView text = (TextView) convertView.findViewById(R.id.rowParentText);
text.setText(this.parentHeaders[groupPosition]);
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(context);
String[] headers = secondLevel.get(groupPosition);
List<String[]> childData = new ArrayList<>();
HashMap<String, String[]> secondLevelData = data.get(groupPosition);
for (String key : secondLevelData.keySet()) {
childData.add(secondLevelData.get(key));
}
secondLevelELV.setAdapter(new SecondLevelAdapter(context, headers, childData));
secondLevelELV.setGroupIndicator(null);
secondLevelELV.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
int previousGroup = -1;
@Override
public void onGroupExpand(int groupPosition) {
if (groupPosition != previousGroup)
secondLevelELV.collapseGroup(previousGroup);
previousGroup = groupPosition;
}
});
return secondLevelELV;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}}
` Fragment
SearchView mSearchView;
static String TAG = "product search";
ExpandableListView suggestion_list;
ThreeLevelListAdapter threeLevelListAdapterAdapter;
String[] parent = new String[]{"A2A 100 MG TABS", "AQUA-VIT SACHET", "BRONCOPHYLINE SYRUP"};
String[] q1 = new String[]{"BAHRIA TOWN MARKET", "MAIN ROAD BAGH MONCHI LADHA"};
String[] q2 = new String[]{"DAROGHEWALA MAIN BAZAR"};
String[] q3 = new String[]{"ASKARI-10 HOUSING SOCITEY","CHUNGI DOGAJE"};
String[] des1 = new String[]{"M/S MEDSERVE PHARMACY"};
String[] des2 = new String[]{"M/S DANISH MEDICAL STORE"};
String[] des3 = new String[]{"FUTURE HEALTH PHARMACY","M/S ASAD PHARMACY","M/S GHAFOOR MEDICAL STORE"};
String[] des4 = new String[]{"M/S NOOR MEDICOSE"};
String[] des5 = new String[]{"M/S ABDUL GHAFAR MEDIACL STORE"};
ArrayList<String> parentlist = new ArrayList<String>(Arrays.asList(parent));
LinkedHashMap<String, String[]> thirdLevelq1 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq2 = new LinkedHashMap<>();
LinkedHashMap<String, String[]> thirdLevelq3 = new LinkedHashMap<>();
/**
* Second level array list
*/
List<String[]> secondLevel = new ArrayList<>();
/**
* Inner level data
*/
List<LinkedHashMap<String, String[]>> data = new ArrayList<>();
secondLevel.add(q1);
secondLevel.add(q2);
secondLevel.add(q3);
thirdLevelq1.put(q1[0], des1);
thirdLevelq1.put(q1[1], des2);
thirdLevelq2.put(q2[0], des3);
thirdLevelq3.put(q3[0], des4);
thirdLevelq3.put(q3[1], des5);
data.add(thirdLevelq1);
data.add(thirdLevelq2);
data.add(thirdLevelq3);
threeLevelListAdapterAdapter = new ThreeLevelListAdapter(getActivity(), parent, secondLevel, data);
suggestion_list.setAdapter(threeLevelListAdapterAdapter);
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
if (TextUtils.isEmpty(s)) {
// here I want to empty the threeLevelListAdapterAdapter
suggestion_list.clearTextFilter();
} else {
// here I want to fill the threeLevelListAdapterAdapter
}
return true;
}
});
Update 1
I have found the below code which implements the search with an expandable list view.
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.getName(),newList);
continentList.add(nContinent);
}
}
}
Log.v("MyListAdapter", String.valueOf(continentList.size()));
notifyDataSetChanged();
}
How I can clear and fill my custom adapter during the search?
Any help would be highly appreciated
First step, make the adapters to run on ArrayList instead of String[]. The following is a full sample.
MainActivity.java
ThreeLevelListAdapter.java
SecondLevelAdapter.java
SampleData.java