App gets crashed randomly while scrolling expandable list view - java

I've been recyclerview in my app and on scrolling it gets crashed, not under any certain use case..it just gets crashed randomly. Below is my adapter and View Holder code.
class CustomListView_Base_Adapter extends BaseExpandableListAdapter {
Context context;
private LayoutInflater inflater = null;
public CustomListView_Base_Adapter(Context context) {
this.inflater = LayoutInflater.from(context);
this.context = context;
}
#Override
public int getGroupCount() {
return dataforSubcategoryItem.size();
}
#Override
public int getChildrenCount(int groupPosition) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return dataforSubcategoryItem.get(groupPosition);
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return dataforSubcategoryItem.get(groupPosition);
}
#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(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
View resultView = convertView;
final ViewHolder holder;
if (resultView == null) {
resultView = inflater.inflate(R.layout.add_subcategory_list_item, null); //TODO change layout id
holder = new ViewHolder(resultView);
resultView.setTag(holder);
} else {
holder = (ViewHolder) resultView.getTag();
}
final SubcategoryItemBean beanObj = (SubcategoryItemBean) getGroup(groupPosition);
holder.tvColumnTitle.setText(beanObj.getName());
holder.checkBox.setOnCheckedChangeListener(null);
CustomWatcher oldWatcher = (CustomWatcher) holder.etInput.getTag();
if (oldWatcher != null)
holder.etInput.removeTextChangedListener(oldWatcher);
if (beanObj.isColumnSelected()) {
holder.checkBox.setChecked(true);
holder.etInput.setText(dataforSubcategoryItem.get(groupPosition).getLabel());
holder.etInput.setEnabled(true);
listView.expandGroup(groupPosition);
} else {
holder.checkBox.setChecked(false);
holder.etInput.setText("");
holder.etInput.setEnabled(false);
listView.collapseGroup(groupPosition);
}
CustomWatcher newWatcher = new CustomWatcher(groupPosition, holder.etInput);
holder.etInput.setTag(newWatcher);
holder.etInput.addTextChangedListener(newWatcher);
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
dataforSubcategoryItem.get(groupPosition).setColumnSelected(true);
} else {
dataforSubcategoryItem.get(groupPosition).setColumnSelected(false);
}
notifyDataSetChanged();
}
});
return resultView;
}
#Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View resultView = convertView;
final ViewHolder holder;
if (resultView == null) {
resultView = inflater.inflate(R.layout.child_list_item, null); //TODO change layout id
holder = new ViewHolder(resultView, context);
resultView.setTag(holder);
} else {
holder = (ViewHolder) resultView.getTag();
}
final SubcategoryItemBean beanObj = (SubcategoryItemBean) getChild(groupPosition, childPosition);
CustomWatcher oldWatcher = (CustomWatcher) holder.defaultValues.getTag();
if (oldWatcher != null)
holder.defaultValues.removeTextChangedListener(oldWatcher);
holder.radiogroup.setOnCheckedChangeListener(null);
holder.defaultValues.setText(beanObj.getValue());
if (beanObj.getInput_type().equalsIgnoreCase(getString(R.string.column_type_text))) {
holder.defaultValues.setInputType(InputType.TYPE_CLASS_TEXT);
holder.defaultValues.setHint("Enter Default Value");
holder.rbText.setChecked(true);
} else if (beanObj.getInput_type().equalsIgnoreCase(getString(R.string.column_type_number))) {
holder.defaultValues.setInputType(InputType.TYPE_CLASS_NUMBER);
holder.defaultValues.setHint("Enter Default Value");
holder.rbNumber.setChecked(true);
} else if (beanObj.getInput_type().equalsIgnoreCase(getString(R.string.column_type_radiobutton))) {
holder.defaultValues.setInputType(InputType.TYPE_CLASS_TEXT);
holder.defaultValues.setHint("Enter comma separated values");
holder.rbRadioButton.setChecked(true);
} else if (beanObj.getInput_type().equalsIgnoreCase(getString(R.string.column_type_checkbox))) {
holder.rbCheckbox.setChecked(true);
holder.defaultValues.setHint("Enter comma separated values");
holder.defaultValues.setInputType(InputType.TYPE_CLASS_TEXT);
} else {
holder.rbDropdown.setChecked(true);
holder.defaultValues.setHint("Enter comma separated values");
holder.defaultValues.setInputType(InputType.TYPE_CLASS_TEXT);
}
holder.radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, #IdRes int checkedId) {
RadioButton button = (RadioButton) group.findViewById(group.getCheckedRadioButtonId());
dataforSubcategoryItem.get(groupPosition).setInput_type(button.getText().toString());
holder.defaultValues.setText("");
notifyDataSetChanged();
}
});
CustomWatcher newWatcher = new CustomWatcher(groupPosition, holder.defaultValues);
holder.defaultValues.setTag(newWatcher);
holder.defaultValues.addTextChangedListener(newWatcher);
return resultView;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
private class CustomWatcher implements TextWatcher {
private int position;
private EditText editText;
private CustomWatcher(int position, EditText editText) {
this.position = position;
this.editText = editText;
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
if (editText.getId() == R.id.addCategory_chooseItem_EditText) {
dataforSubcategoryItem.get(position).setLabel(editable.toString().trim());
System.out.println("CustomWatcher.onTextChanged label" + editable.toString());
}
if (editText.getId() == R.id.defaultValues_addSubCategory_chooseItem) {
dataforSubcategoryItem.get(position).setValue(editable.toString().trim());
System.out.println("CustomWatcher.afterTextChanged default value " + editable.toString());
}
}
}
}
class ViewHolder {
CheckBox checkBox;
TextView tvColumnTitle;
EditText etInput;
RadioGroup radiogroup;
RadioButton rbText, rbNumber, rbRadioButton, rbCheckbox, rbDropdown;
EditText defaultValues;
LinearLayout columnDetailsLayout;
public ViewHolder(View view) {
checkBox = (CheckBox) view.findViewById(R.id.addCategory_chooseItem_Checkbox);
tvColumnTitle = (TextView) view.findViewById(R.id.addCategory_chooseItem_TextView);
etInput = (EditText) view.findViewById(R.id.addCategory_chooseItem_EditText);
}
public ViewHolder(View view, Context context) {
columnDetailsLayout = (LinearLayout) view.findViewById(R.id.columnDetailsLayout);
radiogroup = (RadioGroup) view.findViewById(R.id.radiogroup_addSubCategory_chooseItem);
rbText = (RadioButton) view.findViewById(R.id.rbText_addSubCategory_chooseItem);
rbNumber = (RadioButton) view.findViewById(R.id.rbNumber_addSubCategory_chooseItem);
rbRadioButton = (RadioButton) view.findViewById(R.id.rbRadioButton_addSubCategory_chooseItem);
rbCheckbox = (RadioButton) view.findViewById(R.id.rbCheckbox_addSubCategory_chooseItem);
rbDropdown = (RadioButton) view.findViewById(R.id.rbDropdown_addSubCategory_chooseItem);
defaultValues = (EditText) view.findViewById(R.id.defaultValues_addSubCategory_chooseItem);
rbText.setChecked(true);
}
}
This is the output:
This is the error I'm getting after app gets crashed
E/AndroidRuntime: FATAL EXCEPTION: main
Process: global.edios.inventorymanagementtabletapp, PID: 2927
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setBounds(android.graphics.Rect)' on a null object reference
at android.widget.ExpandableListView.drawDivider(ExpandableListView.java:536)
at android.widget.ListView.dispatchDraw(ListView.java:3288)
at android.widget.ExpandableListView.dispatchDraw(ExpandableListView.java:353)
at android.view.View.draw(View.java:15234)
at android.widget.AbsListView.draw(AbsListView.java:4110)
at android.view.View.updateDisplayListIfDirty(View.java:14167)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3389)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3368)
at android.view.View.updateDisplayListIfDirty(View.java:14127)
at android.view.View.getDisplayList(View.java:14189)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2530)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Please help. Thanks.

