我正在寻找一种将数据加载到ExpanDale List视图中的方法,我的输出类似于附件中的图片here
为了做到这一点,最好从csv读取?还是创建一个数据库?此外,在子项目上,我希望出现ScrollVIEw.
这是到目前为止的代码:
活动的布局:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <ExpandableListVIEw androID:ID= "@+ID/androID:List" androID:layout_wIDth="match_parent" androID:layout_height="match_parent"/> <TextVIEw androID:ID="@+ID/androID:empty" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" /></linearLayout>The itemlayout: <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <TextVIEw androID:ID="@+ID/grp_child" androID:paddingleft="50dp" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"/></linearLayout>
和子项目:
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="horizontal" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <TextVIEw androID:ID="@+ID/row_name" androID:paddingleft="50dp" androID:focusable="false" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"/></linearLayout>
到目前为止,在活动中,我使用了一些教程对一些值进行了硬编码:
public voID onCreate(Bundle savedInstanceState) { try{ super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_photographers); Simpleexpandablelistadapter explistadapter = new Simpleexpandablelistadapter( this, createGroupList(), R.layout.group_row, new String[] { "Group Item" }, new int[] { R.ID.row_name }, createChildList(), R.layout.child_row, new String[] {"Sub Item"}, new int[] { R.ID.grp_child} ); setlistadapter( explistadapter ); }catch(Exception e){ System.out.println("Errrr +++ " + e.getMessage()); } } /* Creating the Hashmap for the row */ @SuppressWarnings("unchecked") private List createGroupList() { ArrayList result = new ArrayList(); for( int i = 0 ; i < 15 ; ++i ) { // 15 groups........ HashMap m = new HashMap(); m.put( "Group Item","Group Item " + i ); // the key and it's value. result.add( m ); } return (List)result; } @SuppressWarnings("unchecked") private List createChildList() { ArrayList result = new ArrayList(); for( int i = 0 ; i < 15 ; ++i ) { // this -15 is the number of groups(Here it's fifteen) /* each group need each HashMap-Here for each group we have 3 subgroups */ ArrayList secList = new ArrayList(); for( int n = 0 ; n < 3 ; n++ ) { HashMap child = new HashMap(); child.put( "Sub Item", "Sub Item " + n ); secList.add( child ); } result.add( secList ); } return result; } public voID onContentChanged () { System.out.println("onContentChanged"); super.onContentChanged(); } /* This function is called on each child click */ public boolean onChildClick( ExpandableListVIEw parent, VIEw v, int groupposition,int childposition,long ID) { System.out.println("InsIDe onChildClick at groupposition = " + groupposition +" Child clicked at position " + childposition); return true; } /* This function is called on expansion of the group */ public voID onGroupExpand (int groupposition) { try{ System.out.println("Group expanding Listener => groupposition = " + groupposition); }catch(Exception e){ System.out.println(" groupposition Errrr +++ " + e.getMessage()); } }
我想知道将要显示的数据存储在单独的csv文件中(一个存储项目数据,一个存储子项目,一个存储ScrollVIEw中的附加信息)是否具有ID-s是一个好主意.进行识别并使用OpenCSVReader直接从CSV中读取?
我将不胜感激任何建议,
谢谢
解决方法:
为您的可扩展ListVIEw创建适配器.像那样
public class ExpandableAdapter extends Baseexpandablelistadapter { private final Context context; private final List<Menu> parentObjects; public ExpandableAdapter(Context context, ArrayList<Menu> parentObjects) { this.context = context; this.parentObjects = parentObjects; } @OverrIDe public int getGroupCount() { return parentObjects.size(); } @OverrIDe public int getChildrenCount(int i) { return parentObjects.get(i).childMenu.size(); } @OverrIDe public Menu getGroup(int i) { return parentObjects.get(i); } @OverrIDe public Menu.ChildMenu getChild(int i, int i2) { return parentObjects.get(i).childMenu.get(i2); } @OverrIDe public long getGroupID(int i) { return i; } @OverrIDe public long getChildID(int i, int i2) { return 0; } @OverrIDe public boolean hasStableIDs() { return false; } @OverrIDe public VIEw getGroupVIEw(int i, boolean b, VIEw vIEw, VIEwGroup vIEwGroup) { Menu currentParent = parentObjects.get(i); if (vIEw == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context .LAYOUT_INFLATER_SERVICE); vIEw = inflater.inflate(R.layout.navdrawer_parent_item, vIEwGroup,false); } ImageVIEw imageVIEwIndicator = (ImageVIEw) vIEw.findVIEwByID(R.ID.imageVIEwNav); if (getChildrenCount(i) == 0) imageVIEwIndicator.setVisibility(VIEw.GONE); else imageVIEwIndicator.setVisibility(VIEw.VISIBLE); TextVIEw textVIEwNavMenuname = (TextVIEw) vIEw.findVIEwByID(R.ID.textVIEwNavParentMenuname); ImageVIEw imageVIEwIcon = (ImageVIEw) vIEw.findVIEwByID(R.ID.imageVIEwIcon); String base64 = currentParent.getimage(); if (base64 != null && !base64.equals("")) { byte[] imageAsBytes = Base64.decode(currentParent.getimage().getBytes(), Base64 .DEFAulT); imageVIEwIcon.setimageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)); } else imageVIEwIcon.setimageResource(R.drawable.ic_action_androID); textVIEwNavMenuname.setText(currentParent.getMenuname()); return vIEw; } @OverrIDe public VIEw getChildVIEw(int groupposition, int childposition, boolean b, VIEw vIEw, VIEwGroup vIEwGroup) { Menu currentChild = getGroup(groupposition); if (vIEw == null) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context .LAYOUT_INFLATER_SERVICE); vIEw = inflater.inflate(R.layout.navdrawer_child_item, vIEwGroup,false); } VIEw divIDer = vIEw.findVIEwByID(R.ID.divIDer); if (b) divIDer.setVisibility(VIEw.VISIBLE); else divIDer.setVisibility(VIEw.GONE); TextVIEw textVIEwNavMenuname = (TextVIEw) vIEw.findVIEwByID(R.ID.textVIEwNavChildMenuname); textVIEwNavMenuname.setText(currentChild.childMenu.get(childposition).getMenuname()); return vIEw; } @OverrIDe public boolean isChildSelectable(int i, int i2) { return true; }}
以及可扩展ListVIEw的模型,例如
public class Menu { private int AssociatedApp; private String Menuname; private String NavigateURL; private String Activityname; private String Image; private int MenuID; public ArrayList<ChildMenu> childMenu; public ArrayList<ChildMenu> getChildMenu() { return childMenu; } public voID setChildMenu(ArrayList<ChildMenu> childMenu) { this.childMenu = childMenu; } public int getAssociatedApp() { return AssociatedApp; } public voID setAssociatedApp(int associatedApp) { AssociatedApp = associatedApp; } public String getMenuname() { return Menuname; } public voID setMenuname(String menuname) { Menuname = menuname; } public String getNavigateURL() { return NavigateURL; } public voID setNavigateURL(String navigateURL) { NavigateURL = navigateURL; } public String getActivityname() { return Activityname; } public voID setActivityname(String activityname) { Activityname = activityname; } public String getimage() { return Image; } public voID setimage(String image) { Image = image; } public Menu() { } public int getMenuID() { return MenuID; } public class ChildMenu { private int AssociatedApp; private String Menuname; private String NavigateURL; private String Activityname; private String Image; private int MenuID; public ChildMenu(String menuname, String activityname) { this.Menuname = menuname; this.Activityname=activityname; } public ChildMenu() { } public int getAssociatedApp() { return AssociatedApp; } public voID setAssociatedApp(int associatedApp) { AssociatedApp = associatedApp; } public String getMenuname() { return Menuname; } public voID setMenuname(String menuname) { Menuname = menuname; } public String getNavigateURL() { return NavigateURL; } public voID setNavigateURL(String navigateURL) { NavigateURL = navigateURL; } public String getActivityname() { return Activityname; } public voID setActivityname(String activityname) { Activityname = activityname; } public String getimage() { return Image; } public voID setimage(String image) { Image = image; } public int getMenuID() { return MenuID; } }}
好的,现在您可以使用数据创建菜单并使用可扩展列表视图进行绑定
elv = (ExpandableListVIEw)findVIEwByID(R.ID.elv); elv.setonGroupExpandListener(onGroupExpandListenser); MyExpandableAdapter adapter = new MyExpandableAdapter(this, getData());//where getData() will return List of data. elv.setAdapter(adapter);
父XML
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical"> <relativeLayout androID:layout_wIDth="fill_parent" androID:layout_height="match_parent" androID:paddingBottom="12dp" androID:paddingleft="12dp" androID:paddingtop="12dp"> <ImageVIEw androID:contentDescription="@string/app_name" androID:ID="@+ID/imageVIEwIcon" androID:layout_wIDth="35dp" androID:layout_height="35dp" androID:layout_alignParentleft="true" androID:layout_margin="4dp" /> <TextVIEw androID:ID="@+ID/textVIEwNavParentMenuname" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerInParent="true" androID:layout_centerVertical="true" androID:layout_toleftOf="@+ID/imageVIEwNav" androID:layout_toRightOf="@+ID/imageVIEwIcon" androID:padding="10dp" androID:textSize="15sp"/> <ImageVIEw androID:contentDescription="@string/app_name" androID:ID="@+ID/imageVIEwNav" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_margin="4dp" androID:src="@drawable/ic_action_more_nav" /> </relativeLayout> <include layout="@layout/divIDer" androID:layout_wIDth="fill_parent" androID:layout_height="fill_parent"/></linearLayout>
子xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:gravity="center_vertical" androID:orIEntation="vertical"> <TextVIEw androID:ID="@+ID/textVIEwNavChildMenuname" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_marginleft="45dp" androID:gravity="center_vertical" androID:text="TextVIEw" androID:padding="16dp"/> <include layout="@layout/divIDer" androID:layout_wIDth="match_parent" androID:layout_height="fill_parent"/></linearLayout>
像这样为可扩展ListVIEw添加侦听器. groupExpandListener用于在打开一个组时对其他组进行整理
private int lastExpandedposition = -1;
用于设置监听器
elv.setonChildClickListener(new DrawerItemClickListener()); elv.setonGroupClickListener(new DrawerItemClickListener()); elv.setonGroupExpandListener(new DrawerItemClickListener()); private class DrawerItemClickListener implements ExpandableListVIEw.OnChildClickListener, ExpandableListVIEw.OnGroupClickListener, ExpandableListVIEw.OnGroupExpandListener { @OverrIDe public boolean onChildClick(ExpandableListVIEw parent, VIEw v, int groupposition, int childposition, long ID) { selectItem(childposition, navigationConfig.getBaseexpandablelistadapter ().getChild (groupposition, childposition)); return true; } @OverrIDe public boolean onGroupClick(ExpandableListVIEw parent, VIEw v, int groupposition, long ID) { selectItem(groupposition, navigationConfig.getBaseexpandablelistadapter ().getGroup (groupposition)); return false; } @OverrIDe public voID onGroupExpand(int groupposition) { if (lastExpandedposition != -1 && groupposition != lastExpandedposition) { expandableListVIEw.collapseGroup(lastExpandedposition); } lastExpandedposition = groupposition; }}
样本数据用于与getData()绑定.注意在模型类中创建构造函数,如getData()所示
//Sample data for expandable List vIEw. public List<Menu> getData() { List<Menu> parentObjects = new ArrayList<Menu>(); for (int i = 0; i<20; i++) { parentObjects.add(new Menu("Mother " +i, "Father " +i, "header " + i, "Footer " +i, getChildren(i))); } return parentObjects; } private List<Menu.ChildMenu> getChildren(int childCount) { List<Menu.ChildMenu> childobjects = new ArrayList<Menu.ChildMenu>(); for (int i =0; i<childCount; i++) { childobjects.add(new Menu.ChildMenu("Child " + (i+1), 10 +i )); } return childobjects; }
总结 以上是内存溢出为你收集整理的android-将数据加载到ExpandableListView中全部内容,希望文章能够帮你解决android-将数据加载到ExpandableListView中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)