Scrolling issue with an expandablelist - java

I have a display problem with my 4-stage expandableList.
I can display my data, expand them but the last child does not display the whole list. How would you do it?
Here is a picture and the part of the code in question, I think it comes from the fact that I declare the size of the last list.
public static class ThirdLevelAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<String> materiau;
private HashMap<String, ArrayList<MatosItem>> materiauItemList;
int c = 1;
LayoutInflater inflater3;
public ThirdLevelAdapter(Context context,HashMap<String, ArrayList<MatosItem>> materiauItemList,ArrayList<String> materiau) {
this.context = context;
this.materiauItemList = materiauItemList;
this.materiau = materiau;
this.inflater3 = LayoutInflater.from(context);
}
#Override
public Object getGroup(int groupPosition) {
return materiau.get(groupPosition);
}
#Override
public int getGroupCount() {
return materiau.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_expandable_list_item_1,
parent,false);
//Initialization and assign variable
TextView textView = convertView.findViewById(android.R.id.text1);
//Initial string pour le nom de famille
String sGroup = String.valueOf(getGroup(groupPosition));
//Set text on text view
textView.setText(sGroup);
//Set text style bold
textView.setTypeface(null, Typeface.BOLD);
//Set text color
//textView.setBackgroundColor(Color.parseColor("#FF018786"));
textView.setTextColor(Color.parseColor("#FF018786"));
textView.setTextSize(1,20);
//Return View
}
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return materiauItemList.get(materiau.get(groupPosition)).get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
view = inflater3.inflate(R.layout.adapter_item2, null);
//get information about item
MatosItem currentItem = (MatosItem) getChild(i,i1);
String itemName = currentItem.getName();
String itemNumber = currentItem.getNumber();
Boolean intemCheck = currentItem.getCheck();
String itemRef = currentItem.getRef();
String itemCodefourn = currentItem.getCodefourn();
//get item name view
TextView itemNameView = view.findViewById(R.id.name);
itemNameView.setText(itemName);
//get item number view
TextView itemNumberView = view.findViewById(R.id.item_number);
itemNumberView.setText(itemNumber);
//get item ref view
TextView itemRefView = view.findViewById(R.id.ref);
itemRefView.setText(itemRef);
//get item codefournisseur
TextView itemCodefournView = view.findViewById(R.id.codefourn);
itemCodefournView.setText(itemCodefourn);
LinearLayout layout = view.findViewById(R.id.compteur);
Button btnplus = view.findViewById(R.id.btnplus);
Button btnmoins = view.findViewById(R.id.btnmoins);
TextView text = view.findViewById(R.id.text);
if(intemCheck){
//checkBox.setChecked(true);
mdescription.add(itemName);
mid.add(itemNumber);
mref.add(itemRef);
mcodefourn.add(itemCodefourn);
}
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
layout.setVisibility(View.VISIBLE);
mdescription.add(""+itemName);
mid.add(""+itemNumber);
mref.add(itemRef);
mcodefourn.add(itemCodefourn);
//checkBox.setChecked(true);
//Toast.makeText(viewGroup.getContext(),itemName , Toast.LENGTH_SHORT).show();
}
});
btnplus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
c=c+1;
text.setText(""+c);
mdescription.add(""+itemName);
mid.add(""+itemNumber);
mref.add(itemRef);
mcodefourn.add(itemCodefourn);
}
});
btnmoins.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
c=c-1;
text.setText(""+c);
mdescription.remove(""+itemName);
mid.remove(""+itemNumber);
mref.remove(itemRef);
mcodefourn.remove(itemCodefourn);
}
});
return view;
}
#Override
public int getChildrenCount(int groupPosition) {
return materiauItemList.get(materiau.get(groupPosition)).size();
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
level 1-2 and 3 work fine and i can scroll
level 4 is not displayed completely and I can't scroll
Thank you for your help

Related

Problems At Three Level Expandable List View in Android

I wrote a 3 level expandable list code which is like that:
A
|_B
|_C
D
|_E
|_F
But in my list, each row consists of buttons, edittexts etc. And I'm adding childs dynamically as you can see at the below code.
I need some help about 2 things.
1) When I click A, B is opening but when i clicked B, C is not showing in the screen.
2) When i click A or D, group position is not changing always the same which is 0. Because of that A always expands.
You can find the codes below, thank you :)
Beginning of the activity I wrote these:
public static int FIRST_LEVEL_COUNT = 1;
public static int SECOND_LEVEL_COUNT = 1;
public static int THIRD_LEVEL_COUNT = 1;
boolean firstLevel_Expanded = false;
boolean secondLevel_Expanded = false;
And then calling expandable list view from xml:
demand_table = (ExpandableListView) dialog.findViewById(R.id.demandresponsetable);
demand_table.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
demand_table.setAdapter(new ParentLevel(this));
My expandable list's code is here:
public class ParentLevel extends BaseExpandableListAdapter {
private Context context;
public ParentLevel(Context context) {
this.context = context;
}
#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) {
SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(LoadDemandResponse.this);
secondLevelELV.setAdapter(new SecondLevelAdapter(context));
secondLevelELV.setGroupIndicator(null);
return secondLevelELV;
}
#Override
public int getChildrenCount(int groupPosition) {
return SECOND_LEVEL_COUNT;
}
#Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
#Override
public int getGroupCount() {
return FIRST_LEVEL_COUNT;
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.threelevel_listrow_first, null);
final long position = getGroupId(groupPosition);
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(firstLevel_Expanded == true) {
firstLevel_Expanded = false;
demand_table.expandGroup((int) position);
}
else {
firstLevel_Expanded = true;
demand_table.collapseGroup((int) position);
}
}
});
TextView text = (TextView) convertView.findViewById(R.id.Edit_eventsListEventRowText);
text.setHint("Demand Response Name");
ToggleButton toggle = (ToggleButton) convertView.findViewById(R.id.dr_status);
toggle.setText("Passıve");
toggle.setTextOff("Passıve");
toggle.setTextOn("Actıve");
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
} else {
// The toggle is disabled
}
}
});
Button time = (Button) convertView.findViewById(R.id.add_time);
time.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SECOND_LEVEL_COUNT++;
}
});
}
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public class SecondLevelExpandableListView extends ExpandableListView {
public SecondLevelExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations.
heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class ThirdLevelExpandableListView extends ExpandableListView {
public ThirdLevelExpandableListView(Context context) {
super(context);
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations.
heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public class SecondLevelAdapter extends BaseExpandableListAdapter {
private Context context;
public SecondLevelAdapter(Context context) {
this.context = context;
}
#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(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.threelevel_listrow_second, null);
//text.setText("SECOND LEVEL");
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(secondLevel_Expanded == true) {
secondLevel_Expanded = false;
demand_table.expandGroup(groupPosition);
}
else {
secondLevel_Expanded = true;
demand_table.collapseGroup(groupPosition);
}
}
});
final View finalConvertView = convertView;
final EditText start = (EditText) convertView.findViewById(R.id.edit_starttime);
start.setInputType(InputType.TYPE_NULL);
start.requestFocus();
start.setHint("Start Time");
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(finalConvertView.getContext(), new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
start.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
final EditText end = (EditText) convertView.findViewById(R.id.edit_endtime);
end.setInputType(InputType.TYPE_NULL);
end.requestFocus();
end.setHint("End Time");
end.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(finalConvertView.getContext(), new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
end.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});
Button offer = (Button) convertView.findViewById(R.id.add_offer);
offer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
THIRD_LEVEL_COUNT++;
}
});
}
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ThirdLevelExpandableListView thirdLevelELV = new ThirdLevelExpandableListView(LoadDemandResponse.this);
thirdLevelELV.setAdapter(new ThirdLevelAdapter(context));
thirdLevelELV.setGroupIndicator(null);
return thirdLevelELV;
}
#Override
public int getChildrenCount(int groupPosition) {
return THIRD_LEVEL_COUNT;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
public class ThirdLevelAdapter extends BaseExpandableListAdapter {
private Context context;
public ThirdLevelAdapter(Context context) {
this.context = context;
}
#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(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.threelevel_listrow_third, null);
EditText quantity = (EditText) convertView.findViewById(R.id.edit_quantity);
quantity.setHint("Reserve Quantity");
EditText price = (EditText) convertView.findViewById(R.id.edit_price);
price.setHint("Offered Price");
}
return convertView;
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if(convertView != null) {
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
demand_table.expandGroup(groupPosition);
}
});
}
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
return THIRD_LEVEL_COUNT;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

