有没有人知道如何从ExpandableListView移动到另一个页面。我是android的新手,所以这有点棘手。到目前为止,我的ELV有标题和孩子。但是,我想要的是当有人点击其中一个孩子时,它会将它们发送到另一个页面。我失去了,所以任何帮助都会受到赞赏。
以下是我迄今为止所做的代码:
public class MainMenu extends Activity {
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_menu);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.expandableListView1);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
}
/ *
* Preparing the list data
*/
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Lesson 1");
listDataHeader.add("Lesson 2");
listDataHeader.add("Lesson 3");
listDataHeader.add("Lesson 4");
listDataHeader.add("Lesson 5");
// Adding child data
List<String> Lesson1 = new ArrayList<String>();
Lesson1.add("The Alphabet");
Lesson1.add("Common Expressions");
Lesson1.add("Vocabulary: Things in a Room");
Lesson1.add("Grammar: Demonstrative Pronouns");
Lesson1.add("Questions and Answers");
List<String> Lesson2 = new ArrayList<String>();
Lesson2.add("Numbers 1-10");
Lesson2.add("Common Expressions");
Lesson2.add("Vocabulary: Classroom Tools");
Lesson2.add("Grammar: Plurals");
Lesson2.add("Grammar: Using Plurals");
Lesson2.add("Questions and Answers");
List<String> Lesson3 = new ArrayList<String>();
Lesson3.add("Numbers 11-20");
Lesson3.add("Common Expressions");
Lesson3.add("Vocabulary: School");
Lesson3.add("Grammar: Possessives");
Lesson3.add("Questions and Answers");
List<String> Lesson4 = new ArrayList<String>();
Lesson4.add("The Tens (20-100)");
Lesson4.add("Common Expressions");
Lesson4.add("Vocabulary: Food");
Lesson4.add("Grammar: Subject Pronouns, Using 'Want'");
Lesson4.add("Questions and Answers");
List<String> Lesson5 = new ArrayList<String>();
Lesson5.add("Numbers 21-30");
Lesson5.add("Common Expressions");
Lesson5.add("Vocabulary: Common Verbs I");
Lesson5.add("Grammar: Using 'Want' + Infinitive");
Lesson5.add("Questions and Answers");
listDataChild.put(listDataHeader.get(0), Lesson1); // Header, Child data
listDataChild.put(listDataHeader.get(1), Lesson2);
listDataChild.put(listDataHeader.get(2), Lesson3);
listDataChild.put(listDataHeader.get(3), Lesson4);
listDataChild.put(listDataHeader.get(4), Lesson5);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
我已经创建了一个适配器类,如下所示:
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}