Use of expandablelist in Android 2

Source: Internet
Author: User
Use of expandablelist in Android 2

This article introduces the use of expandablelist in Android, and makes many improvements based on the previous article, adding, deleting, and callback functions.

In the figure, "first line team no.: 1 class 2 of software engineering" is the information to be displayed.

First look:

 

First, define a layout containing expandablelistview. There is also text information displayed.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <ExpandableListView        android:id="@+id/expandableListView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/textView1"        android:layout_below="@+id/textView1"        android:layout_marginTop="52dp" >    </ExpandableListView></RelativeLayout>

 

2. Prepare a data source for expandablelistview.

The classgroups List defines all classes.

Classdetails defines detailed information about the class. Including the class number, class name, class number, and teacher.

Private list <string> classgroups = new arraylist <string> (); Private list <string> classdetails = new arraylist <list <string> (); private string [] [] classdetailssource = {"20100801", "Software Engineering Class 1", "50", "instructor Wang" },{ "20100802 ", "Software Engineering Class 2", "47", "teacher" },{ "20100803", "network engineering class 1", "52" },{ "20100804 ", "Computer Science and Technology "}};

Use the additem method to initialize four classes.

Private void pareparedatasource () {for (INT I = 0; I <4; I ++) {additem ("Team ID:" + I, classdetailssource [I]);}

Add classdetails to all classes.

private void addItem(String string, String[] data) {// TODO Auto-generated method stubClassGroups.add(string);List<String> item = new ArrayList<String>();for (int i = 0; i < data.length; i++) {item.add(data[i]);}ClassDetails.add(item);}

 

 3. Use baseexpandablelistadapter to bind the data source.This article has been introduced in my previous article and is not described here.

@ Overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); pareparedatasource (); tview = (textview) findviewbyid (R. id. textview1); expandablelistview = (expandablelistview) findviewbyid (R. id. expandablelistview1); expandablelistview. setadapter (New expandablelistadapter (); // The callback function of expandablelistview is used to listen to the ID that is expandexpandablelistview. setongroupclicklistener (New ongroupclicklistener () {@ overridepublic Boolean ongroupclick (expandablelistview parent, view V, int groupposition, long ID) {// todo auto-generated method stubtoast. maketext (expandablelist3.this, "you click" + groupposition + "group", toast. length_long ). show (); Return false ;}}); expandablelistview. setonchildclicklistener (New onchildclicklistener () {@ overridepublic Boolean onchildclick (expandablelistview parent, view V, int groupposition, int childposition, long ID) {// todo auto-generated method stubtoast. maketext (expandablelist3.this, "you click" + childposition + "child in group" + groupposition, toast. length_long ). show (); Return false ;}});}

 

Note: expandablelistview. setongroupclicklistener (New ongroupclicklistener () and expandablelistview. setonchildclicklistener (New onchildclicklistener () are callback methods.

The data source form of expandablelistadapter is as follows:

public class ExpandableListAdapter extends BaseExpandableListAdapter {@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubString string = ClassGroups.get(groupPosition)+ ClassDetails.get(groupPosition).get(childPosition); 
tView.setText(string);return true;}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn true;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {// TODO Auto-generated method stubLinearLayout layout = new LinearLayout(ExpandableList3.this);layout.setOrientation(0);layout.setPadding(50, 0, 0, 0);ImageView imageView = new ImageView(ExpandableList3.this);imageView.setImageResource(R.drawable.ic_launcher);layout.addView(imageView);TextView textView = getTextView();textView.setText(getGroup(groupPosition).toString());layout.addView(textView);return layout;}private TextView getTextView() {// TODO Auto-generated method stubAbsListView.LayoutParams lParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, 64);TextView textView = new TextView(ExpandableList3.this);textView.setLayoutParams(lParams);textView.setPadding(20, 0, 0, 0);textView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);return textView;}@Overridepublic long getGroupId(int groupPosition) {// TODO Auto-generated method stubreturn groupPosition;}@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn ClassGroups.size();}@Overridepublic Object getGroup(int groupPosition) {// TODO Auto-generated method stubreturn ClassGroups.get(groupPosition);}@Overridepublic int getChildrenCount(int groupPosition) {// TODO Auto-generated method stubreturn ClassDetails.get(groupPosition).size();
}@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {// TODO Auto-generated method stubTextView textView = getTextView();textView.setText(getChild(groupPosition, childPosition).toString());return textView;}@Overridepublic long getChildId(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn childPosition;}@Overridepublic Object getChild(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn ClassDetails.get(groupPosition).get(childPosition); }};

 

2. Create two menus and call their respective methods. One is to create the class dialog, and the other is to create and delete the dialog.

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);menu.add(Menu.NONE, Menu.FIRST + 1, 1, "Add").setIcon(R.drawable.ic_launcher);menu.add(Menu.NONE, Menu.FIRST + 2, 2, "Delete").setIcon(R.drawable.ic_launcher);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {switch (item.getItemId()) {case Menu.FIRST + 1:createAddDialog();break;case Menu.FIRST + 2:createDeleteDialog();break;}return false;}

 

1. The added layout add_item.xml is as follows: four texts, one confirmation button, and one cancel button are put in the text box respectively.

<? 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 = "match_parent" Android: Orientation = "vertical"> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_margin = "5dp" Android: text = "class number"/> <edittext Android: Id = "@ + ID/add_item1" Android: layout_width = "200dip" Android: layout_height = "wrap_content" Android: layout_margin = "5dp"/> </linearlayout> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: orientation = "horizontal"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_margin = "5dp" Android: TEXT = "Class Name"/> <edittext Android: Id = "@ + ID/add_item2" Android: layout_width = "200dip" Android: layout_height = "wrap_content" Android: layout_margin = "5dp"/> </linearlayout> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_margin = "5dp" Android: text = "class size"/> <edittext Android: id = "@ + ID/add_item3" Android: layout_width = "200dip" Android: layout_height = "wrap_content" Android: layout_margin = "5dp"/> </linearlayout> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_margin = "5dp" Android: text = ""/> <edittext Android: Id = "@ + ID/add_item4" Android: layout_width = "200dip" Android: layout_height = "wrap_content" Android: layout_margin = "5dp"/> </linearlayout> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: orientation = "horizontal"> <button Android: Id = "@ + ID/OK" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_margin = "5dp" Android: text = "OK"/> <button Android: Id = "@ + ID/cancle" Android: layout_width = "200dip" Android: layout_height = "wrap_content" Android: layout_margin = "5dp" Android: text = "cancle"/> </linearlayout>

 

2. The added code is as follows: add the class information by additem (class_no.gettext (). tostring (), data. Close the current dialog when you cancel the button.

private EditText class_no;private EditText class_name;private EditText stu_num;private EditText teacher;private Dialog addDialog;public void createAddDialog() {View addView = getLayoutInflater().inflate(R.layout.add_item, null);addDialog = new Dialog(this);addDialog.setContentView(addView);addDialog.setTitle("Add class dialog");class_no = (EditText) addView.findViewById(R.id.add_item1);class_name = (EditText) addView.findViewById(R.id.add_item2);stu_num = (EditText) addView.findViewById(R.id.add_item3);teacher = (EditText) addView.findViewById(R.id.add_item4);Button ok = (Button) addView.findViewById(R.id.ok);Button cancle = (Button) addView.findViewById(R.id.cancle);ok.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString[] data = { class_no.getText().toString(),class_name.getText().toString(), stu_num.getText().toString(),teacher.getText().toString() };addItem(class_no.getText().toString(), data);addDialog.dismiss();}});cancle.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubaddDialog.dismiss();}});addDialog.show();}

 

