Here is my MainActivity
public class MainActivity extends Activity {
private ExpandableListAdapter expandableListAdapter;
private ExpandableListView expandableListView;
List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;
protected Context mContext;
EditText searchView;
CheckBox chkIos;
Button display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.activity_main);
mContext = this;
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
searchView = (EditText) findViewById(R.id.search_friends_field);
expandableListDetail = ExpandableListDataPump.getData();
expandableListTitle = new ArrayList<String>(
expandableListDetail.keySet());
// categories = Category.getCategories();
expandableListAdapter = new ExpandableListAdapter(this,
expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
expandableListView
.setChoiceMode(expandableListView.CHOICE_MODE_MULTIPLE);
display = (Button) findViewById(R.id.button1);
display.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer OUTPUT = new StringBuffer();
OUTPUT.append("These are selected/n").append(
expandableListAdapter.selected_profileid);
Toast.makeText(MainActivity.this, OUTPUT.toString(),
Toast.LENGTH_LONG).show();
}
});
expandableListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent,
View clickedView, int groupPosition, int childPosition,
long id) {
return true;
}
});
}
public class CustomComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
And here is my ExpandableListAdapter
@SuppressLint({ "InflateParams", "UseSparseArrays" })
public class ExpandableListAdapter extends BaseExpandableListAdapter {
public ArrayList<String> selchkboxlist;
private Context context;
private List<String> expandableListTitle;
HashMap<String, List<String>> expandableListDetail;
private LayoutInflater inflater;
private HashMap<Integer, boolean[]> mChildCheckStates;
private ExpandableListView expandableListView;
public int lastExpandedGroupPosition;
public CheckBox cb;
public String selected;
protected ArrayList<Object> limits;
ArrayList<Boolean> positionArray;
public static List<String> selected_profileid =new ArrayList<String>();
public ExpandableListAdapter(Context context,
List<String> expandableListTitle,
HashMap<String, List<String>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
mChildCheckStates = new HashMap<Integer, boolean[]>();
}
@Override
public int getGroupCount() {
return this.expandableListTitle.size();
}
@Override
// counts the number of children items so the list knows how many times
// calls getChildView() method
public int getChildrenCount(int listPosition) {
return this.expandableListDetail.get(
this.expandableListTitle.get(listPosition)).size();
// return mParent.get(i).children.size();
}
@Override
// gets the title of each parent/group
public Object getGroup(int listPosition) {
// return mParent.get(i).name;
return this.expandableListTitle.get(listPosition);
}
@Override
// gets the name of each item
public Object getChild(int listPosition, int expandedListPosition) {
return this.expandableListDetail.get(
this.expandableListTitle.get(listPosition)).get(
expandedListPosition);
// return mParent.get(i).children.get(i1);
}
@Override
public long getGroupId(int listPosition) {
return listPosition;
}
@Override
public long getChildId(int listPosition, int expandedListPosition) {
return expandedListPosition;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
// in this method you must set the text to see the parent/group on the list
public View getGroupView(int listPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(listPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_group, null);
}
// set category name as tag so view can be found view later
convertView.setTag(getGroup(listPosition).toString());
TextView textView = (TextView) convertView.findViewById(R.id.listTitle);
// "i" is the position of the parent/group in the list
textView.setText(getGroup(listPosition).toString());
TextView sub = (TextView) convertView
.findViewById(R.id.list_item_text_subscriptions);
return convertView;
}
@Override
// in this method you must set the text to see the children on the list
public View getChildView(final int listPosition,
final int expandedListPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
final String expandedListText = (String) getChild(listPosition,
expandedListPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null);
}
TextView textView = (TextView) convertView
.findViewById(R.id.item_name_expandable_list_item);
textView.setText(expandedListText);
cb = (CheckBox) convertView.findViewById(R.id.check_mark_picker_item);
cb.setOnCheckedChangeListener(null);
if (mChildCheckStates.containsKey(listPosition)) {
/*
* if the hashmap mChildCheckStates<Integer, Boolean[]> contains the
* value of the parent view (group) of this child (aka, the key),
* then retrive the boolean array getChecked[]
*/
boolean getChecked[] = mChildCheckStates.get(listPosition);
// set the check state of this position's checkbox based on the
// boolean value of getChecked[position]
cb.setChecked(getChecked[expandedListPosition]);
} else {
/*
* if the hashmap mChildCheckStates<Integer, Boolean[]> does not
* contain the value of the parent view (group) of this child (aka,
* the key), (aka, the key), then initialize getChecked[] as a new
* boolean array and set it's size to the total number of children
* associated with the parent group
*/
boolean getChecked[] = new boolean[getChildrenCount(listPosition)];
// add getChecked[] to the mChildCheckStates hashmap using
// mGroupPosition as the key
mChildCheckStates.put(listPosition, getChecked);
// set the check state of this position's checkbox based on the
// boolean value of getChecked[position]
cb.setChecked(false);
}
cb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// is chkIos checked?
if (((CheckBox) v).isChecked()) {
Toast.makeText(
context,
expandableListDetail.get(
expandableListTitle.get(listPosition)).get(
expandedListPosition)
+ ((CheckBox) v).isChecked(),
Toast.LENGTH_LONG).show();
}
}
});
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
boolean getChecked[] = mChildCheckStates.get(listPosition);
getChecked[expandedListPosition] = isChecked;
mChildCheckStates.put(listPosition, getChecked);
selected=expandableListTitle.get(listPosition)+ " : "+ expandableListDetail.get(expandableListTitle.get(listPosition)).get(expandedListPosition);
System.out.println("value of" + selected);
selected_profileid.add(selected);
System.out.println("add selected_profileid "+selected_profileid);
} else {
boolean getChecked[] = mChildCheckStates.get(listPosition);
getChecked[expandedListPosition] = isChecked;
mChildCheckStates.put(listPosition, getChecked);
selected=expandableListTitle.get(listPosition)+ " : "+ expandableListDetail.get(expandableListTitle.get(listPosition)).get(expandedListPosition);
System.out.println("value of" + selected);
selected_profileid.remove(selected);
}
}
});
return convertView;
}
@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
return true;
}
}
I want to implement search filter to my expandablelistview when edit text enters a word it should show all the list of childitems with checkboxes and when checked it ,automatically in the normal expandablelistview childitem should be checked.can we do that any suggestions
Aucun commentaire:
Enregistrer un commentaire