Java ExpandableListView子数组元素到新页面
这是我目前正在处理的项目的Java ExpandableListView子数组元素到新页面,java,android,expandablelistview,Java,Android,Expandablelistview,这是我目前正在处理的项目的ExpandableListView布局。我的孩子ArrayList有问题。我想能够点击每一个菜,并导致我一个新的页面。我试着使用intent,但它不起作用 这是我的Java类代码 public class Menupage extends ExpandableListActivity { private static final String arrGroupElements[] = { "Main","Pasta","Pizza", "Sal
ExpandableListView
布局。我的孩子ArrayList
有问题。我想能够点击每一个菜,并导致我一个新的页面。我试着使用intent,但它不起作用
这是我的Java类代码
public class Menupage extends ExpandableListActivity {
private static final String arrGroupElements[] = {
"Main","Pasta","Pizza", "Salads", "Sides",
"Desserts", "Soups", "Beverages" };
/**
014
* strings for child elements
015
*/
private static final String arrChildElements[][] = {
{ "Fish & Chips","Lamb Chop", "Cajun Fish", "Chicken Chop"},
{ "Spaghetti Bolognese ","Spaghetti Carbonara", "Spaghetti Aglio Olio" },
{ "Hawaiian Pizza","Simply Cheese Pizza", "Classic Pepperoni" },
{ "Chicken Salad","Potatoes Salad", "Greek Salad" },
{ "Garlic Bread" ,"Criss Cross Fries", "Hot & Sweet Wings" },
{ "Banana Boat Ice Cream ","Strawberry Shortcake", "Chocolate Lava" },
{ "Cream of Mushroom Soup","Chicken & Veg Soup", "Tomato Soup" },
{ "Coffee","Tea", "Juices" },
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.article_screen);
setListAdapter(new ExpandableListAdapter(this));
}
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context myContext;
public ExpandableListAdapter(Context context) {
myContext = context;
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.child_item_layout, null);
}
TextView yourSelection = (TextView) convertView
.findViewById(R.id.articleContentTextView);
yourSelection
.setText(arrChildElements[groupPosition][childPosition]);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return arrChildElements[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public int getGroupCount() {
return arrGroupElements.length;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate( R.layout.article_list_item_layout, null);
}
TextView groupName = (TextView) convertView.findViewById(R.id.articleHeaderTextView);
groupName.setText(arrGroupElements[groupPosition]);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
@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);
}
}
在menupage活动中,为可展开listview创建id,然后调用setOnChildClickListener 示例代码和答案:
为什么不将
onClick
listner设置为子视图?您好,我尝试在我的上使用您的代码,但似乎不起作用。我是一名学生,我不太喜欢这样做。你能说得更具体些吗。非常感谢!好啊在布局编辑器文本、图像视图中使用以下任何一个小部件。。。。。如果是,请删除对这些小部件的关注。
ExpandableListView expListView = (ExpandableListView) findViewById(R.id.lvExp);
expListView.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Intent nextActivity=new Intent(Menupage.this,NextScreen.class);
nextActivity.putExtra("SELECTED_CHILD_VALUE", arrChildElements[groupPosition][childPosition]);
startActivity(nextActivity);
return false;
}
});