3. added the following:

 

4. Similarly, layout is delete_item.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 = "match_parent" Android: Orientation = "vertical"> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "deleted team ID:"/> <edittext Android: Id = "@ + ID/delete_id" Android: layout_width = "200dip" Android: layout_height = "wrap_content"/> </linearlayout> <linearlayout Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Orientation = "horizontal"> <button Android: id = "@ + ID/Delete" Android: layout_width = "90dip" Android: layout_height = "wrap_content" Android: text = "delete"/> <button Android: id = "@ + ID/cancle_delete" Android: layout_width = "90dip" Android: layout_height = "wrap_content" Android: text = "canlce"/> </linearlayout>

 

5. The deletion code is: Enter the number, for example, 2. delete all the information of the class whose team number is 2. The classgroups. Remove (I); classdetails. Remove (I); method is called.

Dialog deleteDialog;EditText delete_group_id;public void createDeleteDialog() {View deleteView = getLayoutInflater().inflate(R.layout.delete_item,null);deleteDialog = new Dialog(this);deleteDialog.setContentView(deleteView);deleteDialog.setTitle("Delete class dialog");delete_group_id = (EditText) deleteView.findViewById(R.id.delete_id);Button ok = (Button) deleteView.findViewById(R.id.delete);Button cancle = (Button) deleteView.findViewById(R.id.cancle_delete);ok.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString deleteId = delete_group_id.getText().toString();if (!deleteId.equals("")) {int i = Integer.parseInt(deleteId);ClassGroups.remove(i);ClassDetails.remove(i);}deleteDialog.dismiss();}});cancle.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubdeleteDialog.dismiss();}});deleteDialog.show();}

 

6. Delete the following:

 

 

 

Iii. Use of callback Functions.

When you click a group, the group information is displayed. Click item information to display the information of the combined item. For example, the information shown in the bottom right corner of the first image.

// The callback function of expandablelistview is used to listen to the ID that is expandexpandablelistview. setongroupclicklistener (New ongroupclicklistener () {@ overridepublic Boolean ongroupclick (expandablelistview parent, view V, int groupposition, long ID) {// todo auto-generated method stubtoast. maketext (expandablelist3.this, "you click" + groupposition + "group", toast. length_long ). show (); Return false ;}}); expandablelistview. setonchildclicklistener (New onchildclicklistener () {@ overridepublic Boolean onchildclick (expandablelistview parent, view V, int groupposition, int childposition, long ID) {// todo auto-generated method stubtoast. maketext (expandablelist3.this, "you click" + childposition + "child in group" + groupposition, toast. length_long ). show (); Return false ;}});

The use of expandablelist is described here. This article also records your learning situation, so that you can learn it again later.

 

Related Article

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.