This crash has occurred due to Expandable List View and invoking Text Watcher.
and using multiple Text Watcher's for both parent and child item.
Plus you even change Radio Group.
Try Changing text Watcher with Something else

Heyy, the app was crashed due to divider used in expandable view .. i removed the divider from expandable list and added a custom view instead of divider in
<ExpandableListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/layout_SelectAll"
android:background="#color/colorWhite"
android:divider="#null"
android:groupIndicator="#null"
android:paddingBottom="70dp"
/>
Problem solved :)

Related

Scrolling issue with an expandablelist

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

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;
}
}

ExpandableListAdapter methods are not working properly in my case. Any alternate to ExpandableListView

I am using an ExpandableListAdapter for the first time. I have some textviews in the groupView and some buttons in childView. I've used the following code from the internet and tried to convert it according to my preference. But there is a problem in the getGroupCount() method. I know in my code _listDataHeader and _listDataChild are null, but what should I use instead? Here are the pictures of my header view
and Child View layout
.
On the click of header this child View should be shown on screen.. Am i using the correct android View for this design? Please help
public class ExpandableListAdapter extends BaseExpandableListAdapter {
Activity context;
ArrayList<String> s_date,c_number,d_ration,s_time,download_path,a_number,a_name;
String id[];
String mUrl;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Activity context, ArrayList<String> start_date, ArrayList<String> caller_number, ArrayList<String> duration,ArrayList<String> start_time, ArrayList<String> download_path, ArrayList<String> agent_number, ArrayList<String> agent_name) {
this.context=context;
this.s_date=start_date;
this.c_number=caller_number;
this.d_ration=duration;
this.s_time=start_time;
this.download_path=download_path;
this.a_number=agent_number;
this.a_name=agent_name;
}
#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(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.list_item, null);
}
Button play_button=(Button) convertView.findViewById(R.id.play);
play_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("play child is",childText);
}
});
Button download_button=(Button) convertView.findViewById(R.id.download);
download_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("download child is",childText);
}
});
}
#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.s_date.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 start_date = (TextView) convertView.findViewById(R.id.start_date);
start_date.setText(s_date.get(groupPosition));
TextView caller_number = (TextView) convertView.findViewById(R.id.caller_number);
caller_number.setText(c_number.get(groupPosition));
TextView duration = (TextView) convertView.findViewById(R.id.duration);
duration.setText(d_ration.get(groupPosition));
TextView start_time = (TextView) convertView.findViewById(R.id.start_time);
start_time.setText(s_time.get(groupPosition));
TextView agent_name = (TextView) convertView.findViewById(R.id.agent_name);
agent_name.setText(a_name.get(groupPosition));
TextView agent_number = (TextView) convertView.findViewById(R.id.agent_number);
agent_number.setText(a_number.get(groupPosition));
// start_date.setTypeface(null, Typeface.BOLD);
// start_date.setText(headerTitle);
return convertView;
}
#Override
public boolean hasStableIds() {
return false;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
and this is how i'm setting the adpater
ExpandableListAdapter a=new ExpandableListAdapter(MainScreen.this,start_date,caller_number,duration,start_time,download_path,agent_number,agent_name);
data_list.setAdapter(a);
As Suggested by #PratikDasa I have used Custom ListView and in that i have used Visible gone method. Below is the solution.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/sub_layout"
android:background="#FFC7FFFE"
android:visibility="gone">
I have set all the contents(Buttons in my case) under this sub_layout and set layout visibilty as gone.
And then on the click of any of the list item(Headers), I have shown the sub_layout that i have set as gone.
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View row = inflater.inflate(R.layout.list_item, g, false);
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Toast.makeText(context,"i'm Clicked",Toast.LENGTH_LONG).show();
RelativeLayout sub_list=(RelativeLayout)row.findViewById(R.id.sub_layout);
sub_list.setVisibility(View.VISIBLE);
}
});
Now it works like an ExpandableListView.

