Android开发人员:通过Onclick方法实现ExpandableList子级的意图
我在为Android开发人员:通过Onclick方法实现ExpandableList子级的意图,android,android-intent,expandablelistview,Android,Android Intent,Expandablelistview,我在为ExpandableListView的子位置执行intentonclick时遇到问题。基本上,我正在创建一个简单的配方应用程序。该应用程序有4个家长位置(早餐、沙拉、主菜和零食)。每个父位置下面都有2个项目。我希望能够点击子项的一个,并加载一个新的意图(或页面) 例如,单击子香蕉煎饼时,我希望它加载早餐香蕉煎饼.xml文件。此文件将包含图片和配方说明 我知道这可能不是最好的方法,但我是为coursera课程而做的,不允许使用任何数据库或应用程序使用任何权限 这是我的主屏幕xml文件中的代码
ExpandableListView
的子位置执行intentonclick
时遇到问题。基本上,我正在创建一个简单的配方应用程序。该应用程序有4个家长位置(早餐、沙拉、主菜和零食)。每个父位置下面都有2个项目。我希望能够点击子项的一个,并加载一个新的意图(或页面)
例如,单击子香蕉煎饼时,我希望它加载早餐香蕉煎饼.xml文件。此文件将包含图片和配方说明
我知道这可能不是最好的方法,但我是为coursera课程而做的,不允许使用任何数据库或应用程序使用任何权限
这是我的主屏幕xml文件中的代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/paleoportraitxhdpi"
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=".MainScreen" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textViewPaleoFact"
android:layout_alignParentLeft="true"
android:paddingRight="4dp"
android:text="@string/paleoFactText" />
<TextView
android:id="@+id/textViewPaleoFact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/textView1"
android:text="@string/paleoFactText"
android:textSize="12sp" />
<ExpandableListView
android1:id="@+id/expandableListView1"
android1:layout_width="match_parent"
android1:layout_height="250dp"
android1:layout_alignLeft="@+id/textView1"
android1:layout_alignParentTop="true"
android1:layout_marginTop="14dp"
android1:padding="10dp" >
</ExpandableListView>
这是我的myAdapter文件的代码,它控制我的可扩展列表
@SuppressWarnings("unused")
public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
String []parentList={"Breakfast", "Salads", "Main Dishes", "Snacks"};
String [][]childList={
{
"Banana Pancakes", "Breakfast Casserole"
},
{
"Bacon Chickn Avacado Salad", "Curried Salmon Salad"
},
{
"Watercress Bacon Soup", "Spicy Chicken and Bacon Poppers"
},
{
"Primal Trail Mix", "Fat Guacamole Devils"
},
};
public MyAdapter(Context context) {
// TODO Auto-generated constructor stub
this.context=context;
}
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return null;
}
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return 0;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv=new TextView(context);
tv.setText(childList[groupPosition][childPosition]);
tv.setPadding(100, 15, 0, 15);
return tv;
}
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return childList[groupPosition].length;
}
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
public int getGroupCount() {
// TODO Auto-generated method stub
return parentList.length;
}
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv=new TextView(context);
tv.setText(parentList[groupPosition]);
tv.setPadding(60, 15, 0, 15);
return tv;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
以下是我试图在MyAdapter.java文件上实现的代码
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
if( groupPosition == 0 && childPosition == 0){
Intent i = new Intent(v.getContext(), breakfast_banana_pancakes.class );
v.getContext().startActivity(i);
Log.e ("Banana", "Banana pancakes was clicked");
}
//Intent i = new Intent(mActivity, TargetActivity.class);
//i.putExtra(TargetActivity.EXTRA_WHATEVER, MyAdapter.getChild(groupPosition, childPosition));
//startActivity(i);
return true;
}
如果我没看错你的话,你会想听听孩子们点击的
onChildClick
:
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Intent i = new Intent(mActivity, TargetActivity.class);
i.putExtra(TargetActivity.EXTRA_WHATEVER, mListAdapter.getChild(groupPosition, childPosition));
startActivity(i);
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
exv=(ExpandableListView)findViewById(R.id.expandableListView1);
exv.setAdapter(new MyAdapter(this));
exv.setOnChildClickListener(this); // Add this--make sure the list knows to use this as a listener, since we implemented the interface.
这将使您进入下一个活动。您可以通过Intent
另外,请确保listview知道如何调用此方法:
getExpandableListview().setOnChildClickListener(此)代码>(如果活动在ChildClickListener上实现了OnChildClickListener
)
编辑格式设置
onChildClick应该存在于main屏幕中
你的定义应该是:
public class MainScreen extends Activity implements ExpandableListView.OnChildClickListener {...}
然后,您需要告诉ListView调用childclick上的onChildClick
:
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Intent i = new Intent(mActivity, TargetActivity.class);
i.putExtra(TargetActivity.EXTRA_WHATEVER, mListAdapter.getChild(groupPosition, childPosition));
startActivity(i);
return false;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
exv=(ExpandableListView)findViewById(R.id.expandableListView1);
exv.setAdapter(new MyAdapter(this));
exv.setOnChildClickListener(this); // Add this--make sure the list knows to use this as a listener, since we implemented the interface.
人力资源管理。这似乎不起作用。我试过几种方法修改你的代码。最后,我对嵌套代码进行了注释,并尝试运行一条log.e消息,但这仍然不起作用。我在上面添加了新代码,以说明我是如何在MyAdapter.java文件中实现它的。您不会将其添加到适配器(除非您将适配器用作侦听器,但通常情况并非如此)。您还需要确保它正在侦听(代码示例的最后一行)!你太棒了。真是太棒了!谢谢你帮助我!