I am preparing an app for our city to help people find what they want fast and offline. I am using ExpandableListAdapter
with SearchView
and I need to make only group items searchable.Is it possible to do that please ? ...
Here´s my code:
MainActivity.java
package com.martin.icmt;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.SearchView;
import com.martin.icmt.com.martin.icmt.expandablelist.ExpandableListAdapter;
public class MainActivity extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
SearchView search;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
/*
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Nemocnica");
listDataHeader.add("AKB Martinská Kaliareň");
listDataHeader.add("TESCO Informačné Centrum");
listDataHeader.add("Pizzeria Venezia");
listDataHeader.add("Pizzeria Milano");
listDataHeader.add("OC Tulip");
// Adding child data
List<String> Nemocnica = new ArrayList<String>();
Nemocnica.add("Otváracie hodiny: nonstop");
Nemocnica.add("Telefón"+" 0432 545 684");
List<String> AKB = new ArrayList<String>();
AKB.add("Otváracie hodiny: 06:00-18:00");
AKB.add("Telefón"+" 0524 456 545");
List<String> TescoIC = new ArrayList<String>();
TescoIC.add("Otváracie hodiny: nonstop");
TescoIC.add("Telefón"+" 043 / 421 81 11");
List<String> Venezia = new ArrayList<String>();
Venezia.add("Otváracie hodiny: 06:00-18:00");
Venezia.add("Telefón"+" 043 / 430 4916");
List<String> Milano = new ArrayList<String>();
Milano.add("Otváracie hodiny: 08:00-22:00");
Milano.add("Telefón"+" 0907 391 404");
List<String> Tulip = new ArrayList<String>();
Tulip.add("Otváracie hodiny: 09:00-21:00");
Tulip.add("Telefón" + " 043 / 424 15 00");
listDataChild.put(listDataHeader.get(0), Nemocnica); // Header, Child data
listDataChild.put(listDataHeader.get(1), AKB);
listDataChild.put(listDataHeader.get(2), TescoIC);
listDataChild.put(listDataHeader.get(3), Venezia);
listDataChild.put(listDataHeader.get(4), Milano);
listDataChild.put(listDataHeader.get(5), Tulip);
}
}
ExpandableListAdapter.java
package com.martin.icmt.com.martin.icmt.expandablelist;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Filter;
import android.widget.TextView;
import com.martin.icmt.R;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}