Android, Expandable List View Delete child on Button click

I have implemented a Expandable List View,in Android , you can see in the screenshot , it is working fine , and in Expandable List i have multiple child with delete option in group as you can see in the screen shot, i want to delete a particular child which user has selected to delete , I am not able to delete the child form Expandable List , i will post my code , please anyone guide me
public class PendingFragment extends Fragment {
private AnimatedExpandableListView listView;
private PendingAdapter adapter;
CoordinatorLayout coordinatorLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_pending, container, false);
coordinatorLayout = (CoordinatorLayout)view.findViewById(R.id.pending_frag_coordinatorLayout);
List<GroupItem> items = new ArrayList<GroupItem>();
// Populate our list with groups and it's children
for(int i = 1; i < 10; i++) {
GroupItem item = new GroupItem();
item.title = "GroupItem " + i;
for(int j = 0; j < i; j++) {
ChildItem child = new ChildItem();
child.title = "ChildItem " + j;
item.items.add(child);
}
items.add(item);
}
adapter = new PendingAdapter(getActivity());
adapter.setData(items);
listView = (AnimatedExpandableListView)view.findViewById(R.id.pending_explistView);
listView.setAdapter(adapter);
return view;
}
}
My Listview Adapter
public class PendingAdapter extends AnimatedExpandableListView.AnimatedExpandableListAdapter {
private LayoutInflater inflater;
private List<GroupItem> items;
ListPopupWindow listPopupWindow;
String[] products = {"Delete"};
Context contexts;
public PendingAdapter(Context context) {
inflater = LayoutInflater.from(context);
this.contexts = context;
}
public void setData(List<GroupItem> items) {
this.items = items;
}
private class ChildHolder {
TextView title;
ImageView option_menu;
}
private class GroupHolder {
TextView title;
}
#Override
public ChildItem getChild(int groupPosition, int childPosition) {
return items.get(groupPosition).items.get(childPosition);
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getRealChildView(final int groupPosition,final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ChildHolder holder;
ChildItem item = getChild(groupPosition, childPosition);
Log.e("groupPosition=" + groupPosition, "childPosition=" + childPosition);
if (convertView == null) {
holder = new ChildHolder();
convertView = inflater.inflate(R.layout.expchildlistview, parent, false);
holder.title = (TextView) convertView.findViewById(R.id.txtListChild);
holder.option_menu = (ImageView) convertView.findViewById(R.id.checkin_option_menu);
convertView.setTag(holder);
} else {
holder = (ChildHolder) convertView.getTag();
}
holder.title.setText(item.title);
holder.option_menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ShowListMenu(view,groupPosition, childPosition);
}
});
return convertView;
}
public void ShowListMenu(View v, final int childPosition, final int groupPosition){
listPopupWindow = new ListPopupWindow(contexts);
listPopupWindow.setAdapter(new ArrayAdapter(contexts, android.R.layout.simple_list_item_1, products));
listPopupWindow.setAnchorView(v);
if (Utils.device_width(contexts) >= 320 && Utils.device_width(contexts) < 480) {
//Log.v(">= 320 < 480=", ">= 320 < 480");
listPopupWindow.setWidth(150);
}else if (Utils.device_width(contexts) >= 480 && Utils.device_width(contexts) < 500) {
//Log.v(">= 480=", "< 500");
listPopupWindow.setWidth(150);
}else{
listPopupWindow.setWidth(200);
}
listPopupWindow.setModal(true);
listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.v("item.remove=", "" + items.get(groupPosition).items.get(childPosition));
items.get(groupPosition).items.remove(childPosition);
notifyDataSetChanged();
listPopupWindow.dismiss();
}
});
listPopupWindow.show();
}
#Override
public int getRealChildrenCount(int groupPosition) {
return items.get(groupPosition).items.size();
}
#Override
public GroupItem getGroup(int groupPosition) {
return items.get(groupPosition);
}
#Override
public int getGroupCount() {
return items.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupHolder holder;
GroupItem item = getGroup(groupPosition);
if (convertView == null) {
holder = new GroupHolder();
convertView = inflater.inflate(R.layout.explist_group, parent, false);
holder.title = (TextView) convertView.findViewById(R.id.lblListHeader);
convertView.setTag(holder);
} else {
holder = (GroupHolder) convertView.getTag();
}
holder.title.setText(item.title);
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
}
public class GroupItem {
public String title;
public List<ChildItem> items = new ArrayList<ChildItem>();
}
public class ChildItem {
public String title;
}
Cant see items.remove(child) on click on delete. I think you should try deleting the child from the list and than set the adapter with updated values. #Achin

