Android Adapter Datamodifyhelper data Manipulation class encapsulation

Source: Internet
Author: User

The adapter code is often plagued by several issues: 1. Maintenance of the same set of data in the business layer and in the adapter is difficult to manage 2. The adapter update must be notified after the business layer modifies the data, otherwise promptThe content of the adapter has changed but a ListView did not receive anotification
3. The business layer is flooded with redundant code such as non-null & data Normalization when data is modified
for the first two issues, the data can be transferred to the adapter to manage, business layer of data additions and deletions are processed through the adapter, so only need to maintain good adapter data.
The data operation class can be extracted to simplify the logic in the adapter, for the non-null judgment of the data and the operation of adding and deleting.
The following shows the specific code to implement the adapter Update mode interface definition, its implementation can be based on the adapter type (Baseadapter/pageadapter/recyclerview.adapter) self-tuning.
/**
* @Description:
* @author:Xiaoxuan948
* @date:2016/7/20 20:39
*/
Public InterfaceInotifyadapterdatasetchange {
/**
* Notification Adapter update
*/
voidNotifyunityadapterdatachange();

voidNotifyunityadapterdatainsert(intPosition;

voidnotifyunityadapterdataupdate(intPosition;

voidNotifyunityadapterdataremove(intPosition;
}
Interface Iadapterdatamodifyhelper defines various operations for the data, and its subclasses are absdatamodifyhelper for a brief implementation.
Public abstract classabsdatamodifyhelper<T>Implementsiadapterdatamodifyhelper<T> {
Private FinalUtilslogLG= Utilslog.GetLogger(Absdatamodifyhelper.class);
protectedlist<T>mdataresources;
protectedInotifyadapterdatasetchangeInotifydatasetchange;

PublicAbsdatamodifyhelper () {
This.mdataresources=NewArraylist<> ();
}

@Override
Public final voidSetnotifyadapterdatasetchange(Inotifyadapterdatasetchange Inotifydatasetchange) {
This.Inotifydatasetchange= Inotifydatasetchange;
}

@Override
Public Finallist<T>getdataresources() {
returnmdataresources;
}
Omit part of the code
}
the observation indicates that theAbsdatamodifyhelperinstantiation in a constructor methoda Mdataresources object that corresponds to the data displayed in the list control. Setnotifyadapterdatasetchange provides an interface injection method for external data updates, and calls Inotifydatasetchange's notify after data modification ... () method to notify the adapter of updates.

Developers can extend the absdatamodifyhelper themselves, and a sample implementation class Datamodifyhelper is given below.
/**
* @Description:
* @author:Xiaoxuan948
* @date:2016/8/26 10:04
*/
Public classdatamodifyhelper<T>extendsabsdatamodifyhelper<T> {
Private FinalUtilslogLG= Utilslog.GetLogger(Datamodifyhelper.class);

@Override
Public voidSetdataresource(LIST&LT;?extendsT> setdataresources) {
/ * processing for collection of empty data */
if(Utilscollections.Iscollectionnotempty(setdataresources) && setdataresources.contains (NULL)) {
LG. E ("Setdataresource collection contains null data");
Setdataresources = Lists.newarraylist(Collections2.Filter(setdataresources, Newpredicate<T> () {
@Override
Public BooleanApply(TInput) {
returnInput! =null;
}
}));
}
if(! Utilscollections.Iscollectionnotempty(setdataresources)) {
return;
}
/ * processing for type mismatch */
This.mdataresources= Lists.Transform(setdataresources, NewFunction<object, T> () {
@Override
PublicTApply(Object input) {
return(T) input;
}
});
Inotifydatasetchange. Notifyunityadapterdatachange ();
}

@Override
Public voidAdddataresource(intLocation, list<?extendsT> adddataresources) {
if(Utilscollections.Iscollectionnotempty(adddataresources) && adddataresources.contains (NULL)) {
LG. E ("Adddataresource collection contains null data");
Adddataresources = Lists.newarraylist(Collections2.Filter(adddataresources, Newpredicate<T> () {
@Override
Public BooleanApply(TInput) {
returnInput! =null;
}
}));
}
off(! Utilscollections.Iscollectionnotempty(adddataresources)) {
LG. E ("Adddataresource: The set to be added is empty");
return;
}
Location = proofoperatedatalocation (location);
This.mdataresources. AddAll (Location, Adddataresources);
Inotifydatasetchange. Notifyunityadapterdatainsert (location);
}

@Override
Public voidRevisedataresource(intPosition, TEnity) {
if(Isoperatelocationright (position)) {
This.mdataresources. Set (position, enity);
Inotifydatasetchange. Notifyunityadapterdataupdate (position);
}Else{
LG. E ("Revisedataresource failed because position out of size"+ This.mdataresources. Size ());
}
}

@Override
Public voidremovedataresourceonposition(intPosition) {
This.mdataresources. Remove (position);
Inotifydatasetchange. Notifyunityadapterdataremove (position);
}

@Override
Public voidClear() {
This.mdataresources. Clear ();
Inotifydatasetchange. Notifyunityadapterdatachange ();
}
}
In the Setdataresource method, a non-null filter is first performed against the list data, and then the data is modified after the list is not empty to ensureThe mdataresources collection is always non-empty. It is noted here that its parameters use unrestricted wildcard characters, mainly to improve the extensibility of the program, when the adapter is based on interface programming can be convenient set data. (UsingReasons for Transform:list<child>cannot be converted tolist<parent>)The implementation process of Adddataresource is consistent with Setdataresource, which is a series of processing such as non-null filtering and judgment for data sets.
SOURCE Link: https://github.com/xiaoxuan948/AndroidUnityLab/tree/master/unity_base_dev_helper/src/main/java/com/coca/ Unity_base_dev_helper/adapter
Examples of Use (blog Park can be entered via the left "search"): Android adapter based on Baseadapter definition Abslistview Universal Adapter
The use of the V7 expansion pack Recyclerview of the Android adapter and the encapsulation implementation of the Universal adapter
Definition of Android Adapter Pageradapter Universal Adapter




From for notes (Wiz)

Android Adapter Datamodifyhelper data Manipulation class encapsulation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.