我有一个带有子列表视图项的listView,其中我引用了Using lists in Android(15.2.ExpandableListView示例)。
我可以通过Toast点击每个列表项并显示。
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(activity, children,
Toast.LENGTH_SHORT).show();
}
但是当我想在childClick中使用Intent时,我收到一个错误:cannot find symbol method startActivity(Intent)
。
以下是我的课程:
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
//Some sort of class code in here...
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String children = (String) getChild(groupPosition, childPosition);
TextView text = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
text = (TextView) convertView.findViewById(R.id.textView1);
text.setText(children);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//11 group position exist, each has 5 child
switch ((groupPosition)){
case 0:
if (childPosition == 0) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=editors_choice"));
startActivity(intent); //cannot find symbol method startActivity(Intent)
} else if (childPosition == 1) {
Toast.makeText(activity, "2",
Toast.LENGTH_SHORT).show();
} else if (childPosition == 2) {
Toast.makeText(activity, "3",
Toast.LENGTH_SHORT).show();
} else if (childPosition == 3) {
Toast.makeText(activity, "4",
Toast.LENGTH_SHORT).show();
} else if (childPosition == 4) {
Toast.makeText(activity, "5",
Toast.LENGTH_SHORT).show();
}
break;
case 1:
...
}
});
return convertView;
}
}
感谢您的帮助。
答案 0 :(得分:3)
错误:找不到符号方法startActivity(Intent)。
由于startActivity
方法不是来自BaseExpandableListAdapter
,因此请使用上下文来访问startActivity
方法。为:
v.getContext().startActivity(intent);
答案 1 :(得分:1)
试试这个,
在MyExpandableListAdapter类中定义Context的引用,
private Context context;
为MyExpandableListAdapter类创建一个构造函数,并从实例化它的位置传递上下文,
public MyExpandableListAdapter(Context con) {
context = con;
}
使用
开始您的活动context.startActivity(intent)