How to use set changes in list when one row deleted

I'm using expandable listView and Expandable ListAdapter.
ChlidView show me the titles and child View show me the subTitle .
In the "child View" i have a button that delete the the specific row .
i have two classes, one for loading the data from database into the hashMap and the second class for the ExpandableListAdapter. i create the onClick method on the adapter class , and the delete function is working, but the list not refreshing the updated data. i know i need to load again the data to the hashMap but the adapter class is outSide the first class. i'm trying to use "notifyDataSetChanged" and no result too.
class 1 :
hand = new DbHandler(getActivity());
fullList = new HashMap<ArrayList<ClockModel>, ArrayList<ClockModel>>();
temp = new ArrayList<ClockModel>();
ArrayList<ClockModel> child = new ArrayList<ClockModel>();
child = hand.getDay(workName);
for (int i = 0; i < child.size(); i++) {
if(child.get(i).getDateMonth() == month && child.get(i).getDateYear()==year){
ClockModel m1 = new ClockModel(
child.get(i).getId(),
child.get(i).getWorkName(),
child.get(i).getDateDay(),
child.get(i).getDateMonth(),
);
temp.add(m1);
fullList.put(temp, temp);
}
}
adapter = new ExpandableListCustom(getActivity(), fullList,temp);
lv.setAdapter(adapter);
adapter :
#Override
public View getChildView(final 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.row_child,null);
}
TextView txMyVal = (TextView) convertView.findViewById(R.id.row_myVal);
TextView txBreakTime = (TextView) convertView.findViewById(R.id.row_breakTime);
TextView txComment = (TextView) convertView.findViewById(R.id.row_comments);
TextView txNameOfDay = (TextView) convertView.findViewById(R.id.row_nameOfDay);
String comments= fullList.get(child).get(groupPosition).getComment();
nameOfDay= fullList.get(child).get(groupPosition).getNameOfDay();
shiftType= fullList.get(child).get(groupPosition).getShiftType();
int breakTime= fullList.get(child).get(groupPosition).getBreakTime();
currentPosition = fullList.get(child).get(groupPosition).getId();
btnRemove = (ImageButton) convertView.findViewById(R.id.row_btnRemove);
btnRemove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hand.deleteDay(currentPosition);
Toast.makeText(context, "Del : "+currentPosition, 2000).show();
}
});
public class ExpandableListCustom extends BaseExpandableListAdapter implements OnClickListener {
DbHandler hand;
private Context context;
private HashMap<ArrayList<ClockModel>,ArrayList<ClockModel> > fullList; //Headers
ArrayList<ClockModel> child; //data
MySharedPreferences preferences;
public ExpandableListCustom(Context context,
HashMap<ArrayList<ClockModel>, ArrayList<ClockModel>> fullList,
ArrayList<ClockModel> child) {
super();
this.context = context;
this.fullList = fullList;
this.child = child;
clockSet = new Clock();
preferences = new MySharedPreferences(context);
hand = new DbHandler(context);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return this.fullList.get(this.fullList.get(childPosition));
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public View getChildView(final 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.row_child,null);
}
TextView txMyVal = (TextView) convertView.findViewById(R.id.row_myVal);
TextView txBreakTime = (TextView) convertView.findViewById(R.id.row_breakTime);
TextView txComment = (TextView) convertView.findViewById(R.id.row_comments);
TextView txNameOfDay = (TextView) convertView.findViewById(R.id.row_nameOfDay);
enterHour= (int) fullList.get(child).get(groupPosition).getEnterHour();
exitHour= (int) fullList.get(child).get(groupPosition).getExitHour();
enterMin= (int) fullList.get(child).get(groupPosition).getEnterMin();
exitMin= (int) fullList.get(child).get(groupPosition).getExitMin();
currentPosition = fullList.get(child).get(groupPosition).getId();
btnRemove = (ImageButton) convertView.findViewById(R.id.row_btnRemove);
btnRemove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hand.deleteDay(currentPosition);
Toast.makeText(context, "Del : "+currentPosition, 2000).show();
}
});
return convertView;
}
#Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub //////////////////////////////////////////
// return this.fullList.get(groupPosition).size();
if(fullList != null){
return 1;
}else{
return 0;
}
}
#Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return this.fullList.get(child).get(groupPosition);
}
#Override
public int getGroupCount() {
if(fullList != null){
return this.fullList.size();
}else{
return 0;
}
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
int dateDay= fullList.get(child).get(groupPosition).getDateDay();
int dateMonth= fullList.get(child).get(groupPosition).getDateMonth();
int dateYear= fullList.get(child).get(groupPosition).getDateYear();
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.row_header, null);
}
return convertView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
}
Now when you delete the item from the database you need at the same time to remove it from the adapter and call notifyDataSetChanged():
#Override
public void onClick(View v) {
hand.deleteDay(currentPosition);
fullList.remove(currentPosition);
notifyDataSetChanged();
}
you need to fetch data from database and assign to arraylist "fullList" which is used for adapter when you delete the data and use ...
notifyDataSetChanged();
method of adapter...

