Android data binding in extensible list

I have a very specific question. I am using android data binding library: https://developer.android.com/topic/libraries/data-binding/index.html

I have a datamodel like this:

Class Participant Public String Name; //and so on Public ObservableArrayList<Drink> DrinkList; End Class Class Drink extends BaseObservable @Bindable Public String Name; @Bindable Public Float Price; End Class 

ViewModel:

 Class ParticipantList public ObservableArrayList<Participant> list = new ObservableArrayList<>(); //some Methods to add items and concstructor 

I also use Getter and Setter for all @Bindable fields.

Now I have activity with the listview extension, where I bind the adapter to the expandable list via XML and a custom @BindingAdapter as follows:

 //in XML for the layout <layout xmlns:bind="http://schemas.android.com/apk/res-auto" <data> <variable name="Participants" type="com.example.ParticipantList"/> </data> <ExpandableListview> <bind:participants=Particpants.list> 

In a helper helper class, this static method

  @BindingAdapter("participants") public static void bindList(ExpandableListView view, ObservableArrayList<Participant> list) { ParticipantListAdapter adapter = new ParticipantListAdapter(list); view.setAdapter(adapter); } 

and finally my adapter:

 Class ParticpantListAdapter Extends ExpandableListAdapter { public ObservableArrayList<Participant> list; @Override public View getGroupView(..) ParticipantListItemBinding binding = DataBindingUtil.inflate(inflater,R.layout.participant_list_item, parent, false); binding.setParticipant(list.get(groupposition)); return binding.getRoot(); } @Override public View getChildView(..) { DrinkListItemBinding binding = DataBindingUtil.inflate(inflater, R.layout.drink_list_item, parent, false); binding.setDrink(list.get(goupposition).DrinkList.get(childposition)); return binding.getRoot(); } 

I have 2 more xml LayoutFiles with data binding layout.drink_list_item + layout.participant_list_item that are bound to the party and class of the drink.

I missed a lot of code, which is not very important here. Everything works just fine, except that I have a button in my layout.participant_list_item that will open a new event where I can add a new drink to my drink list in my tables. When I complete the action, a new child should be added to the group. But this will only appear after I collapse and ungroup the group once. Usually data binding does this for me when I call NotifyPropertychanged for the @Bindable field or add a new element to the directly related ObservableArrayList. But this is not so. I am adding an element to an ObservableArrayList element in an ObservableArrayList. Is there a way to update only the children of this group? Or is my approach somehow wrong?

My dirty decision is that I tell my main activity binding in my onResume event that if a certain condition is met, it should update all the bindings, which leads to a complete redraw of the ExpandableListView object and all elements will be collapsed again (which is not desirable behavior).

I wrote all the code from my memory since I am not in my development location at the moment. But this problem has bothered me since then, and I have not yet found a decent example of Databinding and ExpandableListView. If you need more information, just ask.

I am happy for any tips that help me find a good solution.

This is my first question, so please be kind if the message does not meet all the requirements.

Thank you in advance

// Change. Edited code. Does anyone here have any helpful comments? You need more code or how can I get some tips.

+5
source share
1 answer

Try it.

 public class IExpandableListViewAdapter extends BaseExpandableListAdapter { @Override public int getGroupCount() { return 0; } @Override public int getChildrenCount(int i) { return 0; } @Override public Object getGroup(int i) { return null; } @Override public Object getChild(int i, int i1) { return null; } @Override public long getGroupId(int i) { return 0; } @Override public long getChildId(int i, int i1) { return 0; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { if(view == null) { parentBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.basket_list_layout, viewGroup, false); view = parentBinding.getRoot(); } else { parentBinding = (BasketListLayoutBinding)view.getTag(); } parentBinding.setBasketParentModel(parent.get(i)); view.setTag(parentBinding); return view; } @Override public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) { final int index = i; if(view == null) { childBinding = DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.basket_detail_layout, viewGroup, false); view = childBinding.getRoot(); } else { childBinding = (BasketDetailLayoutBinding) view.getTag(); } childBinding.setBasketChildModel(parent.get(i).getBasketChildModel()); view.setTag(childBinding); return view; } @Override public boolean isChildSelectable(int i, int i1) { return false; } } 
+2
source

All Articles