In the previous article, the ListView group and the alphabetical index navigation were reconstructed and reconstructed, so that the new implementation depended only on the interface, not the specific Bo. However, the original data Bo is required to implement an interface or inherit an abstract class. Can this step be simplified, just the raw data bolist? The answer is yes, you can use annotations, that is, annnotion.
Ideas and ideas
Java annotations are also called Java annotations, and Java provides a set of mechanisms that allow us to add standards (that is, some information) to methods, classes, parameters, packages, fields, and variables. And, at a later time, the annotated information is extracted for use by reflection.
Group ListView After the last refactoring, the most needed information is "which field is used for group navigation", so we just use annotations to mark this information out.
Also need to save some pinyin, such as the cache, then you can wrap the original Bo, the use of generics. Step by step below.
Annotations and Bo Packaging
Annotations as long as the tag is sorted by which field in Bo, and the annotation information remains at run time:
@Target (Elementtype.type) @Documented @retention (retentionpolicy.runtime) public @interface rulersortstr { String SortField ();}
Bo is packaged with a generic type, the wrapper class implements the previous interface Bosort or inherits the abstract class Defaultbosortimp, and the Getsortstr uses Bo's annotated information when implementing the method.
public class Rulerlistdatawrapper<t> extends defaultbosortimp{ private T data; Public Rulerlistdatawrapper () { super (); } Public Rulerlistdatawrapper (T data) { super (); This.data = data; } Public T GetData () { return data; } public void SetData (T data) { this.data = data; } /** * Use the annotation information of T to implement this method * /@Override public String getsortstr () { class<?> temp = Data.getclass (); Rulersortstr anno = temp.getannotation (rulersortstr.class); if (anno==null) { return ""; } String SortField = Anno.sortfield (); Field F; try { f = Temp.getdeclaredfield (SortField); F.setaccessible (true); Return (String) f.get (data); } catch (Exception e) { e.printstacktrace (); Return "";}} }
Rulerutil Re-construction
This tool class is also refactored into generics, which constructs the original data Bo list list<t> as the wrapper type list<rulerlistdatawrapper<t>>. The added label is also the wrapper type (rulerlistdatawrapper<t>), which, after generics, is no longer dependent on the specific Bo.
public class Rulerutil<t> {/** * list adaptation?? */public static final string[] Indexstr = {"#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L "," M "," N "," O "," P "," Q "," R "," S "," T "," U "," V "," W "," X "," Y "," Z "}; public static final char[] letters = {' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z '}; /** * @throws illegalaccessexception * @throws instantiationexception * @date 2014-7-28 * @return return processed data * @Description: Process data, sort, add tags */public list<rulerlistdatawrapper<t>> gensorteddataandtaglocation (L Ist<t> MyData, hashmap<string, integer> taglocation) throws Instantiationexception, illegalaccessexception {list<rulerlistdatawrapper<t>> res = getList (myData); First sort Collections.sort (res, new Comparator<rulerlistdatawrappEr<t>> () {@Override public int compare (rulerlistdatawrapper<t> lhs, Rulerlistdatawrapp Er<t> RHS) {char Firchar = Checkandgetpinyin (LHS); Char Secchar = Checkandgetpinyin (RHS); if (Firchar < Secchar) {return-1; } else if (Firchar > Secchar) {return 1; } else return 0; } }); int size = Res.size (); int i = 0; Char Nowtag = ' + '; for (i = 0; i < size; i++) {Rulerlistdatawrapper<t> temp = res.get (i); Char Temptag = checkandgetpinyin (temp); if (Arrays.binarysearch (Letters, Temptag) < 0) {Temptag = ' # '; } if (Nowtag! = Temptag) {rulerlistdatawrapper<t> Tagbo = new Rulerlistdatawrapper<t> ( ); Tagbo.settag (temptag+ ""); Res.add (i, Tagbo); TaglocatiOn.put (Temptag + "", I); i++; size++; Nowtag = Temptag; }} taglocation.put ("#", 0); return res; } private char Checkandgetpinyin (Bosort bo) {String pinyinstr = Bo.getsortstrpinyin (); if (pinyinstr==null) {Bo.setsortstrpinyin (Hanzitopinyin.getpinyin (Bo.getsortstr ()). toUpperCase ()); Pinyinstr = Bo.getsortstrpinyin (); } if (Pinyinstr!=null&&pinyinstr.length () >=1) {return Pinyinstr.charat (0); } return ' to '; } public list<rulerlistdatawrapper<t>> getList (list<t> data) {List<rulerlistdatawrap per<t>> res = new arraylist<rulerlistdatawrapper<t>> (); for (t t:data) {Res.add (new rulerlistdatawrapper<t> (t)); } return res; }
Activity and Adaptor
are also generics.
1.adaptor
The first is adaptor, and the data required is list<rulerlistdatawrapper<t>> After generics, the following:
public class Ruleradapter<t> extends baseadapter{/** * processed data */private List<rulerlistdatawrapp Er<t>> list; /** * AA */private Context mcontext; /** * * */Public Ruleradapter () {super (); TODO auto-generated Constructor stub}/** * @param list */public Ruleradapter (List<rulerlistdat Awrapper<t>> list) {super (); This.list = list; }/** * */Public Ruleradapter (list<rulerlistdatawrapper<t>> List, Context mcontext) {su Per (); This.list = list; This.mcontext = Mcontext; } public list<rulerlistdatawrapper<t>> GetList () {return List; } public void setlist (list<rulerlistdatawrapper<t>> list) {this.list = list; } public Context Getmcontext () {return mcontext; } public void Setmcontext (Context mcontext) {this.mcontext = Mcontext; } @Override public int GetCount () {return list.size (); } @Override public rulerlistdatawrapper<t> getItem (int arg0) {//TODO auto-generated method stub Return List.get (arg0); } @Override public long getitemid (int position) {return 0; }/** * @date 2014-9-3 * @Description: Override this method when needed, provide your own custom non-tabbed view * @param * @return View */PU Blic View getcontentview (int position,rulerlistdatawrapper<t> bo) {return null; } @Override public View getView (int position, view Convertview, ViewGroup parent) {Viewholder holder; if (Convertview = = null) {Convertview = Layoutinflater.from (Mcontext). Inflate (R.layout.g_ruler_list_item, NULL); Holder = new Viewholder (); Holder.tag = (TextView) Convertview.findviewbyid (R.id.g_ruler_list_item_tag); Holder.content = (linearlayout) Convertview.findviewbyid (R.id.g_ruler_list_item); Convertview.settag (holder); } else {holder = (Viewholder) convertview.gettag (); }//dependent on rulerlistdatawrapper<t> final rulerlistdatawrapper<t> bo = getItem (position); String tag = Bo.gettag (); if (tag!=null) {//Is the label holder.tag.setVisibility (view.visible); Holder.content.setVisibility (View.gone); Holder.tag.setText (tag); }else{//is content//first taken from a defined view, if not, customize a TextView add to content holder.content.removeAllViews (); Holder.tag.setVisibility (View.gone); Holder.content.setVisibility (view.visible); View Contentview = Getcontentview (position, Bo); if (contentview==null) {TextView TEXTV = new TextView (mcontext); Textv.settext (Bo.getsortstr ()); Textv.settextcolor (Color.Black); Textv.setgravity (gravity.center_vertical); Textv.settextsize (16); Contentview = TEXTV; } holder.content.addView (Contentview, layoutparams.wrap_content, layoutparams.wrap_content); } return Convertview; } static class viewholder{TextView tag; LinearLayout content; }
2.Activity
Activity is also generic, mainly the invocation of Rulerutil and the acquisition of data when generics, its basic code is as follows:
Public abstract class Ruleractivity<t> extends activity{protected TextView Nodataview; protected TextView Rulertag; protected Progressbarwithtext progress; Protected ListView ListView; protected Rulerwidget ruler; Private ruleradapter<t> Ruleradapter; Private list<t> originallist; Private list<rulerlistdatawrapper<t>> dealedlist; Private hashmap<string, integer> taglocation = new hashmap<string, integer> (); /** * */@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (Savedinstanc EState); Setcontentview (R.layout.g_ruler); Findview (); Initview (); InitData (); } private void Findview () {...} private void Initview () {...} private void InitData () {new Getdataasytask (). Execute (); }/** * @date 2014-9-4 * @Description: Need to implement this method of obtaining data * @param * @return List<? Extends bosort> */public abstract list<t> getdatalist (); /** * @date 2014-9-3 * @Description: * @param * @return void */private void Handlesuccessdata () { . . . } /** * @Description: Gets the position of the list to scroll to when navigating the letter with the touch. If the touch of the letter, in the label taglocation mapping, does not exist, then look forward. */Private Integer getPosition (final int j) {...} Class Getdataasytask extends Asynctask<void, void, void> {@Override protected void O Npreexecute () {super.onpreexecute (); Progress.setvisibility (view.visible); } @Override protected void Doinbackground (void ... params) {originallist = Getdatalist (); try {dealedlist = new rulerutil<t> (). Gensorteddataandtaglocation (Originallist, taglocation); } catch (Exception e) {e.printstacktrace (); if (dealedlist!=null) {dealedlist.clear (); Dealedlist = null; } if (Originallist!=null) {originallist.clear (); Originallist = null; } if (Taglocation!=null) {taglocation.clear (); Taglocation = null; }} return null; } @Override protected void onpostexecute (void result) {progress.setvisibility (view.gone); Super.onpostexecute (result); if (dealedlist==null) {nodataview.setvisibility (view.visible); Return } handlesuccessdata (); } } }
Test using
The test is used as long as you inherit the meaning activity and use annotations on Bo:
As follows:
1. Bo
Using annotations,@RulerSortStr(sortfield="testtr"), the table name uses the grouping index field testtr.
@RulerSortStr (sortfield= "testtr") public class Testannotationbo { private String testtr; Private String test2; /** * * * /public Testannotationbo () { super (); TODO auto-generated constructor stub } public Testannotationbo (string testtr, String test2) { super (); c11/>this.testtr = testtr; This.test2 = test2; } Public String gettesttr () { return testtr; } public void Settesttr (String testtr) { this.testtr = testtr; } Public String GetTest2 () { return test2; } public void SetTest2 (String test2) { this.test2 = test2; } }
2. Activity
Just inherit the above activity and implement the method interface to get the data
public class Testrulerannoactivity extends ruleractivity<testannotationbo>{ @Override public list< Testannotationbo> getdatalist () { list<testannotationbo> res = new arraylist<testannotationbo> (); Testannotationbo a = new Testannotationbo ("AA", "SDDS"); Testannotationbo A1 = new Testannotationbo ("Aunty", "SDDS"); Testannotationbo B = new Testannotationbo ("BB", "SDDS"); Testannotationbo B1 = new Testannotationbo ("Papa", "Sdds"); Res.add (B1); Res.add (a); Res.add (A1); Res.add (b); return res; }}
Refactoring is complete.
Android list grouping and letter navigation-refactoring (2)-annotations