How to update Android ExpandableList data dynamically?

I have a ExpandableListView in a Fragment.
I'm posting a request to the server using RoboSpice onCreateView.
I want to be able to update the list twice, once when I get the data from the cache and second after the response returns.
For some reason the list is not populated.
I was able to populate it when I instantiate a new adapter and set the adapter for list, but for some reason it doesn't work on some android devices.
This is the code I'm using:
Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
_expandableListAdapter = new GroupsExpandableListAdapter(getActivity(), getSpiceManager());
_groupListView.setAdapter(_expandableListAdapter);
DBServerHelper.Groups.getByBusinessId(getActivity(), getSpiceManager(), getDBHelper(), _clientBusiness.getId(),
new CachedRequestListener<ClientGroup.List>(getDBHelper(), getActivity()) {
#Override
// This method runs twice, after the data comes from the cache and once the request succeed
public void onRequestFromCache(ClientGroup.List clientGroups) {
if (clientGroups == null || clientGroups.isEmpty()) {
Ln.d("Received clientGroups " + clientGroups == null ? "null" : "empty" + " form request GetByBusinessId");
return;
}
_expandableListAdapter.clear();
_expandableListAdapter.setGroups(clientGroups);
_expandableListAdapter.notifyDataSetChanged();
_groupListView.invalidateViews();
}
#Override
public void onRequestFailure(SpiceException spiceException) {
super.onRequestFailure(spiceException);
Ln.e(spiceException, "Failed to get groups ");
Toaster.showLong(getActivity(), "Could not get businesses groups: " + spiceException.getMessage());
}
});
}
GroupsExpandableListAdapter
public class GroupsExpandableListAdapter extends BaseExpandableListAdapter {
private ClientGroup.List _data;
private Activity _activity;
/**
* It is only possible to create a group using the factory method create below
* #param activity
*/
public GroupsExpandableListAdapter(Activity activity, SpiceManager manager) {
super();
_data = new ClientGroup.List();
_activity = activity;
_manager = manager;
}
#Override
public int getGroupCount() {
return _data.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return _data.size();
}
#Override
public Object getGroup(int groupPosition) {
return _data.get(groupPosition).getName();
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return _data.get(groupPosition).getDeleteReason();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
#Override
public boolean hasStableIds() {
return false;
}
public void clear() {
_data.clear();
}
public void setGroups(ClientGroup.List groups) {
_groups = groups;
}
#Override
public View getGroupView(final int groupPosition, final boolean isExpanded, final View convertView,
final ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.group_list_group, parent, false);
TextView textView = (TextView) v.findViewById(R.id.groupName);
textView.setText((CharSequence) _data.get(groupPosition).getName());
((ImageView) v.findViewById(R.id.groupIndicator))
.setImageResource(isExpanded ? R.drawable.arrow_up_turquoise : R.drawable.arrow_down_white);
} else {
v = convertView;
}
return v;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = (LayoutInflater) _activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.group_list_child, parent, false);
} else {
v = convertView;
}
TextView joinGroupButton = (TextView) v.findViewById(R.id.joinGroupButton);
final ClientGroup group = _data.get(groupPosition);
String leaveGroupStr = _activity.getString(R.string.leave_group);
String joinGroupStr = _activity.getString(R.string.join_group);
return v;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
Any suggestions?
Thanks!

