I have an ExpandableListView of items and on list item I have TextView with two buttons to increment or decrements the value in TextView on clicks. The problem occurs every time I try to scroll the list. If I increment one item and then scroll the list the values get mixed (becouse the ListView keeps recycling its views) and I don't know how to fix it.
I have tried many soulutions I have found here, so yes this may be a duplicate, but I wasn't able to solve my problem with any method I have found.
My ExpandableListAdapter.java
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.Button;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class ExpandableListAdapter extends BaseExpandableListAdapter {
public static class ViewHolder {
TextView childText;
TextView counterText;
Button addItemButton;
Button deleteItemButton;
int quantity = 0;
}
private Context context;
private List<String> listDataHeader;
private HashMap<String, List<String>> listHashMap;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
this.context = context;
this.listDataHeader = listDataHeader;
this.listHashMap = listHashMap;
}
#Override
public int getGroupCount() {
return listDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return listHashMap.get(listDataHeader.get(groupPosition)).size();
}
#Override
public Object getGroup(int groupPosition) {
return listDataHeader.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return listHashMap.get(listDataHeader.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_group, null);
}
TextView listHeader = (TextView) convertView.findViewById(R.id.list_header);
listHeader.setTypeface(null, Typeface.BOLD);
listHeader.setText(headerTitle);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
TextView textListChild = (TextView) convertView.findViewById(R.id.list_item_header);
TextView itemsCounter = (TextView) convertView.findViewById(R.id.items_counter);
Button addItemButton = (Button) convertView.findViewById(R.id.plus_btn);
viewHolder = new ViewHolder();
viewHolder.childText = textListChild;
viewHolder.counterText = itemsCounter;
viewHolder.addItemButton = addItemButton;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final String childText = (String) getChild(groupPosition, childPosition);
viewHolder.childText.setText(childText);
viewHolder.addItemButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//int itemCount = Integer.parseInt((String)viewHolder.counterText.getText());
//itemCount++;
viewHolder.quantity++;
viewHolder.counterText.setText( Integer.toString(viewHolder.quantity));
PutOrderDrinks.addOrder(childText);
}
});
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
It is not a good idea to store quantity in ViewHolder.
Hope below sample helps :)
MainActivity.java:
public class MainActivity extends Activity {
Button clearChecks, putOrder;
ExpandableListView expandableListView;
ExpandableListViewAdapter expandableListAdapter;
int lastExpandedPosition = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableListView = findViewById(R.id.expandedListView);
clearChecks = findViewById(R.id.btnClearChecks);
putOrder = findViewById(R.id.btnPutOrder);
List<String> listTitle = genGroupList();
expandableListAdapter = new ExpandableListViewAdapter(this, listTitle, genChildList(listTitle));
expandableListView.setAdapter(expandableListAdapter);
expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
if(lastExpandedPosition != -1 && (lastExpandedPosition != groupPosition)){
expandableListView.collapseGroup(lastExpandedPosition);
}
lastExpandedPosition = groupPosition;
}
});
clearChecks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
expandableListAdapter.clearChecks();
}
});
putOrder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ArrayList<ChildItemSample> putOrder = expandableListAdapter.getOrderList();
String msg = "";
for(int i=0; i<putOrder.size(); i++){
msg += putOrder.get(i).getName() + ": " + putOrder.get(i).getQty() + "\n";
}
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<String> genGroupList(){
ArrayList<String> listGroup = new ArrayList<>();
for(int i=1; i<10; i++){
listGroup.add("Group: " + i);
}
return listGroup;
}
private Map<String, List<ChildItemSample>> genChildList(List<String> header){
Map<String, List<ChildItemSample>> listChild = new HashMap<>();
for(int i=0; i<header.size(); i++){
List<ChildItemSample> testDataList = new ArrayList<>();
int a = (int)(Math.random()*8);
for(int j=0; j<a; j++){
ChildItemSample testItem = new ChildItemSample("Child " + (j + 1), 0);
testDataList.add(testItem);
}
listChild.put(header.get(i), testDataList);
}
return listChild;
}
}
ChildItemSample.java:
public class ChildItemSample {
private boolean checked = false;
private String name;
private int qty;
public int getQty() {
return qty;
}
public void setQty(int qty) {
this.qty = qty;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String getName() {
return name;
}
public ChildItemSample(String name, int qty){
this.name = name;
this.qty = qty;
}
}
ExpandableListViewAdapter.java:
public class ExpandableListViewAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> listGroup;
private Map<String, List<ChildItemSample>> listChild;
private int checkedBoxesCount;
private boolean[] checkedGroup;
public ExpandableListViewAdapter(Context context, List<String> listGroup, Map<String,
List<ChildItemSample>> listChild) {
this.context = context;
this.listGroup = listGroup;
this.listChild = listChild;
checkedBoxesCount = 0;
checkedGroup = new boolean[listGroup.size()];
}
#Override
public int getGroupCount() {
return listGroup.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return listChild.get(listGroup.get(groupPosition)).size();
}
#Override
public String getGroup(int groupPosition) {
return listGroup.get(groupPosition);
}
#Override
public ChildItemSample getChild(int groupPosition, int childPosition) {
return listChild.get(listGroup.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
String itemGroup = getGroup(groupPosition);
GroupViewHolder groupViewHolder;
if(view == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.expanded_list_group, null);
groupViewHolder = new GroupViewHolder();
groupViewHolder.tvGroup = view.findViewById(R.id.tv_group);
groupViewHolder.cbGroup = view.findViewById(R.id.cb_group);
groupViewHolder.cbGroup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int pos = (int)view.getTag();
checkedGroup[pos] = !checkedGroup[pos];
for(ChildItemSample item : listChild.get(listGroup.get(pos))){
item.setChecked(checkedGroup[pos]);
}
notifyDataSetChanged();
}
});
view.setTag(groupViewHolder);
}else {
groupViewHolder = (GroupViewHolder)view.getTag();
}
groupViewHolder.tvGroup.setText(String.format("%s (%d)", itemGroup, getChildrenCount(groupPosition)));
if(checkedGroup[groupPosition]) groupViewHolder.cbGroup.setChecked(true);
else groupViewHolder.cbGroup.setChecked(false);
groupViewHolder.cbGroup.setTag(groupPosition);
return view;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
ChildItemSample expandedListText = getChild(groupPosition,childPosition);
ChildViewHolder childViewHolder;
if(view == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.expanded_list_item, null);
childViewHolder = new ChildViewHolder();
childViewHolder.tvChild = view.findViewById(R.id.tv_child);
childViewHolder.cbChild = view.findViewById(R.id.cb_child);
childViewHolder.tvQty = view.findViewById(R.id.tv_qty);
childViewHolder.btInc = view.findViewById(R.id.bt_inc);
childViewHolder.btDec = view.findViewById(R.id.bt_dec);
childViewHolder.cbChild.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
CheckBox cb = (CheckBox) view;
Pos pos = (Pos) cb.getTag();
ChildItemSample selectedItem = getChild(pos.group, pos.child);
selectedItem.setChecked(cb.isChecked());
if(cb.isChecked()){
checkedBoxesCount++;
Toast.makeText(context,"Checked value is: " + getChild(pos.group, pos.child).getName(),
Toast.LENGTH_SHORT).show();
}else {
checkedBoxesCount--;
if(checkedBoxesCount == 0){
Toast.makeText(context,"nothing checked",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context,"unchecked",Toast.LENGTH_SHORT).show();
}
}
notifyDataSetChanged();
}
});
childViewHolder.btInc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button bt = (Button) view;
Pos pos = (Pos) bt.getTag();
ChildItemSample selectedItem = getChild(pos.group, pos.child);
selectedItem.setQty(selectedItem.getQty() + 1);
notifyDataSetChanged();
}
});
childViewHolder.btDec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button bt = (Button) view;
Pos pos = (Pos) bt.getTag();
ChildItemSample selectedItem = getChild(pos.group, pos.child);
if(selectedItem.getQty() > 0) selectedItem.setQty(selectedItem.getQty() - 1);
notifyDataSetChanged();
}
});
}else {
childViewHolder = (ChildViewHolder)view.getTag();
}
childViewHolder.cbChild.setChecked(expandedListText.isChecked());
childViewHolder.tvChild.setText(expandedListText.getName() + " :");
childViewHolder.tvQty.setText("" + expandedListText.getQty());
childViewHolder.cbChild.setTag(new Pos(groupPosition, childPosition));
childViewHolder.btInc.setTag(new Pos(groupPosition, childPosition));
childViewHolder.btDec.setTag(new Pos(groupPosition, childPosition));
view.setTag(childViewHolder);
return view;
}
public void clearChecks() {
for(int i=0; i<checkedGroup.length; i++) checkedGroup[i] = false;
for(List<ChildItemSample> value : listChild.values()) {
for (ChildItemSample sample : value) {
sample.setChecked(false);
}
}
checkedBoxesCount = 0;
notifyDataSetChanged();
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private class GroupViewHolder {
CheckBox cbGroup;
TextView tvGroup;
}
private class ChildViewHolder {
CheckBox cbChild;
TextView tvChild;
TextView tvQty;
Button btInc;
Button btDec;
}
private class Pos {
int group;
int child;
Pos(int group, int child){
this.group = group;
this.child = child;
}
}
public ArrayList<ChildItemSample> getOrderList(){
ArrayList<ChildItemSample> overallOrder = new ArrayList<>();
for(int i=0; i<getGroupCount(); i++){
for(int j=0; j<getChildrenCount(i); j++){
if(getChild(i,j).getQty() > 0){
ChildItemSample newOrder = new ChildItemSample(getGroup(i) + ">" +
getChild(i, j).getName(), getChild(i, j).getQty());
overallOrder.add(newOrder);
}
}
}
return overallOrder;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btnClearChecks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Clear Checks" />
<Button
android:id="#+id/btnPutOrder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Put Order" />
</LinearLayout>
<ExpandableListView
android:id="#+id/expandedListView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ExpandableListView>
</LinearLayout>
expanded_list_group.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants" >
<CheckBox
android:id="#+id/cb_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_gravity="center_vertical" />
<TextView
android:id="#+id/tv_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:textSize="30sp" />
</LinearLayout>
expanded_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="#+id/cb_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="60dp" />
<TextView
android:id="#+id/tv_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/cb_child"
android:text="Child: "
android:textSize="20sp" />
<TextView
android:id="#+id/tv_qty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/tv_child"
android:text="0"
android:textSize="20sp" />
<Button
android:id="#+id/bt_inc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/bt_dec"
android:text="+" />
<Button
android:id="#+id/bt_dec"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:text="-" />
</RelativeLayout>
Try this adapter:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
class ViewHolder {
TextView childText;
TextView counterText;
Button addItemButton;
Button deleteItemButton;
}
class ChildItem{
String name;
int quantity;
ChildItem(String name, int quantity){
this.name = name;
this.quantity = quantity;
}
}
class Pos{
int group;
int child;
Pos(int group, int child){
this.group = group;
this.child = child;
}
}
private Context context;
private List<String> listDataHeader;
//private HashMap<String, List<String>> listHashMap;
private HashMap<String, List<ChildItem>> listChildMap;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) {
this.context = context;
this.listDataHeader = listDataHeader;
listChildMap = new HashMap<>();
for(int i=0; i<getGroupCount(); i++){
List<ChildItem> listTemp = new ArrayList<>();
for(int j=0; j<listHashMap.get(listDataHeader.get(i)).size(); j++){
listTemp.add(new ChildItem(listHashMap.get(listDataHeader.get(i)).get(j), 0));
}
listChildMap.put(listDataHeader.get(i), listTemp);
}
}
#Override
public int getGroupCount() {
return listDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return listChildMap.get(listDataHeader.get(groupPosition)).size();
}
#Override
public String getGroup(int groupPosition) {
return listDataHeader.get(groupPosition);
}
#Override
public ChildItem getChild(int groupPosition, int childPosition) {
return listChildMap.get(listDataHeader.get(groupPosition)).get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = getGroup(groupPosition);
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_group, null);
}
TextView listHeader = (TextView) convertView.findViewById(R.id.list_header);
listHeader.setTypeface(null, Typeface.BOLD);
listHeader.setText(headerTitle);
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
TextView textListChild = (TextView) convertView.findViewById(R.id.list_item_header);
TextView itemsCounter = (TextView) convertView.findViewById(R.id.items_counter);
Button addItemButton = (Button) convertView.findViewById(R.id.plus_btn);
viewHolder = new ViewHolder();
viewHolder.childText = textListChild;
viewHolder.counterText = itemsCounter;
viewHolder.addItemButton = addItemButton;
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
ChildItem child = getChild(groupPosition, childPosition);
viewHolder.childText.setText(child.name);
viewHolder.counterText.setText("" + child.quantity);
viewHolder.addItemButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Pos pos = (Pos)v.getTag();
ChildItem selectedItem = getChild(pos.group, pos.child);
selectedItem.quantity = selectedItem.quantity + 1;
notifyDataSetChanged();
PutOrderDrinks.addOrder(selectedItem.name);
}
});
viewHolder.addItemButton.setTag(new Pos(groupPosition, childPosition));
convertView.setTag(viewHolder);
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Related
I'm trying to implement a selection activity for a given list of items. Each item is checkable, so I have an item with a TextView and a CheckBox. I implemented a ListView for displaying all the options and a Spinner for showing only the "Top Ten" choices, as a subset of the same list. For now I'm showing all the items in both ListView and Spinner.
I want for the items in the ListView to update when the user selects an item in the Spinner (Note: The reverse path works fine, as the Spinner grabs the updated ArrayList each time it dropsdown).
I tried to implement setOnItemSelectedListener for my Spinner, and to call notifyOnDataSetChanged() for my ListViewAdapter inside the Listener. But the Listener is only called on collapse and I get a weird (maybe unrelated) warning message.
The onItemSelectedListener for the Spinner only runs when the Spinner gets collapsed. But notifyOnDataSetChanged() seems to ignore the checked status of the items as a change. How can I make the first option run everytime I check an item and have the change get properly received by the ListAdapter?
Here's the Activity.java code:
public class TriageReasonActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_triage_reason);
final String[] select_qualification = {
"Select Qualification", "10th / Below", "12th", "Diploma", "UG",
"PG", "Phd"};
Spinner spinner = (Spinner) findViewById(R.id.top_reasons_spinner);
ListView symptoms_list = (ListView) findViewById(R.id.view_list_symptoms);
ArrayList<Symptoms> listVOs = new ArrayList<>();
for (int i = 0; i < select_qualification.length; i++) {
Symptoms reason = new Symptoms();
reason.setTitle(select_qualification[i]);
reason.setSelected(false);
listVOs.add(reason);
}
SymptomsListAdapter mListAdapter = new SymptomsListAdapter(this, 0,
listVOs);
SymptomsSpinnerAdapter mSpinnerAdapter = new SymptomsSpinnerAdapter(this, 0,
listVOs);
symptoms_list.setAdapter(mListAdapter);
spinner.setAdapter(mSpinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
Log.i("Item selected", "but not cahnged");
symptoms_list.invalidateViews();
mListAdapter.notifyDataSetInvalidated();
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
Log.i("Not item selected", "but actually it did");
}
});
}
The SpinnerCustom Adapter code:
public class SymptomsSpinnerAdapter extends ArrayAdapter<Symptoms>{
private Context mContext;
private ArrayList<Symptoms> listState;
private SymptomsSpinnerAdapter myAdapter;
private boolean isFromView = false;
/*#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
//mNotifyOnChange = true;
}*/
public SymptomsSpinnerAdapter(Context context, int resource, List<Symptoms> objects) {
super(context, resource, objects);
this.mContext = context;
this.listState = (ArrayList<Symptoms>) objects;
this.myAdapter = this;
}
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.item_reasons, null);
holder = new ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
// To check weather checked event fire from getview() or user input
isFromView = true;
holder.mCheckBox.setChecked(listState.get(position).isSelected());
isFromView = false;
if ((position == 0)) {
holder.mCheckBox.setVisibility(View.INVISIBLE);
} else {
holder.mCheckBox.setVisibility(View.VISIBLE);
}
holder.mCheckBox.setTag(position);
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
if (!isFromView) {
listState.get(position).setSelected(isChecked);
}
}
});
return convertView;
}
#Override
public int getCount() {
return listState.size();
}
#Override
public Symptoms getItem(int position) {
if( position < 1 ) {
return null;
}
else {
return listState.get(position-1);
}
}
#Override
public long getItemId(int position) {
return 0;
}
private class ViewHolder {
private TextView mTextView;
private CheckBox mCheckBox;
}
}
Here's the (almost identical) ListAdapter:
public class SymptomsListAdapter extends BaseAdapter implements ListAdapter {
private Context mContext;
private ArrayList<Symptoms> listState;
private boolean isFromView = false;
public SymptomsListAdapter(Context context, int resource, List<Symptoms> objects) {
this.mContext = context;
this.listState = (ArrayList<Symptoms>) objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(final int position, View convertView,
ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflator = LayoutInflater.from(mContext);
convertView = layoutInflator.inflate(R.layout.item_reasons, null);
holder = new SymptomsListAdapter.ViewHolder();
holder.mTextView = (TextView) convertView.findViewById(R.id.text);
holder.mCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (SymptomsListAdapter.ViewHolder) convertView.getTag();
}
holder.mTextView.setText(listState.get(position).getTitle());
// To check weather checked event fire from getview() or user input
isFromView = true;
holder.mCheckBox.setChecked(listState.get(position).isSelected());
isFromView = false;
if ((position == 0)) {
holder.mCheckBox.setVisibility(View.INVISIBLE);
} else {
holder.mCheckBox.setVisibility(View.VISIBLE);
}
holder.mCheckBox.setTag(position);
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
if (!isFromView) {
listState.get(position).setSelected(isChecked);
}
}
});
return convertView;
}
#Override
public int getCount() {
return listState.size();
}
#Override
public Symptoms getItem(int position) {
if( position < 1 ) {
return null;
}
else {
return listState.get(position-1);
}
}
#Override
public long getItemId(int position) {
return 0;
}
private class ViewHolder {
public TextView mTextView;
public CheckBox mCheckBox;
}
}
And here's the warning I'm getting:
W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
EDIT: Adding the layouts and the model class in case they may cause an issue:
Activity Layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="demo.hb.activity.visit.TriageReasonActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textFontWeight="6dp"
android:textSize="30sp"
android:layout_margin="20dp"
android:textAlignment="center"
android:textColor="#000000"
android:text="What is the reason for your visit?" />
<Spinner
android:id="#+id/top_reasons_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="end">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_list_symptoms"
android:layout_above="#+id/next_btn"
android:layout_alignParentTop="true"/>
</RelativeLayout>
</LinearLayout>
</FrameLayout>
Item layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="text"
android:textAlignment="gravity" />
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Model Class:
public class Symptoms {
private String title;
private boolean selected;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
The reason that nothing is changing is because you haven't implemented the method to handle the data set changes. You need to handle how the data is reloaded in your adapter:
public class SymptomsListAdapter extends BaseAdapter implements ListAdapter {
...
public void refreshData(ArrayList<Symptoms> objects){
this.listState = (ArrayList<Symptoms>) objects;
notifyDataSetChanged();
}
...
}
This link does a great job of explaining how the notifyDataSetInvalidated() works (or in your case, why it's not working).
I need to make an expandable listview but for parent i have to pass arraylist and i am getting class cast exception in getGroupView . Please help me in writing the adapter for the following :
PhraseModel.java :
package in.abc.pdfsearchapp;
import android.os.Parcel;
import android.os.Parcelable;
import java.util.ArrayList;
public class PhraseModel implements Parcelable {
private ArrayList<PhraseList> phraseList;
// constructor
public PhraseModel(ArrayList<PhraseList> phraseList) {
this.phraseList = phraseList;
}
//---------------------------Getter-Setter--------------------------------------------------------------------------------
public ArrayList<PhraseList> getPhraseList() {
return phraseList;
}
public void setPhraseList(ArrayList<PhraseList> phraseList) {
this.phraseList = phraseList;
}
// Parcelling
protected PhraseModel(Parcel in) {
phraseList=in.createTypedArrayList(PhraseList.CREATOR);
}
public static final Creator<PhraseModel> CREATOR = new Creator<PhraseModel>() {
#Override
public PhraseModel createFromParcel(Parcel in) {
return new PhraseModel(in);
}
#Override
public PhraseModel[] newArray(int size) {
return new PhraseModel[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeTypedList(phraseList);
}
//----------------------------------- //PhraseList // ------------------------------------------------------------------------
public static class PhraseList implements Parcelable {
public int documentId,noOfOccurences;
public String documentName,phrase;
public ArrayList<OccurenceDetails> occurenceDetails;
//constructor
public PhraseList(int documentId, String documentName,String phrase, int noOfOccurences, ArrayList<OccurenceDetails> occurenceDetails) {
this.documentId = documentId;
this.documentName = documentName;
this.phrase = phrase;
this.noOfOccurences = noOfOccurences;
this.occurenceDetails = occurenceDetails;
}
//------------------------------Getter-Setter--------------------------------------------------------------------------------------
public int getDocumentId() {
return documentId;
}
public void setDocumentId(int documentId) {
this.documentId = documentId;
}
public int getNoOfOccurences() {
return noOfOccurences;
}
public void setNoOfOccurences(int noOfOccurences) {
this.noOfOccurences = noOfOccurences;
}
public String getDocumentName() {
return documentName;
}
public void setDocumentName(String documentName) {
this.documentName = documentName;
}
public String getPhrase() {
return phrase;
}
public void setPhrase(String phrase) {
this.phrase = phrase;
}
public ArrayList<OccurenceDetails> getOccurenceDetails() {
return occurenceDetails;
}
public void setOccurenceDetails(ArrayList<OccurenceDetails> occurenceDetails) {
this.occurenceDetails = occurenceDetails;
}
//Parcelling
protected PhraseList(Parcel in) {
documentId = in.readInt();
noOfOccurences = in.readInt();
documentName = in.readString();
phrase = in.readString();
}
public static final Creator<PhraseList> CREATOR = new Creator<PhraseList>() {
#Override
public PhraseList createFromParcel(Parcel in) {
return new PhraseList(in);
}
#Override
public PhraseList[] newArray(int size) {
return new PhraseList[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(documentId);
dest.writeString(documentName);
dest.writeInt(noOfOccurences);
dest.writeString(phrase);
}
}
//--------------------------------- //OccurrenceDetails // --------------------------------------------------------------------
public static class OccurenceDetails implements Parcelable {
int occurenceId,pageNo;
String fullPhrase;
//constructor
public OccurenceDetails(int occurenceId, int pageNo, String fullPhrase) {
this.occurenceId = occurenceId;
this.pageNo = pageNo;
this.fullPhrase = fullPhrase;
}
//-------------------------------Getter-Setter----------------------------------------------------------------------------------
public int getOccurenceId() {
return occurenceId;
}
public void setOccurenceId(int occurenceId) {
this.occurenceId = occurenceId;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
public String getFullPhrase() {
return fullPhrase;
}
public void setFullPhrase(String fullPhrase) {
this.fullPhrase = fullPhrase;
}
//Parcelling
protected OccurenceDetails(Parcel in) {
occurenceId = in.readInt();
pageNo = in.readInt();
fullPhrase = in.readString();
}
public static final Creator<OccurenceDetails> CREATOR = new Creator<OccurenceDetails>() {
#Override
public OccurenceDetails createFromParcel(Parcel in) {
return new OccurenceDetails(in);
}
#Override
public OccurenceDetails[] newArray(int size) {
return new OccurenceDetails[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(occurenceId);
dest.writeInt(pageNo);
dest.writeString(fullPhrase);
}
}
}
ExpandableListAdapter.java :
package in.abc.pdfsearchapp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by Android on 22-Nov-17.
*/
public class ExpandableListAdapter extends BaseExpandableListAdapter {
Context context;
public ArrayList<PhraseModel.PhraseList> mPhraseList;
private HashMap<Integer,ArrayList<PhraseModel.OccurenceDetails>> occurenceList;
public ExpandableListAdapter(Context context, ArrayList<PhraseModel.PhraseList> mPhraseList, HashMap<Integer, ArrayList<PhraseModel.OccurenceDetails>> occurenceList) {
this.context = context;
this.mPhraseList = mPhraseList;
this.occurenceList = occurenceList;
}
public Object getChild(int groupPosition, int childPosition){
return this.occurenceList.get(this.mPhraseList.get(groupPosition)).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final ArrayList<PhraseModel.OccurenceDetails> childList = (ArrayList<PhraseModel.OccurenceDetails>) 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 occurenceId,fullPhrase,pageNo;
occurenceId=(TextView)convertView.findViewById(R.id.occurenceId);
fullPhrase=(TextView)convertView.findViewById(R.id.fullPhrase);
pageNo=(TextView)convertView.findViewById(R.id.pageNo);
occurenceId.setText(childList.get(groupPosition).getOccurenceId());
fullPhrase.setText(childList.get(groupPosition).getFullPhrase());
pageNo.setText(childList.get(groupPosition).getPageNo());
return convertView;
}
public int getChildrenCount(int groupPosition) {
return this.occurenceList.get(this.mPhraseList.get(groupPosition))
.size();
}
public Object getGroup(int groupPosition) {
return this.mPhraseList.get(groupPosition);
}
public int getGroupCount() {
return this.mPhraseList.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ArrayList<PhraseModel.PhraseList> parentList = (ArrayList<PhraseModel.PhraseList>) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView docName,totalOccurences;
docName=(TextView)convertView.findViewById(R.id.docName);
totalOccurences=(TextView)convertView.findViewById(R.id.totalOccurences);
docName.setText(parentList.get(groupPosition).getDocumentName());
totalOccurences.setText(parentList.get(groupPosition).getNoOfOccurences());
return convertView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
LandingActivity.java :
package in.abc.pdfsearchapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
public class LandingActivity extends AppCompatActivity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
ArrayList<PhraseModel.PhraseList> phraseLists;
HashMap<Integer, ArrayList<PhraseModel.OccurenceDetails>> occurenceList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_landing);
expListView = (ExpandableListView) findViewById(R.id.phraseList);
// preparing list data
phraseLists=new ArrayList<PhraseModel.PhraseList>();
occurenceList=new HashMap<Integer,ArrayList<PhraseModel.OccurenceDetails>>();
phraseLists.addAll(getmphraseset());
for (PhraseModel.PhraseList pl: phraseLists){
ArrayList<PhraseModel.OccurenceDetails>occurenceDetailses=pl.getOccurenceDetails();
occurenceList.put(pl.getDocumentId(),occurenceDetailses);
}
listAdapter = new ExpandableListAdapter(this, phraseLists,occurenceList);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
phraseLists.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
phraseLists.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
phraseLists.get(groupPosition)
+ " : "
+ occurenceList.get(
phraseLists.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}
private ArrayList<PhraseModel.PhraseList> getmphraseset() {
try{
ArrayList<PhraseModel.PhraseList>phrase_Lists = new ArrayList<PhraseModel.PhraseList>();
ArrayList<PhraseModel.OccurenceDetails>occurenceDetailsArrayList=new ArrayList<PhraseModel.OccurenceDetails>();
JSONObject jsonObject = new JSONObject(readJSONFromAsset());
JSONArray phraseArray = jsonObject.getJSONArray("phraseList");
Log.d("getmphraseset", "phrase count: "+phraseArray.length());
for (int i = 0; i < phraseArray.length(); i++)
{
JSONObject job = phraseArray.getJSONObject(i);
int documentId =job.getInt("documentId");
int noOfOccurences=job.getInt("noOfOccurences");
String documentName=job.getString("documentName");
String phrase=job.getString("phrase");
//This i for Occurences array
ArrayList<PhraseModel.OccurenceDetails> occurencesList = new ArrayList<>();
JSONArray occurencesArray = job.getJSONArray("occurenceDetails");
for (int j = 0; j < occurencesArray.length(); j++) {
JSONObject jobIn = occurencesArray.getJSONObject(j);
int occurenceId=jobIn.getInt("occurenceId");
int pageNo=jobIn.getInt("pageNo");
String fullPhrase=jobIn.getString("fullPhrase");
occurencesList.add(new PhraseModel.OccurenceDetails(occurenceId, pageNo, fullPhrase));
}
//here your Phrase[] value store in pdfArrayList
phrase_Lists.add(new PhraseModel.PhraseList(documentId,documentName,phrase,noOfOccurences,occurencesList));
Log.i("phraseList size = ", ""+phraseArray.length());
}
if (phrase_Lists != null)
{
Log.i("phraseList size = ", ""+phraseArray.length());
}
return phrase_Lists;
}catch (JSONException e){
e.printStackTrace();
return null;
}
}
#Override
protected void onResume() {
super.onResume();
}
public String readJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("hipaJson.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
Logcat :
11-23 15:52:06.428 1519-2794/? E/ActivityManager: applyOptionsLocked: Unknown animationType=0
11-23 15:52:06.428 459-2412/? E/ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only
11-23 15:52:07.059 29210-29210/? E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so
11-23 15:52:07.059 29210-29210/? E/HAL: PATH2 /vendor/lib64/hw/gralloc.qcom.so
11-23 15:52:07.059 29210-29210/? E/HAL: PATH1 /system/lib64/hw/gralloc.qcom.so
11-23 15:52:07.059 29210-29210/? E/HAL: PATH3 /odm/lib64/hw/gralloc.msm8953.so
11-23 15:52:07.059 29210-29210/? E/HAL: PATH2 /vendor/lib64/hw/gralloc.msm8953.so
11-23 15:52:07.059 29210-29210/? E/HAL: PATH1 /system/lib64/hw/gralloc.msm8953.so
11-23 15:52:11.066 459-2412/? E/ANDR-PERF-MPCTL: Invalid profile no. 0, total profiles 0 only
11-23 15:52:11.152 29210-29210/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.hkcl.pdfsearchapp, PID: 29210
java.lang.ClassCastException: in.hkcl.pdfsearchapp.PhraseModel$PhraseList cannot be cast to java.util.ArrayList
at in.hkcl.pdfsearchapp.ExpandableListAdapter.getGroupView(ExpandableListAdapter.java:81)
at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
at android.widget.AbsListView.obtainView(AbsListView.java:2367)
at android.widget.ListView.makeAndAddView(ListView.java:1972)
at android.widget.ListView.fillDown(ListView.java:704)
at android.widget.ListView.fillFromTop(ListView.java:765)
at android.widget.ListView.layoutChildren(ListView.java:1744)
at android.widget.AbsListView.onLayout(AbsListView.java:2161)
at android.view.View.layout(View.java:17548)
at android.view.ViewGroup.layout(ViewGroup.java:5614)
Please explain and help, what i am doing wrong?? The app has splashscreen > landingActivity containing expandable list which i am not able to show as app crashes giving class cat exception in getGroupView method.
list_group.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:id="#+id/parent_phraseList_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#BDBDBD"
app:cardElevation="4dp"
android:padding="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:padding="5dp">
<TextView
android:id="#+id/docNameLabel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Doc Name : "
android:textStyle="bold"/>
<TextView
android:id="#+id/docName"
android:layout_toRightOf="#id/docNameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/totalOccurencesLabel"
android:layout_below="#id/docNameLabel"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Total Occurences : "
android:textStyle="bold"/>
<TextView
android:id="#+id/totalOccurences"
android:layout_toRightOf="#id/totalOccurencesLabel"
android:layout_below="#id/docNameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
<ImageButton
android:id="#+id/linkToDirectOpenDoc_btn"
android:layout_below="#id/totalOccurencesLabel"
android:layout_centerHorizontal="true"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/link"
android:elevation="6dp"
android:layout_marginTop="5dp"/>
<ImageButton
android:id="#+id/expandCard_btn"
android:layout_below="#id/totalOccurencesLabel"
android:layout_toRightOf="#id/linkToDirectOpenDoc_btn"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="#drawable/expand"
android:elevation="6dp"
android:layout_marginTop="5dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
list_item.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="4dp"
android:padding="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#FFFFFF">
<TextView
android:id="#+id/occurenceId_label"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Occurence : "
android:textStyle="bold"/>
<TextView
android:id="#+id/occurenceId"
android:layout_toRightOf="#id/occurenceId_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/fullPhrase_label"
android:layout_below="#id/occurenceId_label"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Full Phrase : "
android:textStyle="bold"
android:layout_marginTop="5dp"/>
<TextView
android:id="#+id/fullPhrase"
android:layout_below="#id/occurenceId_label"
android:layout_toRightOf="#id/fullPhrase_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
<TextView
android:id="#+id/pageNo_label"
android:layout_below="#id/fullPhrase_label"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Full Phrase : "
android:textStyle="bold"
android:layout_marginTop="5dp"/>
<TextView
android:id="#+id/pageNo"
android:layout_below="#id/fullPhrase_label"
android:layout_toRightOf="#id/pageNo_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Try replacing
ArrayList<PhraseModel.PhraseList> parentList = (ArrayList<PhraseModel.PhraseList>) getGroup(groupPosition);
by
PhraseList parentList = (PhraseList) getGroup(groupPosition);
This may help
//Change this
ArrayList<PhraseModel.PhraseList> parentList = (ArrayList<PhraseModel.PhraseList>) getGroup(groupPosition);
//To
PhraseModel.PhraseList mPhraseListItem = getGroup(groupPosition);
//AND getGroup return type to PhraseModel.PhraseList
public PhraseModel.PhraseList getGroup(int groupPosition) {
return this.mPhraseList.get(groupPosition);
}
Update As Per Requirement
I made Some Change In Your ExpandableListAdapter class. I made it as simple as possible. Just Copy and Paste This Class.
public class ExpandableListAdapter extends BaseExpandableListAdapter {
Context context;
ExpandableListView expandableListView;
private ArrayList<PhraseModel.PhraseList> mPhraseList;
private int lastExpandedPosition = -1;
public ExpandableListAdapter(Context context, ArrayList<PhraseModel.PhraseList> mPhraseList, ExpandableListView expandableListView) {
this.context = context;
this.mPhraseList = mPhraseList;
this.expandableListView = expandableListView;
}
public PhraseModel.OccurenceDetails getChild(int groupPosition, int childPosition){
return this.mPhraseList.get(groupPosition).getOccurenceDetails().get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final PhraseModel.OccurenceDetails occurenceDetails = 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 occurenceId,fullPhrase,pageNo;
occurenceId=(TextView)convertView.findViewById(R.id.occurenceId);
fullPhrase=(TextView)convertView.findViewById(R.id.fullPhrase);
pageNo=(TextView)convertView.findViewById(R.id.pageNo);
occurenceId.setText(String.valueOf(occurenceDetails.getOccurenceId()));
fullPhrase.setText(occurenceDetails.getFullPhrase());
pageNo.setText(String.valueOf(occurenceDetails.getPageNo()));
return convertView;
}
public int getChildrenCount(int groupPosition) {
return this.mPhraseList.get(groupPosition).getOccurenceDetails().size();
}
public PhraseModel.PhraseList getGroup(int groupPosition) {
return this.mPhraseList.get(groupPosition);
}
public int getGroupCount() {
return this.mPhraseList.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(final int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
PhraseModel.PhraseList parentList = getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView docName,totalOccurences;
docName=(TextView)convertView.findViewById(R.id.docName);
totalOccurences=(TextView)convertView.findViewById(R.id.totalOccurences);
docName.setText(parentList.getDocumentName());
totalOccurences.setText(String.valueOf(parentList.getNoOfOccurences()));
ImageButton linkToDirectOpenDoc_btn, expandCard_btn;
linkToDirectOpenDoc_btn = (ImageButton) convertView.findViewById(R.id.linkToDirectOpenDoc_btn);
linkToDirectOpenDoc_btn.setFocusable(false);
expandCard_btn = (ImageButton) convertView.findViewById(R.id.expandCard_btn);
expandCard_btn.setFocusable(false);
expandCard_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (lastExpandedPosition == groupPosition) {
expandableListView.collapseGroup(lastExpandedPosition);
lastExpandedPosition = -1;
} else {
expandableListView.collapseGroup(lastExpandedPosition);
lastExpandedPosition = groupPosition;
expandableListView.expandGroup(lastExpandedPosition);
}
}
});
return convertView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Then in your Activity put below code to set up adapters and array lists. This is just for your reference you have to make change it as per your need, so you can get this values from your assets.
//FIRST GROUP
PhraseModel.OccurenceDetails oneOccurenceDetails = new PhraseModel.OccurenceDetails(1, 20, "Occurence Details Pharse One");
ArrayList<PhraseModel.OccurenceDetails> firstOccurenceDetails = new ArrayList<>();
firstOccurenceDetails.add(oneOccurenceDetails);
PhraseModel.PhraseList onePhraseList = new PhraseModel.PhraseList(1,
"Doc One", "Pharse One", 10, firstOccurenceDetails);
//SECOND GROUP
PhraseModel.OccurenceDetails twoOccurenceDetails = new PhraseModel.OccurenceDetails(2, 30, "Occurence Details Pharse Second");
ArrayList<PhraseModel.OccurenceDetails> secondOccurenceDetails = new ArrayList<>();
secondOccurenceDetails.add(twoOccurenceDetails);
PhraseModel.PhraseList twoPhraseList = new PhraseModel.PhraseList(1,
"Doc Two", "Pharse Two", 10, secondOccurenceDetails);
ArrayList<PhraseModel.PhraseList> onePhraseArrayList = new ArrayList<>();
onePhraseArrayList.add(onePhraseList);
onePhraseArrayList.add(twoPhraseList);
ExpandableListAdapter expandableListAdapter = new ExpandableListAdapter(this, onePhraseArrayList, expandableListView);
expandableListView.setAdapter(expandableListAdapter);
Try this code,
public class ExpandableListMAdapter extends BaseExpandableListAdapter {
Context context;
public ArrayList<String> mPhraseList;
private HashMap<Integer,ArrayList<String>> occurenceList;
public ExpandableListMAdapter(Context context, ArrayList<String> mPhraseList, HashMap<Integer, ArrayList<String>> occurenceList) {
this.context = context;
this.mPhraseList = mPhraseList;
this.occurenceList = occurenceList;
}
public Object getChild(int groupPosition, int childPosition){
return this.occurenceList.get(this.mPhraseList.get(groupPosition)).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final ArrayList<String> childList = occurenceList.get(groupPosition);
if(convertView==null){
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtchild;
txtchild=(TextView)convertView.findViewById(R.id.txtchild);
txtchild.setText(childList.get(childPosition));
/* TextView occurenceId,fullPhrase,pageNo;
occurenceId=(TextView)convertView.findViewById(R.id.occurenceId);
fullPhrase=(TextView)convertView.findViewById(R.id.fullPhrase);
pageNo=(TextView)convertView.findViewById(R.id.pageNo);
occurenceId.setText(childList.get(groupPosition).getOccurenceId());
fullPhrase.setText(childList.get(groupPosition).getFullPhrase());
pageNo.setText(childList.get(groupPosition).getPageNo());*/
return convertView;
}
public int getChildrenCount(int groupPosition) {
return this.occurenceList.get(groupPosition).size(); }
public Object getGroup(int groupPosition) {
return this.mPhraseList.get(groupPosition);
}
public int getGroupCount() {
return this.mPhraseList.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
ArrayList<String> parentList = mPhraseList;
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView txtgroup;
txtgroup=(TextView)convertView.findViewById(R.id.txtgroup);
txtgroup.setText(parentList.get(groupPosition));
/* TextView docName,totalOccurences;
docName=(TextView)convertView.findViewById(R.id.docName);
totalOccurences=(TextView)convertView.findViewById(R.id.totalOccurences);
docName.setText(parentList.get(groupPosition).getDocumentName());
totalOccurences.setText(parentList.get(groupPosition).getNoOfOccurences());*/
return convertView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
I want to show the count and all the data when clicked. But so far in testing using click on the Parent item
I tried increment checkedCount when check. But it show only checked count in in one group child elemements.
My Adapter:
public class RubExpandAdapter extends BaseExpandableListAdapter {
private Context context;
private List<RubricsModel> rubrics;
private List<VacancyModel> listForRubricListView;
private List<VacancyModel> listForSubRubricListView;
private HashMap<Integer, boolean[]> mChildCheckStates;
public static int rubricPage = 1;
public static int rubricId;
public static int subRubricPage = 1;
public static int subRubricId;
int checkedCount;
FragmentManager fm;
public RubExpandAdapter(Context context, ArrayList<RubricsModel> rubrics, FragmentManager fragmentManager) {
this.context = context;
this.rubrics = rubrics;
fm = fragmentManager;
listForRubricListView = new ArrayList<>();
mChildCheckStates = new HashMap<>();
}
#Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList<SubRubricsModel> chList = rubrics.get(groupPosition)
.getItems();
return chList.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final int mGroupPosition = groupPosition;
final int mChildPosition = childPosition;
SubRubricsModel subRubricsModel = (SubRubricsModel) getChild(groupPosition,
childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.rubrics_child_row_layout, null);
}
final CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
TextView tv = (TextView) convertView.findViewById(R.id.tvsubrubricsfr);
tv.setText(subRubricsModel.getSubRubricName());
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkBox.setChecked(!checkBox.isChecked());
}
});
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
getChecked[mChildPosition] = isChecked;
mChildCheckStates.put(mGroupPosition, getChecked);
} else {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
getChecked[mChildPosition] = isChecked;
mChildCheckStates.put(mGroupPosition, getChecked);
}
for (int i = 0; i < mChildCheckStates.size(); i++){
if (mChildCheckStates.containsValue(true)){
checkedCount ++;
}
}
}
});
if (mChildCheckStates.containsKey(mGroupPosition)) {
boolean getChecked[] = mChildCheckStates.get(mGroupPosition);
checkBox.setChecked(getChecked[mChildPosition]);
} else {
boolean getChecked[] = new boolean[getChildrenCount(mGroupPosition)];
mChildCheckStates.put(mGroupPosition, getChecked);
checkBox.setChecked(false);
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
ArrayList<SubRubricsModel> chList = rubrics.get(groupPosition)
.getItems();
return chList.size();
}
#Override
public Object getGroup(int groupPosition) {
return rubrics.get(groupPosition);
}
#Override
public int getGroupCount() {
return rubrics.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
final RubricsModel rubricsModel = (RubricsModel) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.rubrics_group_row_layout, null);
}
TextView tv = (TextView) convertView.findViewById(R.id.tvrubricsfr);
tv.setText(rubricsModel.getRubricName());
ImageView indicator = (ImageView) convertView.findViewById(R.id.ivGroupIndicator);
if (isExpanded) {
indicator.setImageResource(R.drawable.arrow_down);
} else {
indicator.setImageResource(R.drawable.arrow_right);
}
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// RubFragment.searchByRubrics = true;
// rubricId = rubricsModel.getRubricID();
// searchByRubric(String.valueOf(rubricId));
Toast.makeText(context, String.valueOf(mChildCheckStates.size()), Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
how to do, tell me, pls?
I want to put button below ListView and have everything inside ScrollView(not only ListView should be scrollable but button too so if ListView has a lot of elements button is invisible and user has to scroll down to click it).
My xml is:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.plan.aplikacjamobilna.fragments.tabs.Allergies">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
<ListView
android:id="#+id/allergies_listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="3dp"
android:divider="#cccccc">
</ListView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
</ScrollView>
it looks like:
I'm not sure why it doesn't work, there is "match_parent" as layout_hight.
What shuld I change in my code?
my Adapter:
public class AllergiesAdapter extends ArrayAdapter{
List list = new ArrayList();
char[] keys;
public AllergiesAdapter(Context context, int resource) {
super(context, resource);
}
public static class DataHandler{
ImageView allergieIcon;
ImageView showDetailsIcon;
TextView allergieName;
FrameLayout detailsLayout;
RelativeLayout infoLayout;
CheckBox checkBox;
}
#Override
public void add(Object object) {
super.add(object);
list.add(object);
}
#Override
public int getCount() {
return this.list.size();
}
#Override
public Object getItem(int position) {
return this.list.get(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final DataHandler dataHandler;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.allergie_item,parent,false);
dataHandler = new DataHandler();
dataHandler.allergieIcon = (ImageView) view.findViewById(R.id.allerie_icon);
dataHandler.showDetailsIcon = (ImageView) view.findViewById(R.id.allergie_show_info);
dataHandler.allergieName =(TextView) view.findViewById(R.id.allerie_name);
dataHandler.detailsLayout=(FrameLayout)view.findViewById(R.id.allergie_frameLayout);
dataHandler.infoLayout=(RelativeLayout)view.findViewById(R.id.info_layout);
dataHandler.checkBox=(CheckBox)view.findViewById(R.id.allergie_checkbox);
view.setTag(dataHandler);
}
else{
dataHandler=(DataHandler) view.getTag();
}
LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final AllergiesDataProvider allergiesDataProvider;
allergiesDataProvider = (AllergiesDataProvider) this.getItem(position);
dataHandler.allergieIcon.setImageResource(allergiesDataProvider.getAllergieIcon());
dataHandler.showDetailsIcon.setImageResource(allergiesDataProvider.getShowDetailsIcon());
dataHandler.allergieName.setText(allergiesDataProvider.getAllergieName());
dataHandler.showDetailsIcon.setImageResource(allergiesDataProvider.getShowDetails());
allergiesData = getContext().getSharedPreferences("allergiesData", Context.MODE_PRIVATE);
allergiesEditor = allergiesData.edit();
String myKey = allergiesData.getString(ALLERIES_KEY,"00000000");
keys = myKey.toCharArray();
if(keys[position]=='0')
dataHandler.checkBox.setChecked(false);
else
dataHandler.checkBox.setChecked(true);
dataHandler.infoLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(getContext(),"no elo",Toast.LENGTH_LONG).show();
if (!allergiesDataProvider.isDetailsShows()) {
allergiesDataProvider.setIsDetailsShows(true);
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
dataHandler.detailsLayout.removeAllViews();
dataHandler.detailsLayout.addView(inflater.inflate(allergiesDataProvider.getDetailsLayout(), dataHandler.detailsLayout, false));
dataHandler.showDetailsIcon.setImageResource(allergiesDataProvider.getHindDetails());
} else {
allergiesDataProvider.setIsDetailsShows(false);
dataHandler.detailsLayout.removeAllViews();
dataHandler.showDetailsIcon.setImageResource(allergiesDataProvider.getShowDetails());
}
}
});
dataHandler.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
keys[position] = '1';
else
keys[position] = '0';
String myString = String.valueOf(keys);
allergiesEditor.putString(ALLERIES_KEY,myString).apply();
//Toast.makeText(getContext(),allergiesData.getString(ALLERIES_KEY,"Sd"),Toast.LENGTH_LONG).show();
}
});
return view;
}
}
You can use this function to set the height of list view based on your children.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
Example:
setListViewHeightBasedOnChildren(yourlistview);
A ListView is scrollable on it's own and should not be wrapped in a ScrollView. For your use, what you should do is to simply have the ListView as it is and have the button as the last view in the ListView. You will have to modify the getview() method in your adapter to achieve this. Based on the index, you will return a view that displays a Button.
I suggest you to use RecyclerView with footer as a button like this way,and also try HFRecyclerView
public class HeaderFooterAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private static final int TYPE_FOOTER = 2;
ArrayList<Generic> generics;
Context context;
public HeaderFooterAdapter(Context context, ArrayList<Generic> generics) {
this.context = context;
this.generics = generics;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder (ViewGroup parent, int viewType) {
if(viewType == TYPE_HEADER) {
View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.header_item, parent, false);
return new HeaderViewHolder (v);
} else if(viewType == TYPE_FOOTER) {
View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.footer_item, parent, false);
return new FooterViewHolder (v);
} else if(viewType == TYPE_ITEM) {
View v = LayoutInflater.from (parent.getContext ()).inflate (R.layout.list_item, parent, false);
return new GenericViewHolder (v);
}
return null;
}
private Generic getItem (int position) {
return generics.get (position);
}
#Override
public void onBindViewHolder (RecyclerView.ViewHolder holder, int position) {
if(holder instanceof HeaderViewHolder) {
HeaderViewHolder headerHolder = (HeaderViewHolder) holder;
headerHolder.txtTitleHeader.setText ("Header");
headerHolder.txtTitleHeader.setOnClickListener (new View.OnClickListener () {
#Override
public void onClick (View view) {
Toast.makeText (context, "Clicked Header", Toast.LENGTH_SHORT).show ();
}
});
} else if(holder instanceof FooterViewHolder) {
FooterViewHolder footerHolder = (FooterViewHolder) holder;
footerHolder.txtTitleFooter.setText ("Footer");
footerHolder.txtTitleFooter.setOnClickListener (new View.OnClickListener () {
#Override
public void onClick (View view) {
Toast.makeText (context, "Clicked Footer", Toast.LENGTH_SHORT).show ();
}
});
} else if(holder instanceof GenericViewHolder) {
Generic currentItem = getItem (position - 1);
GenericViewHolder genericViewHolder = (GenericViewHolder) holder;
genericViewHolder.txtName.setText (currentItem.getName ());
}
}
// need to override this method
#Override
public int getItemViewType (int position) {
if(isPositionHeader (position)) {
return TYPE_HEADER;
} else if(isPositionFooter (position)) {
return TYPE_FOOTER;
}
return TYPE_ITEM;
}
private boolean isPositionHeader (int position) {
return position == 0;
}
private boolean isPositionFooter (int position) {
return position == generics.size () + 1;
}
#Override
public int getItemCount () {
return generics.size () + 2;
}
class FooterViewHolder extends RecyclerView.ViewHolder {
TextView txtTitleFooter;
public FooterViewHolder (View itemView) {
super (itemView);
this.txtTitleFooter = (TextView) itemView.findViewById (R.id.txtFooter);
}
}
class HeaderViewHolder extends RecyclerView.ViewHolder {
TextView txtTitleHeader;
public HeaderViewHolder (View itemView) {
super (itemView);
this.txtTitleHeader = (TextView) itemView.findViewById (R.id.txtHeader);
}
}
class GenericViewHolder extends RecyclerView.ViewHolder {
TextView txtName;
public GenericViewHolder (View itemView) {
super (itemView);
this.txtName = (TextView) itemView.findViewById (R.id.txtName);
}
}
}
I want to add one more level of in ExpandableListView.In current ExpandableListView is two level how can i add one more level. I am new in android developing please help me. Thanks in advance!
My MainActivity.java:-
private Toolbar mToolbar;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
// private FragmentDrawer drawerFragment;
ArrayList<String> arraylist1,arraylist2,arraylist3,arrayList4,arrayList5;
int cnt = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
/* drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);*/
// display the first navigation drawer view on app launch
// displayView(0);
Log.d("oncreate method", "");
arraylist1 = new ArrayList<String>();
arraylist2 = new ArrayList<String>();
arraylist3 = new ArrayList<String>();
arrayList4 = new ArrayList<String>();
arrayList5 = new ArrayList<String>();
new ProductsAsynTask().execute("http://opencart.codeniques.com/shopping/?route=feed/web_api/menu&key=test123$");
}
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
}
public class ProductsAsynTask extends AsyncTask<String,Void,Void>{
ProgressDialog dialog;
protected void onPreExecute(){
super.onPreExecute();
Log.d("In onPreExceute","");
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Loading, Please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
protected Void doInBackground(String... param){
try{
Log.d("In doInBackground","");
HttpClient client= new DefaultHttpClient();
HttpPost post = new HttpPost(param[0]);
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
if(status == 200){
Log.d("Status",""+status);
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(data);
JSONArray jArray = jsonObject.getJSONArray("categories");
for(int i = 0;i < jArray.length();i++){
cnt++;
Log.d("value of array",jArray.length()+"");
Log.d("Value of i",""+i);
JSONObject jsonObject1 = jArray.getJSONObject(i);
arraylist1.add(jsonObject1.getString("name"));
//data1 = jsonObject1.getString("name");
// Log.d("hello ",data1);
JSONArray jsonArray = jsonObject1.getJSONArray("children");
// JSONObject jsonObject2 = jsonObject1.getJSONObject("children");
for(int j=0;j<jsonArray.length();j++){
JSONObject jsonObject2 = jsonArray.getJSONObject(j);
arraylist2.add(jsonObject2.getString("name"));
// data2 = jsonObject2.getString("name");
JSONArray jsonArray1 = jsonObject2.getJSONArray("children_lv3");
for(int k=0;k<jsonArray1.length();k++){
JSONObject jsonObject3 = jsonArray1.getJSONObject(k);
arraylist3.add(jsonObject3.getString("name"));
arrayList4.add(jsonObject3.getString("href"));
/* data3 = jsonObject3.getString("name");
data4 = jsonObject3.getString("href"); */
}
arrayList5.add(jsonObject2.getString("href"));
// data5 = jsonObject2.getString("href");
}
}
}
}catch(IOException e){
Log.e("Error IOException :",e.getMessage());
}catch (JSONException e){
Log.e("Error JSONException",e.getMessage());
}
return null;
}
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
dialog.dismiss();
Log.d("Counter value",""+cnt);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if(id == R.id.action_search){
Toast.makeText(getApplicationContext(), "Search action is selected!", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
/* public void onDrawerItemSelected(View view, int position) {
displayView(position);
}
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new HomeFragment();
title = getString(R.string.title_home);
break;
case 1:
fragment = new FriendsFragment();
title = getString(R.string.title_friends);
break;
case 2:
fragment = new MessagesFragment();
title = getString(R.string.title_messages);
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
}*/}
My ExpandableListAdapter.java is here:-
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;
}}
My activity_main.xml is here:-
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<ExpandableListView
android:id="#+id/lvExp"
android:layout_height="match_parent"
android:layout_width="260dp"
android:layout_gravity="start"/>
<!-- <fragment
android:id="#+id/fragment_navigation_drawer"
android:name="com.android.ShoppingMazza.activity.ExpandableListAdapter"
android:layout_width="#dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" /> -->
</android.support.v4.widget.DrawerLayout>
You can try my following sample code. I have posted my full project to GitHub
Of course, you should modify more to meet all your requirements. For basic case, I only use the data source in the arrays.xml file. Hope this helps!
arrays.xml:
<resources>
<string-array name="items_array_expandable_level_one">
<item>Level 1.1</item>
<item>Level 1.2</item>
<item>Level 1.3</item>
</string-array>
<string-array name="items_array_expandable_level_one_one_child">
<item>Level 1.1.1</item>
<item>Level 1.1.2</item>
</string-array>
<string-array name="items_array_expandable_level_one_two_child">
<item>Level 1.2.1</item>
</string-array>
<string-array name="items_array_expandable_other_child">
<item>Second Level 01</item>
<item>Second Level 02</item>
<item>Second Level 03</item>
</string-array>
<string-array name="items_array_expandable_level_three">
<item>Child Level 01</item>
<item>Child Level 02</item>
</string-array>
</resources>
CustomExpListView.java:
public class CustomExpListView extends ExpandableListView
{
public CustomExpListView(Context context)
{
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
ParentLevelAdapter.java:
public class ParentLevelAdapter extends BaseExpandableListAdapter {
private final Context mContext;
private final List<String> mListDataHeader;
private final Map<String, List<String>> mListData_SecondLevel_Map;
private final Map<String, List<String>> mListData_ThirdLevel_Map;
public ParentLevelAdapter(Context mContext, List<String> mListDataHeader) {
this.mContext = mContext;
this.mListDataHeader = new ArrayList<>();
this.mListDataHeader.addAll(mListDataHeader);
// Init second level data
String[] mItemHeaders;
mListData_SecondLevel_Map = new HashMap<>();
int parentCount = mListDataHeader.size();
for (int i = 0; i < parentCount; i++) {
String content = mListDataHeader.get(i);
switch (content) {
case "Level 1.1":
mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_one_one_child);
break;
case "Level 1.2":
mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_level_one_two_child);
break;
default:
mItemHeaders = mContext.getResources().getStringArray(R.array.items_array_expandable_other_child);
}
mListData_SecondLevel_Map.put(mListDataHeader.get(i), Arrays.asList(mItemHeaders));
}
// THIRD LEVEL
String[] mItemChildOfChild;
List<String> listChild;
mListData_ThirdLevel_Map = new HashMap<>();
for (Object o : mListData_SecondLevel_Map.entrySet()) {
Map.Entry entry = (Map.Entry) o;
Object object = entry.getValue();
if (object instanceof List) {
List<String> stringList = new ArrayList<>();
Collections.addAll(stringList, (String[]) ((List) object).toArray());
for (int i = 0; i < stringList.size(); i++) {
mItemChildOfChild = mContext.getResources().getStringArray(R.array.items_array_expandable_level_three);
listChild = Arrays.asList(mItemChildOfChild);
mListData_ThirdLevel_Map.put(stringList.get(i), listChild);
}
}
}
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final CustomExpListView secondLevelExpListView = new CustomExpListView(this.mContext);
String parentNode = (String) getGroup(groupPosition);
secondLevelExpListView.setAdapter(new SecondLevelAdapter(this.mContext, mListData_SecondLevel_Map.get(parentNode), mListData_ThirdLevel_Map));
secondLevelExpListView.setGroupIndicator(null);
return secondLevelExpListView;
}
#Override
public int getChildrenCount(int groupPosition) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return this.mListDataHeader.get(groupPosition);
}
#Override
public int getGroupCount() {
return this.mListDataHeader.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 layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.drawer_list_group, parent, false);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setTextColor(Color.CYAN);
lblListHeader.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
} }
SecondLevelAdapter.java:
public class SecondLevelAdapter extends BaseExpandableListAdapter
{
private final Context mContext;
private final List<String> mListDataHeader;
private final Map<String, List<String>> mListDataChild;
public SecondLevelAdapter(Context mContext, List<String> mListDataHeader, Map<String, List<String>> mListDataChild) {
this.mContext = mContext;
this.mListDataHeader = mListDataHeader;
this.mListDataChild = mListDataChild;
}
#Override
public Object getChild(int groupPosition, int childPosition)
{
return this.mListDataChild.get(this.mListDataHeader.get(groupPosition))
.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.drawer_list_item, parent, false);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
txtListChild.setText(childText);
return convertView;
}
#Override
public int getChildrenCount(int groupPosition)
{
try {
return this.mListDataChild.get(this.mListDataHeader.get(groupPosition)).size();
} catch (Exception e) {
return 0;
}
}
#Override
public Object getGroup(int groupPosition)
{
return this.mListDataHeader.get(groupPosition);
}
#Override
public int getGroupCount()
{
return this.mListDataHeader.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 layoutInflater = (LayoutInflater) this.mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.drawer_list_group_second, parent, false);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setText(headerTitle);
lblListHeader.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
lblListHeader.setTextColor(Color.YELLOW);
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
// Init top level data
List<String> listDataHeader = new ArrayList<>();
String[] mItemHeaders = getResources().getStringArray(R.array.items_array_expandable_level_one);
Collections.addAll(listDataHeader, mItemHeaders);
ExpandableListView mExpandableListView = (ExpandableListView) findViewById(R.id.expandableListView_Parent);
if (mExpandableListView != null) {
ParentLevelAdapter parentLevelAdapter = new ParentLevelAdapter(this, listDataHeader);
mExpandableListView.setAdapter(parentLevelAdapter);
}
}
}
Screenshot result:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ExpandableListView
android:layout_width="fill_parent"
android:id="#+id/ParentLevel"
android:groupIndicator="#null"
android:layout_height="fill_parent">
</ExpandableListView>
</LinearLayout>
MainActivity.Java
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class Home extends Activity
{
ExpandableListView explvlist;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
explvlist = (ExpandableListView)findViewById(R.id.ParentLevel);
explvlist.setAdapter(new ParentLevel());
}
public class ParentLevel extends BaseExpandableListAdapter
{
#Override
public Object getChild(int arg0, int arg1)
{
return arg1;
}
#Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent)
{
CustExpListview SecondLevelexplv = new CustExpListview(Home.this);
SecondLevelexplv.setAdapter(new SecondLevelAdapter());
SecondLevelexplv.setGroupIndicator(null);
return SecondLevelexplv;
}
#Override
public int getChildrenCount(int groupPosition)
{
return 3;
}
#Override
public Object getGroup(int groupPosition)
{
return groupPosition;
}
#Override
public int getGroupCount()
{
return 5;
}
#Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
TextView tv = new TextView(Home.this);
tv.setText("->FirstLevel");
tv.setBackgroundColor(Color.BLUE);
tv.setPadding(10, 7, 7, 7);
return tv;
}
#Override
public boolean hasStableIds()
{
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
public class CustExpListview extends ExpandableListView
{
int intGroupPosition, intChildPosition, intGroupid;
public CustExpListview(Context context)
{
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST);
heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class SecondLevelAdapter extends BaseExpandableListAdapter
{
#Override
public Object getChild(int groupPosition, int childPosition)
{
return childPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
TextView tv = new TextView(Home.this);
tv.setText("child");
tv.setPadding(15, 5, 5, 5);
tv.setBackgroundColor(Color.YELLOW);
tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return tv;
}
#Override
public int getChildrenCount(int groupPosition)
{
return 5;
}
#Override
public Object getGroup(int groupPosition)
{
return groupPosition;
}
#Override
public int getGroupCount()
{
return 1;
}
#Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent)
{
TextView tv = new TextView(Home.this);
tv.setText("-->Second Level");
tv.setPadding(12, 7, 7, 7);
tv.setBackgroundColor(Color.RED);
return tv;
}
#Override
public boolean hasStableIds()
{
// TODO Auto-generated method stub
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return true;
}
}
}
In this case you should create your own Expandable class.
Refer this link.
http://androidcodesnips.blogspot.in/2011/09/three-level-expandable-list.html
Create one more expandable list adapter extending BaseExpandableListAdapter, and in your ExpandableListAdapter getChildView(), call the new expandable list adapter.
I have implemented the same using following link:
https://github.com/talhahasanzia/Three-Level-Expandable-Listview