Trying to deselect a view using getChildAt()

I'm using a GridView inside an ExpandableListView
I have a function that highlights an item when it is clicked, now I'm trying to implement a button that when pressed it will unselect all selected items, but it is unselecting only the last view clicked
public class GridAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Filhos> child;
public ArrayList<CadastraEscolas> escola;
private ArrayList<ArrayList<Filhos>> filhos = new ArrayList();
public GridAdapter(Context context, ArrayList<CadastraEscolas> groups, ArrayList<Filhos> childValues, int groupPosition) {
mContext = context;
child = childValues;
escola = groups;
posicaoEscola = groupPosition;
}
#Override
public int getCount() {
return child.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item, null);
holder = new ViewHolder();
final TextView idAluno = (TextView) convertView.findViewById(R.id.idcrianca);
final TextView nomeAluno = (TextView) convertView.findViewById(R.id.name);
convertView.setTag(holder);
final View finalConvertView = convertView;
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (list.size() > 0) {
isChecked = filhos.get(posicaoEscola).get(position).isChecked();
if (!isChecked) {
selecao(true, position, nomeAluno, 0xFFFFFFFF, finalConvertView, View.VISIBLE);
} else {
selecao(false, position, nomeAluno, 0xFFD5672B, finalConvertView, View.GONE);
}
ex.findViewById(R.id.notificar).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
limpaSelecao(false, position, nomeAluno, 0xFFD5672B, parent, View.GONE);
}
});
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(child.get(position).getNome());
return convertView;
}
static class ViewHolder {
TextView text;
}
public void selecao(boolean check, int position, TextView nomeAluno, int color, View v, int visibility) {
filhos.get(posicaoEscola).get(position).setChecked(check);
nomeAluno.setTextColor(color);
v.findViewById(R.id.overlay).setVisibility(visibility);
v.findViewById(R.id.overlayText).setVisibility(visibility);
}
public void limpaSelecao(boolean check, int position, TextView nomeAluno, int color, ViewGroup v, int visibility) {
for (int x = 0; x < group.size(); x++) {
for (int j = 0; j < group.get(x).getalunos().size(); j++) {
if(filhos.get(x).get(j).isChecked()){
v.getChildAt(j).findViewById(R.id.loadingPanel).findViewById(R.id.overlay).setVisibility(View.GONE);
}
nomeAluno.setTextColor(color);
}
}
}
}
Layout:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/nomealuno_main"
android:layout_below="#+id/child">
<View
android:id="#+id/overlay"
android:layout_width="125dp"
android:layout_height="50dp"
android:background="#color/bgOverlay"
android:visibility="gone"/>
</RelativeLayout>
here is what is happening:
when I select all the items from A to B like that:
And hit clear, it will only remove the selected children from the last group that I clicked:
I need to remove them all when I click on Clear, all the children no matter in what group they are have to be removed.
ExpandableAdapter:
public class ExpandListTest extends BaseExpandableListAdapter {
public static final int CHOICE_MODE_MULTIPLE = AbsListView.CHOICE_MODE_MULTIPLE;
public static final int CHOICE_MODE_MULTIPLE_MODAL = AbsListView.CHOICE_MODE_MULTIPLE_MODAL;
/**
* No child could be selected
*/
public static final int CHOICE_MODE_NONE = AbsListView.CHOICE_MODE_NONE;
/**
* One single choice per group
*/
public static final int CHOICE_MODE_SINGLE_PER_GROUP = AbsListView.CHOICE_MODE_SINGLE;
/**
* One single choice for all the groups
*/
public static final int CHOICE_MODE_SINGLE_ABSOLUTE = 10001;
private Context context;
public static ArrayList<CadastraEscolas> groups;
private ArrayList<ArrayList<Filhos>> child = new ArrayList();
private HashMap<String, GPSEscolas> aMap = new HashMap<String, GPSEscolas>();
private HashMap<String, GPSEscolas> HashTask = new HashMap<String, GPSEscolas>();
public static ArrayList<Filhos> listchild;
private GridAdapter adapter;
private SecurePreferences Sessao;
public static CustomGridView gridView;
private SparseArray<SparseBooleanArray> checkedPositions;
private static final String LOG_TAG = ExpandListAdapter.class.getSimpleName();
public ExpandListTest(Context context, ArrayList<CadastraEscolas> groups, HashMap<String, GPSEscolas> data, SecurePreferences mSessao, HashMap<String, GPSEscolas> hashTask) {
this.context = context;
this.groups = groups;
this.aMap = data;
this.Sessao = mSessao;
checkedPositions = new SparseArray<SparseBooleanArray>();
child = new ArrayList();
if (groups != null) {
for (int i = 0; i < groups.size(); i++) {
child.add(i, groups.get(i).getalunos());
}
}
}
#Override
public Object getChild(int groupPosition, int childPosition) {
return child.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, final ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.gridview, null);
}
listchild = new ArrayList<Filhos>();
for (int j = 0; j < groups.get(groupPosition).getalunos().size(); j++) {
listchild.add(child.get(groupPosition).get(j));
}
gridView = (CustomGridView) convertView.findViewById(R.id.GridView_toolbar);
gridView.setExpanded(true);
adapter = new GridAdapter(context, groups, listchild, child, groupPosition, aMap, HashTask, Sessao);
gridView.setAdapter(adapter);// Adapter
gridView.setChoiceMode(CustomGridView.CHOICE_MODE_MULTIPLE);
return convertView;
}
#Override
public int getChildrenCount(int nGroup) {
return 1;
}
#Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
#Override
public int getGroupCount() {
return groups.size();
}
#Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
CadastraEscolas group = (CadastraEscolas) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater inf = (LayoutInflater) context
.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inf.inflate(R.layout.group_item, null);
}
ExpandableListView mExpandableListView = (ExpandableListView) parent;
mExpandableListView.expandGroup(groupPosition);
TextView tv = (TextView) convertView.findViewById(R.id.group_name);
tv.setText(group.getNome_fantasia());
return convertView;
}
#Override
public boolean hasStableIds() {
return true;
}
#Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
Android ViewGroups can contain any number of Views, but Views can only have one parent ViewGroup, if you try to add a View that already has a parent, you will get this exception (from ViewGroup.addViewInner()):
throw new IllegalStateException("The specified child already has a parent. " +
"You must call removeView() on the child's parent first.");
This means that the structure of your layouts is that of a tree (not a graph), and so you can use any tree traversal algorithm to iterate through every View of your layout.
The following is one of the possible algorithms, which is a post-order traversal.
It goes through the root element, takes the first child and makes a recursive call for the algorithm, then its second, third etc...when there are no children left to go through, it calls your deselect function for the node.
For the tree in the diagram, nodes will be unselected in this order:
A,C,E,D,B,H,I,G, F
public void onClickTheButton(View view) {
unselectall(R.layout.your_layout);
}
public void unselectAll(View view) {
if(view instanceof ViewGroup) {
for(int ii = 0 ; ii<(ViewGroup)view.getChildrenCount(); ii++) {
unselectAll((ViewGroup)view.getChildAt(ii));
}
}
unselect(view);
}
You can find many other ways to do it here: https://en.wikipedia.org/wiki/Tree_traversal
It is a must to learn how those algorithms work if you want to ease your programming experience.
change your getView() method as follows.
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item, null);
holder = new ViewHolder();
final TextView idAluno = (TextView) convertView.findViewById(R.id.idcrianca);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(child.get(position).getNome());
final View finalConvertView = convertView;
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (list.size() > 0) {
isChecked = filhos.get(posicaoEscola).get(position).isChecked();
if (!isChecked) {
selecao(true, position, nomeAluno, 0xFFFFFFFF, finalConvertView, View.VISIBLE);
} else {
selecao(false, position, nomeAluno, 0xFFD5672B, finalConvertView, View.GONE);
}
ex.findViewById(R.id.notificar).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
limpaSelecao(false, position, nomeAluno, 0xFFD5672B, parent, View.GONE);
}
});
}
});
return convertView;
}

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!

Categories