NullPointerException in ExpandableListView

i am getting a null pointer exception
public class ApisimpleActivity extends ExpandableListActivity {
ExpandableListAdapter mAdapter;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set up our adapter
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int i=getExpandableListView().getChildCount();
int k=0;
//Toast.makeText(ApisimpleActivity.this, "hello"+i, Toast.LENGTH_LONG).show();
for(int j=0;j<i;j++)
{
View v=(View)getExpandableListView().getChildAt(i);
if(v==null)
//if(v.findViewById(R.id.chk)==null)
k++;
}
Toast.makeText(ApisimpleActivity.this, k+"",Toast.LENGTH_LONG).show();
} });
mAdapter = new MyExpandableListAdapter();
setListAdapter(mAdapter);
registerForContextMenu(getExpandableListView());
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.setHeaderTitle("Sample menu");
menu.add(0, 0, 0, "Hello World");
}
#Override
public boolean onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
String title = ((TextView) info.targetView).getText().toString();
int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,
Toast.LENGTH_SHORT).show();
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
/**
* A simple adapter which maintains an ArrayList of photo resource Ids.
* Each photo is displayed as an image. This adapter supports clearing the
* list of photos and adding a new photo.
*
*/
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
private String[] groups = { "People Names", "Dog Names", "Cat Names", "Fish Names" };
private String[][] children = {
{ "Arnold", "Barry", "Chuck", "David" },
{ "Ace", "Bandit", "Cha-Cha", "Deuce" },
{ "Fluffy", "Snuggles" },
{ "Goldy", "Bubbles" }
};
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView textView = new TextView(ApisimpleActivity.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
return textView;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
View v;
v=convertView;
if(v==null)
{LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.group, null);
}
return v;}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}
on btn.setOnClickListener when i call the function
getExpandableListView().getChildAt(i) all the views are null.can any one please help why all the view are showing null i need to get the view and check on each weather checkbox is checked or not.please some one help
i think you want getExpandableListView().getChildAt(j) (j instead of i, for j is the size of the list, therefor not in it)
Edit: not really important here, but using k+"" to print an integer is slow. Use String.valueOf(k) instead

Categories