I have an ExpandableListView with a contextmenu. It doesn't matter how many group items I have, it always returns 0.
My code:
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
int group = ExpandableListView.getPackedPositionGroup((int) info.packedPosition);
int type = ExpandableListView.getPackedPositionType((int) info.packedPosition);
int child = ExpandableListView.getPackedPositionChild((int) info.packedPosition);
Log.d("Value ConTEXT: " + String.valueOf(group), String.valueOf(child) + " GROUP COUNT " + getExpandableListAdapter().getGroupCount());
menu.setHeaderTitle(((Person) dAdapter.getGroup(group)).getName());
// menu.add(0, 0, 0, "Geschiedenis verwijderen");
menu.add(1, 1, 1, "Verwijderen uit lijst");
}
And my custom base list adapter:
public class DrinkActionAdapter extends BaseExpandableListAdapter {
Activity context;
ArrayList<Person> personsGroup;
ArrayList<Person> personsChild;
public DrinkActionAdapter(Activity context, ArrayList<Person> personsGroup,
ArrayList<Person> personsChild) {
this.context = context;
this.personsGroup = personsGroup;
this.personsChild = personsChild;
}
public void addPerson(Person p) {
personsGroup.add(p);
personsChild.add(p);
for (Person pp : personsGroup) {
Log.d(pp.getName(), pp.getName());
}
}
public Object getChild(int groupPosition, int childPosition) {
Log.d("GetChild" + String.valueOf(groupPosition), String.valueOf(childPosition));
return personsGroup.get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
Log.d("GetChildID" + String.valueOf(groupPosition), String.valueOf(childPosition));
return childPosition;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Person p = personsChild.get(groupPosition);
View v = convertView;
Log.d(p.getName(), p.getName() + "CHILD");
if (v == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = mInflater.inflate(R.layout.drink_action_child, null);
}
if (personsChild.size() != 0) {
TextView lbFavoriteDrink = (TextView) v.findViewById(R.id.drink_action_child_lb_favorite_drink);
if (p.getFavoriteDrink() != null) {
lbFavoriteDrink.setText(p.getFavoriteDrink().getName());
} else {
lbFavoriteDrink.setText("Nog niks gedronken.");
}
TextView lbCost = (TextView) v.findViewById(R.id.drink_action_child_lb_cost);
lbCost.setText(String.valueOf(p.getTotalMoneySpent()));
TextView lbAmount = (TextView) v.findViewById(R.id.drink_action_child_lb_amount);
lbAmount.setText(String.valueOf(p.getTotalAmountOfDrinks()));
TextView lbKcal = (TextView) v.findViewById(R.id.drink_action_child_lb_kcal);
lbKcal.setText(String.valueOf(p.getTotalKcal()));
TextView lbPunishment = (TextView) v.findViewById(R.id.drink_action_child_lb_boete);
lbPunishment.setText(p.getPunishment());
TextView lbWeightPutOn = (TextView) v.findViewById(R.id.drink_action_child_lb_weight);
lbWeightPutOn.setText(String.valueOf(p.getWeightPutOn()));
TextView lbLiters = (TextView) v.findViewById(R.id.drink_action_child_lb_liter);
lbLiters.setText(String.valueOf(p.getAmountOfLiters()));
TextView lbTimeSinceLastDrink = (TextView) v.findViewById(R.id.drink_action_child_lb_time_since_last_drink);
lbTimeSinceLastDrink.setText(String.valueOf(p.getTimeSinceLast()[2]) + ":" + String.valueOf(p.getTimeSinceLast()[1]) + ":" + String.valueOf(p.getTimeSinceLast()[0]));
} else {
// WHAT TO DO IF NO PROFILES
}
return v;
}
public int getChildrenCount(int groupPosition) {
Log.d("GetChildrenCount" + String.valueOf(groupPosition), String.valueOf(groupPosition));
return 1;
// return persons.size();
}
public Object getGroup(int groupPosition) {
Log.d(String.valueOf(groupPosition), String.valueOf(groupPosition) + personsGroup.get(groupPosition).getName());
return personsGroup.get(groupPosition);
}
public int getGroupCount() {
return personsGroup.size();
}
public long getGroupId(int groupPosition) {
Log.d(String.valueOf(groupPosition) + "GetGroupId", String.valueOf(groupPosition));
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Person p = personsGroup.get(groupPosition);
View v = convertView;
Log.d(groupPosition + " " + p.getName(), p.getName() + "GROUP");
if (v == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = mInflater.inflate(R.layout.drink_action_item, null);
}
if (personsGroup.size() != 0) {
TextView lbName = (TextView) v.findViewById(R.id.drink_action_lb_name);
TextView lbAlcohol = (TextView) v.findViewById(R.id.drink_action_info_current_alcohol);
ImageView btnDrink = (ImageView) v.findViewById(R.id.drink_action_item_btn_drink);
btnDrink.setTag(p);
lbName.setText(p.getName());
lbAlcohol.setText(String.valueOf(p.getCurrentAlcoholLevel()));
} else {
// WHAT TO DO IF NO PROFILES
}
return v;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
Whenever I longclick on an item, it always selects the first one.
Try to register to the contextMenu in the getChildView() method just before return v; with this code ((Activity) context).registerForContextMenu(v);
Related
I am using Exapandable listview inside a fragment,i am passing 8 values but it is showing only 6 values and that too the exapnadable listview is not scrolling.the same if i put it in activity..everything works fine.but when i use within fragment it is showing error.can anyone tell me how to solve it...here is my code
public class GroupsFragment extends Fragment {
View groupview;
List<String> tdp, congress, ysrcp, bjp;
ExpandableListView expandableListView;
HashMap<String, ArrayList<GroupModel>> expandableListDetail;
ArrayList<GroupModel> contacts;
List<String> expandableListTitle;
ExpandableListAdapter expandableListAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the dailog_layout for this fragment
groupview = inflater.inflate(R.layout.contacts_expandable_list, container, false);
expandableListView = groupview.findViewById(R.id.expanded_list_view_contacts);
contacts = new ArrayList<>();
return groupview;
}
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
expandableListDetail = new HashMap<String, ArrayList<GroupModel>>();
showContactsList();
}
#SuppressLint("ClickableViewAccessibility")
private void showContactsList() {
getExpandableListItems();
expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
expandableListAdapter = new CustomContactsExapandableListAdapter(getActivity(), expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
}
private HashMap<String, ArrayList<GroupModel>> getExpandableListItems() {
contacts.add(new GroupModel("gopi", "Officer", "9999999999", "abcd0"));
expandableListDetail.put("TDP", contacts);
expandableListDetail.put("YSRCP", contacts);
expandableListDetail.put("Congress", contacts);
expandableListDetail.put("BJP", contacts);
expandableListDetail.put("ABC", contacts);
expandableListDetail.put("CDE", contacts);
expandableListDetail.put("DEF", contacts);
expandableListDetail.put("FED", contacts);
return expandableListDetail;
}
}
and here is the adapter
public class CustomContactsExapandableListAdapter extends BaseExpandableListAdapter {
private Context context;
private List<String> expandableListTitle;
private HashMap<String, ArrayList<GroupModel>> expandableListDetail;
public CustomContactsExapandableListAdapter(Context context, List<String> expandableListTitle,
HashMap<String, ArrayList<GroupModel>> expandableListDetail) {
this.context = context;
this.expandableListTitle = expandableListTitle;
this.expandableListDetail = expandableListDetail;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return this.expandableListDetail.get(this.expandableListTitle.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) {
Log.i("test", "the value in exapandable is " + getChild(groupPosition, childPosition));
final GroupModel expandedListText = (GroupModel) getChild(groupPosition, childPosition);
Log.i("test", "the text is " + expandedListText.getName()+ " "+ expandedListText.getDesignation() + " " +expandedListText.getMobile() );
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.single_contact, null);
}
TextView expandedListTextViewName = (TextView) convertView
.findViewById(R.id.contact_name);
TextView expandedListTextViewMobile = (TextView) convertView
.findViewById(R.id.contact_mobile_num);
expandedListTextViewMobile.setText(expandedListText.getDesignation());
expandedListTextViewName.setText(expandedListText.getName());
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return this.expandableListDetail.get(this.expandableListTitle.get(groupPosition))
.size();
}
#Override
public Object getGroup(int groupPosition) {
return this.expandableListTitle.get(groupPosition);
}
#Override
public int getGroupCount() {
Log.i("test", "the value in exapandable group is " + expandableListTitle.size());
return expandableListTitle.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String listTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.single_party, null);
}
TextView listTitleTextView = (TextView) convertView
.findViewById(R.id.party_name);
ImageView imageView=convertView.findViewById(R.id.party_expandable_image);
if(isExpanded){
imageView.setImageResource(R.drawable.minus_icon_contacts);
}
else {
imageView.setImageResource(R.drawable.plus_icon_contacts);
}
listTitleTextView.setTypeface(null, Typeface.BOLD);
listTitleTextView.setText(listTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Good day!
I'm a newbie at android/java programming and I'm trying to create, kinda like, a history function to my app. I'm using an expandableListView with a gridView (or listView; I don't really know what else to use aside from these two) inside to show said history. All the data comes from sqLite database which has 3 tables named Requests, Properties & ReqLine all linked together.
The format I'm trying to create is something like this (Sorry, cant post images yet):
HEADER: TextView ----- data from db 1st table
CHILD: TextView ----- data from 2nd GRID: 1)item from DB 3rd table 2)item
from DB 3rd table
I was able to populate the header and child portions but I'm having trouble with the child gridView repeating data on every child. I know it's with the way I'm passing the ArrayList to the GridView Adapter but I don't the correct way of doing it or how to implement it.
I tried a bunch of ways including adding another arraylist to my header class model but I don't know how I should get the childcount for it in my adapter.
I've seen lots on posts here in SO but none of them seems to have the solution to my problem. Can anyone help me with this? Thanks in advance! Suggestions on revisions are welcome as well.
Here's my code for the history fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.history_tab_layout, container, false);
openHelper = new DatabaseHelper(getActivity());
myDb = openHelper.getReadableDatabase();
TextView _number = (TextView) rootView.findViewById(R.id.lblNumber);
TextView _date = (TextView) rootView.findViewById(R.id.lblHistoryDate);
TextView _transtype = (TextView) rootView.findViewById(R.id.lblHistoryTransType);
TextView _amount = (TextView) rootView.findViewById(R.id.lblHistoryAmount);
Cursor mCursor = null;
Cursor dataCursor = null;
Cursor itemCursor = null;
String Query = "SELECT * FROM Requests";
mCursor = myDb.rawQuery(Query, null);
transHistory = new ArrayList<HistoryHeader>();
gridHistory = new ArrayList<GridItems>();
int count = 1;
if(mCursor.getCount()!=0) {
while (mCursor.moveToNext()) {
String reqDate = mCursor.getString(mCursor.getColumnIndex("RequestDate"));
String reqTransType = mCursor.getString(mCursor.getColumnIndex("TransType"));
String reqTotalAmt = mCursor.getString(mCursor.getColumnIndex("AmtTotal"));
String reqPCode = mCursor.getString(mCursor.getColumnIndex("Property"));
String BaseId = mCursor.getString(0);
HistoryHeader history = new HistoryHeader(count, reqDate, reqTransType, reqTotalAmt);
String Query2 = "SELECT * FROM Projects WHERE PrjCode = '"+ mCursor.getString(3) +"'";
dataCursor = myDb.rawQuery(Query2, null);
if (dataCursor.getCount()!=0){
while (dataCursor.moveToNext()){
String reqPName = dataCursor.getString(dataCursor.getColumnIndex("PrjName"));
history.setItemList(createItems(reqPCode, reqPName, 1));
}
}
String Query3 = "SELECT * FROM ReqLine WHERE Base_Id = "+ BaseId;
itemCursor = myDb.rawQuery(Query3, null);
if (itemCursor.getCount()!=0) {
while (itemCursor.moveToNext()) {
String reqPurpose = itemCursor.getString(itemCursor.getColumnIndex("Purpose"));
String reqAmount = itemCursor.getString(itemCursor.getColumnIndex("AmtLine"));
Integer reqNum = itemCursor.getInt(itemCursor.getColumnIndex("Linenum"));
GridItems test = new GridItems(reqNum, reqPurpose, reqAmount);
gridHistory.add(test);
}
}
transHistory.add(history);
count++;
}
}
final ExpandableListView _Content = (ExpandableListView) rootView.findViewById(R.id.historyList);
_Content.setIndicatorBounds(5,5);
HistoryAdapter exAdpt = new HistoryAdapter(getActivity(), transHistory, gridHistory);
_Content.setIndicatorBounds(0,20);
_Content.setAdapter(exAdpt);
return rootView;
}
private List<HistoryDetail> createItems(String _strPropertyCode, String _strPropertyName, int num) {
List<HistoryDetail> result = new ArrayList<HistoryDetail>();
for (int i=0; i < num; i++) {
HistoryDetail item = new HistoryDetail(i, _strPropertyCode, _strPropertyName);
result.add(item);
}
return result;
}
And the Code for my HistoryAdapter.java
public class HistoryAdapter extends BaseExpandableListAdapter {
private Context context;
private List<HistoryHeader> _listDataHeader;
private ArrayList<GridItems> _listGridItems;
public HistoryAdapter(Context context, List<HistoryHeader> _listDataHeader, ArrayList<GridItems> _listGridItems) {
this.context = context;
this._listDataHeader = _listDataHeader;
this._listGridItems = _listGridItems;
}
#Override
public int getGroupCount() {
return _listDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
/*Integer size = _listDataHeader.get(groupPosition).getItemList().size();
return size;*/
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return _listDataHeader.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return _listDataHeader.get(groupPosition).getItemList().get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return _listDataHeader.get(groupPosition).hashCode();
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return _listDataHeader.get(groupPosition).getItemList().get(childPosition).hashCode();
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.history_list_group, null);
}
TextView groupNum = (TextView) v.findViewById(R.id.historyNumber);
TextView groupDate = (TextView) v.findViewById(R.id.historyDate);
TextView groupTransType = (TextView) v.findViewById(R.id.historyTransType);
TextView groupAmount = (TextView) v.findViewById(R.id.historyTotalAmt);
HistoryHeader header = _listDataHeader.get(groupPosition);
groupNum.setText(String.valueOf(header.getId()));
groupDate.setText(header.getDate());
groupTransType.setText(header.getTransType());
groupAmount.setText(header.getTotalAmt());
return v;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup viewGroup) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.history_list_child, null);
}
TextView itemPropCode = (TextView) v.findViewById(R.id.historyPropertyCode);
TextView itemPropName = (TextView) v.findViewById(R.id.historyPropertyName);
GridView itemGrid = (GridView) v.findViewById(R.id.historyItemList);
ItemGridAdapter adapter = new ItemGridAdapter(context,_listGridItems);
itemGrid.setAdapter(adapter);
HistoryDetail detail = _listDataHeader.get(groupPosition).getItemList().get(childPosition);
itemPropCode.setText(detail.getPropertyCode());
itemPropName.setText(detail.getPropertyName());
return v;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
Then the ItemGridAdapter.java
public class ItemGridAdapter extends BaseAdapter {
Context context;
ArrayList<GridItems> itemList;
public ItemGridAdapter(Context context, ArrayList<GridItems> itemList) {
this.context = context;
this.itemList = itemList;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return itemList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.history_grid_layout, null);
}
TextView _rowPurpose = (TextView) convertView.findViewById(R.id.rowPurpose);
TextView _rowAmount = (TextView) convertView.findViewById(R.id.rowAmount);
GridItems gridItems = itemList.get(position);
_rowPurpose.setText(gridItems.getPurpose());
_rowAmount.setText(gridItems.getAmount());
return convertView;
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
// Return true for clickable, false for not
return false;
}
Model Classes
public class HistoryHeader implements Serializable {
private long id;
private String _strDate;
private String _strTransType;
private String _strTotalAmt;
private List<HistoryDetail> itemDetails = new ArrayList<HistoryDetail>();
private List<GridItems> itemGrid = new ArrayList<>();
public HistoryHeader(long id, String _strDate, String _strTransType, String _strTotalAmt) {
this.id = id;
this._strDate = _strDate;
this._strTransType = _strTransType;
this._strTotalAmt = _strTotalAmt;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getDate() {
return _strDate;
}
public void setDate(String _strDate) {
this._strDate = _strDate;
}
public String getTransType() {
return _strTransType;
}
public void setTransType(String _strTransType) {
this._strTransType = _strTransType;
}
public String getTotalAmt() {
return _strTotalAmt;
}
public void setTotalAmt(String _strTotalAmt) {
this._strTotalAmt = _strTotalAmt;
}
public List<HistoryDetail> getItemList() {
return itemDetails;
}
public void setItemList(List<HistoryDetail> itemDetails) {
this.itemDetails = itemDetails;
}
public List<GridItems> getItemGrid(){ return itemGrid; }
public void setItemGrid(List<GridItems> itemGrid) { this.itemGrid = itemGrid; }
public class GridItems {
private long id;
private String _strPurpose, _strAmount;
public GridItems(long id, String _strPurpose, String _strAmount) {
this.id = id;
this._strPurpose = _strPurpose;
this._strAmount = _strAmount;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPurpose() {
return _strPurpose;
}
public void setPurpose(String _strPurpose) {
this._strPurpose = _strPurpose;
}
public String getAmount() {
return _strAmount;
}
public void setAmount(String _strAmount) {
this._strAmount = _strAmount;
}
Try this:
Cursor mCursor = null;
Cursor dataCursor = null;
Cursor itemCursor = null;
String Query = "SELECT * FROM Requests";
mCursor = myDb.rawQuery(Query, null);
transHistory = new ArrayList<HistoryHeader>();
//allGridHistory = new ArrayList<ArrayList<GridItems>>(); // Changed2
//gridHistory = new ArrayList<GridItems>();
int count = 1;
if(mCursor.getCount()!=0) {
while (mCursor.moveToNext()) {
String reqDate = mCursor.getString(mCursor.getColumnIndex("RequestDate"));
String reqTransType = mCursor.getString(mCursor.getColumnIndex("TransType"));
String reqTotalAmt = mCursor.getString(mCursor.getColumnIndex("AmtTotal"));
String reqPCode = mCursor.getString(mCursor.getColumnIndex("Property"));
String BaseId = mCursor.getString(0);
HistoryHeader history = new HistoryHeader(count, reqDate, reqTransType, reqTotalAmt);
String Query2 = "SELECT * FROM Projects WHERE PrjCode = '"+ mCursor.getString(3) +"'";
dataCursor = myDb.rawQuery(Query2, null);
if (dataCursor.getCount()!=0){
while (dataCursor.moveToNext()){
String reqPName = dataCursor.getString(dataCursor.getColumnIndex("PrjName"));
history.setItemList(createItems(reqPCode, reqPName, 1));
}
}
String Query3 = "SELECT * FROM ReqLine WHERE Base_Id = "+ BaseId;
itemCursor = myDb.rawQuery(Query3, null);
if (itemCursor.getCount()!=0) {
gridHistory = new ArrayList<GridItems>(); // Added
while (itemCursor.moveToNext()) {
String reqPurpose = itemCursor.getString(itemCursor.getColumnIndex("Purpose"));
String reqAmount = itemCursor.getString(itemCursor.getColumnIndex("AmtLine"));
Integer reqNum = itemCursor.getInt(itemCursor.getColumnIndex("Linenum"));
GridItems test = new GridItems(reqNum, reqPurpose, reqAmount);
gridHistory.add(test);
}
history.setItemGrid(gridHistory); // Changed2
}
transHistory.add(history);
count++;
}
}
final ExpandableListView _Content = (ExpandableListView) rootView.findViewById(R.id.historyList);
_Content.setIndicatorBounds(5,5);
HistoryAdapter exAdpt = new HistoryAdapter(getActivity(), transHistory); // Changed2
The adapter:
public class HistoryAdapter extends BaseExpandableListAdapter {
private Context context;
private List<HistoryHeader> _listDataHeader;
//private ArrayList<GridItems> _listGridItems;
public HistoryAdapter(Context context, List<HistoryHeader> _listDataHeader) {
this.context = context;
this._listDataHeader = _listDataHeader;
//this._listGridItems = _listGridItems;
}
#Override
public int getGroupCount() {
return _listDataHeader.size();
}
#Override
public int getChildrenCount(int groupPosition) {
/*Integer size = _listDataHeader.get(groupPosition).getItemList().size();
return size;*/
return 1;
}
#Override
public HistoryHeader getGroup(int groupPosition) {
return _listDataHeader.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return _listDataHeader.get(groupPosition).getItemList().get(childPosition);
}
#Override
public long getGroupId(int groupPosition) {
return _listDataHeader.get(groupPosition).hashCode();
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return _listDataHeader.get(groupPosition).getItemList().get(childPosition).hashCode();
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup viewGroup) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.history_list_group, null);
}
TextView groupNum = (TextView) v.findViewById(R.id.historyNumber);
TextView groupDate = (TextView) v.findViewById(R.id.historyDate);
TextView groupTransType = (TextView) v.findViewById(R.id.historyTransType);
TextView groupAmount = (TextView) v.findViewById(R.id.historyTotalAmt);
HistoryHeader header = _listDataHeader.get(groupPosition);
groupNum.setText(String.valueOf(header.getId()));
groupDate.setText(header.getDate());
groupTransType.setText(header.getTransType());
groupAmount.setText(header.getTotalAmt());
return v;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup viewGroup) {
View v = convertView;
// As for using grid view, there is only one child, no suitable view for reuse/recycle.
//if (v == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.history_list_child, null);
//}
TextView itemPropCode = (TextView) v.findViewById(R.id.historyPropertyCode);
TextView itemPropName = (TextView) v.findViewById(R.id.historyPropertyName);
GridView itemGrid = (GridView) v.findViewById(R.id.historyItemList);
// Get the child list of this group/header.
List<GridItems> history_list_child = getGroup(groupPosition).getItemGrid();
// As for using grid view, onChildClickListener cannot be used. You may need to pass the group/header object
// to grid view adapter and do onClick inside getView. First try your original way to display data correctly.
ItemGridAdapter adapter = new ItemGridAdapter(context, history_list_child);
itemGrid.setAdapter(adapter);
HistoryDetail detail = _listDataHeader.get(groupPosition).getItemList().get(childPosition);
itemPropCode.setText(detail.getPropertyCode());
itemPropName.setText(detail.getPropertyName());
return v;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Hope that helps!
I have an expandable list view. In the child item of that is a gridview. Inside gridview there are custom checkboxes.
Now the problem is when I check a checkbox and scroll down, or even close the parent view, the check is gone.
Something like this:
Screen checked before scroll:
Screen checked after scroll:
Here is my ExpandableListView Adapter:
public class CreateProfileExpandableAdapter extends BaseExpandableListAdapter {
ArrayList<CreateProfileFecetsHeaderModel> al;
Activity activity;
public CreateProfileExpandableAdapter(Activity activity, ArrayList<CreateProfileFecetsHeaderModel> al) {
this.activity = activity;
this.al = al;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return al.get(groupPosition).getChildren();
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return al.get(groupPosition).hashCode();
}
#Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.create_profile_child, parent, false);
}
ExpandableHeightGridView gridViewChildren = (ExpandableHeightGridView) v.findViewById(R.id.gridViewChildren);
gridViewChildren.setExpanded(true);
CreateProfileChildAdapter createProfileChildAdapter = new CreateProfileChildAdapter(activity, al.get(groupPosition).getChildren());
gridViewChildren.setAdapter(createProfileChildAdapter);
return v;
}
#Override
public int getChildrenCount(int groupPosition) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return al.get(groupPosition);
}
#Override
public int getGroupCount() {
return al.size();
}
#Override
public long getGroupId(int groupPosition) {
return al.get(groupPosition).hashCode();
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.create_profile_group, parent, false);
}
TextView textViewGroup = (TextView) v.findViewById(R.id.textViewGroup);
textViewGroup.setText(al.get(groupPosition).getText());
return v;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Gridview Adapter:
public class CreateProfileChildAdapter extends BaseAdapter {
Activity activity;
ArrayList<CreateProfileFecetsChildModel> al;
int index = -1;
public CreateProfileChildAdapter(Activity activity, ArrayList<CreateProfileFecetsChildModel> al) {
// TODO Auto-generated constructor stub
this.activity = activity;
this.al = al;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return al.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return al.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
private static class ViewHolder {
CheckBox checkBoxChild;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder viewHolder;
final int pos = position;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
convertView = inflater.inflate(R.layout.create_profile_grid_child, parent, false);
viewHolder = new ViewHolder();
viewHolder.checkBoxChild = (CheckBox) convertView.findViewById(R.id.checkBoxChild);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkBoxChild.setText(al.get(position).getTitle());
viewHolder.checkBoxChild.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
viewHolder.checkBoxChild.setTextColor(activity.getResources().getColor(R.color.white));
} else {
viewHolder.checkBoxChild.setTextColor(activity.getResources().getColor(R.color.black));
}
}
});
return convertView;
}
}
The problem is that the Views get recycled without having their state reset. Imagine displaying 20 items on a screen which only shows four items at once, meaning that the GridViewAdapter uses four, maybe five views. Those views take turns displaying your item when scrolling trough the list:
Solution:
You have to store everything about your view's state (in your case: what is marked as checked) in your Adapter's child list entries of the type CreateProfileFecetsHeaderModel.
In your Adapter's getChildView() fetch the checked state from your Adapter's children using the position and set the holder's CheckBox accordingly.
As it is Expandable adapter, u need to know the groupid,selectedchild,id.
try as below...
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final Pair<Long, Long> tag = new Pair<Long, Long>(
getGroupId(groupPosition),
getChildId(groupPosition, childPosition));
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_countryoforigin, null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.checkBox.setTag(tag);
viewHolder.checkBox.setChecked(mCheckedItems.contains(tag));
viewHolder.textview.setText((CharSequence) getText(groupPosition, childPosition));
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final CheckBox cb = (CheckBox) v;
final Pair<Long, Long> tag = (Pair<Long, Long>) v.getTag();
if (cb.isChecked()) {
mCheckedItems.add(tag);
} else {
mCheckedItems.remove(tag);
}
}
});
return convertView;
}
public Set<Pair<Long, Long>> getCheckedItems() {
return mCheckedItems;
}
u can call getcheckitems .....
This is my expandlable list adapter call
TableOfContentsAdapter adapter = new TableOfContentsAdapter(TableOfContentsActivity.this,toCList);
mExpandableListViewTOC.setAdapter(adapter);
And this is my adapter implementation
class TableOfContentsAdapter extends BaseExpandableListAdapter {
private Context context;
private List<TOCLink> tocLinks;
public TableOfContentsAdapter(Context context, List<TOCLink> tocLinks) {
this.context = context;
this.tocLinks = tocLinks;
}
#Override
public int getChildrenCount(int groupPosition) {
return tocLinks.get(groupPosition).getTocLinks().size();
}
#Override
public Object getGroup(int groupPosition) {
return tocLinks.get(groupPosition);
}
#Override
public int getGroupCount() {
return tocLinks.size();
}
#Override
public long getGroupId(int groupPosition) {
return (long) (groupPosition * 1024);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
List<TOCLink> childTocLinks = tocLinks.get(groupPosition).getTocLinks();
return childTocLinks.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return (long) (groupPosition * 1024 + childPosition);
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
#Override
public View getGroupView(int groupPosition, boolean b, View convertView, ViewGroup viewGroup) {
TOCLink tocLink = (TOCLink) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_header, null);
}
TextView groupHeader = (TextView) convertView.findViewById(R.id.header);
groupHeader.setText(tocLink.getSectionTitle());
return convertView;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean b, View convertView, ViewGroup viewGroup) {
TOCLink tocLink = (TOCLink) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
}
TextView childItem = (TextView) convertView.findViewById(R.id.item);
childItem.setText(tocLink.getSectionTitle());
/*TOCLink tocLink1 = tocLinks.get(groupPosition).getTocLinks().get(childPosition);
if (tocLink1.getTocLinks().size() > 0) {
new TableOfContentsAdapter(context, tocLink1.getTocLinks());
}*/
return convertView;
}
}
I want to implement multilevel expandable list view with single adapter. Problem here is i'm able to go to level 1 but not further.
How can resolve this problem?
try to implement in your project: https://github.com/defacto133/MultiLevelExpandableIndentableListView
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?