我在为onclick
的childPosition实施意图ExpandableListView
时遇到问题。基本上,我正在创建一个简单的食谱应用程序。该应用程序有4个父母职位(早餐,沙拉,主菜和小吃)。每个父母职位下面都有2个项目。我希望能够点击子项并加载新的意图(或页面)。
例如,当点击子香蕉煎饼时,我希望它加载breakfast_banana_pancakes.xml文件。该文件将包含图片和配方说明。
我知道这可能不是最好的方法,但是我正在为一门课程做这件事,不允许使用任何数据库或让应用程序使用任何权限。
这是我的mainScreen 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>
这是我的mainScreenJava文件的代码。
public class MainScreen extends Activity {
ExpandableListView exv;
// Random Paleo Fact Via Strings.xml
String[] myString;
final Random rgenerator = new Random();
@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));
//Paleo Fact
Resources res = getResources();
myString = res.getStringArray(R.array.paleoFacts);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv2 = (TextView)findViewById(R.id.textViewPaleoFact);
tv2.setText(q);
}
@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_screen, menu);
return true;
}
}
以下是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;
}
答案 0 :(得分:1)
如果我正确地为你服务,你会想听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;
}
这会让您进入下一个活动。您可以通过extras
Intent
发送所需的任何内容来加载正确的布局
另外,请确保listview知道调用此方法:
getExpandableListview().setOnChildClickListener(this);
(如果活动实施OnChildClickListener
)
修改格式
onChildClick应存在于MainScreen
。
您的定义应该是:
public class MainScreen extends Activity implements ExpandableListView.OnChildClickListener {...}
然后,您需要告诉ListView调用onChildClick
:
@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.