Recent projects need to use the form of grouping display data, drawing on the essence of the online predecessors, now write a summary, stay as a personal view.
First, to define list<baseinfo> datas = new arraylist<baseinfo> () for populating the ListView data;
In Baseinfo, you first need to define a classname to differentiate the category so that it can be displayed
Before each set of data is stored, a Baseinfo object is stored in the datas with only ClassName, and then the group data is stored in the Datas
Then populate the data in the form of Adapter fill data
In Adapter, the corresponding layout file needs to be introduced for Baseinfo objects with only className not empty
The above is the logical thinking of the ListView grouping
The following is a partial code for personal use:
Data processing code in activity:
JSONArray array_obj = response . Getjsonarray ("obj"); for (Int i = 0; i < array_obj.length (); i++) { jsonobject obj = array_obj.getjsonobject ( i); string classname = obj.getstring ("name"); string classid = obj.getstring ("id" ); Jsonarray array = obj .getjsonarray ("Appproductclientdtolist"); if (Array.Length () == 0) { // do nothing } else { baseinfo base = new baseinfo (); base.setclassname (ClassName); base.setclassid (classId); datas.add (Base); for (int j = 0; j < array.length (); j++) { baseinfo baseinfo = new baseinfo (); // Data Acquisition baseinfo.setclassname (ClassName); baseinfo.setclassid (classId); Baseinfo.setscale (scale); &NBSP;&NBSP;&NBSP;&Nbsp; baseinfo.setappid (appId); Baseinfo.setdownloadnumber (downloadcounts); Baseinfo.setversioncode (integer .parseint (VersionCode)); baseinfo.setsize (appsize); baseinfo.setdownloadurl (Appfilepath); &nbsP; baseinfo.setpackagename (PackageName); baseinfo.setname (name); datas.add (Baseinfo); } } }
Fill adapter After data is populated:
adapter = new Splitclassadapter (Getapplicationcontext (), datas); Adapter.notifydatasetchanged (); List_classes.setadapter (adapter);
Processing logic in Adapter:
if (Mdatas.get ( Position). GetName () ==null) { views = layoutinflater.from (Mcontext). Inflate (R.layout.activity_header, group , false); header = new viewholderheader (); // ViewHolderHeader Component initialization, if there are processing events, the corresponding implementation . }else{ views = layoutinflater.from (Mcontext). Inflate ( R.layout.item_class, group,false); content = new viewholdercontent(); // The viewholdercontent component is initialized and, if there is a handling event, it is implemented accordingly.
Also need to implement areallitemsenabled and isenabled two methods in adapter
@Override public boolean Areallitemsenabled () { return true; } @Override public boolean isenabled (int position) { // If-start, the item is not selectable baseinfo baseinfo = mdatas.get (position); if (Baseinfo.getname () ==null) { return false; } else{ return true; } }
ListView Group Displays network data