对不起,这是我的第一个问题... 我有一个 ArrayAdapter 类,我想从内部匿名类中调用 startActivity ,但是我遇到了“无法解析方法startActivity”的错误
public View getView(final int position, @Nullable final View convertView, @NonNull ViewGroup parent) {
//inflating the layout
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.itemview_antliostasia, parent, false);
//get the reference to the view objects
final TextView myName = (TextView)row.findViewById(R.id.itemViewAntliostasiaName);
final TextView myID = (TextView)row.findViewById(R.id.itemViewAntliostasiaID);
final TextView myAntlies = (TextView)row.findViewById(R.id.itemViewAntliostasiaAntlies);
ImageView myEdit = (ImageView) row.findViewById(R.id.imageAntliostasiaEdit);
ImageView myDelete = (ImageView)row.findViewById(R.id.imageAntliostasiaDelete);
//providing the element of an ArrayList by specifying its position
myName.setText(dataList.get(position).getName());
myID.setText(dataList.get(position).getId());
myAntlies.setText(dataList.get(position).getAntlies());
myEdit.setImageResource(R.drawable.imgedit);
myDelete.setImageResource(R.drawable.imgdelete);
myEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), EditAntliostasioActivity.class);
String[] editValues = new String[2];
editValues[0] = myName.getText().toString();
editValues[1] = myAntlies.getText().toString();
intent.putExtra("edit values", editValues);
startActivity(intent);
}
});
我认为getContext()出了点问题...请帮助我
答案 0 :(得分:0)
尝试一下
Intent intent = new Intent(view.getContext(), EditAntliostasioActivity.class);
发生此错误的原因是因为getContext()
返回了活动的上下文,而不是匿名类。因此,您必须使用view.getContext()
。
答案 1 :(得分:0)
startActivity()
是Context
的一种方法,因此您需要在Context
对象上调用它:
getContext().startActivity(intent);
请注意,如果Context
不是Activity
,则还需要在FLAG_ACTIVITY_NEW_TASK
上设置Intent
。但是startActivity()
在Activity
中被覆盖为不需要它,并且您在此处具有活动上下文。
答案 2 :(得分:0)
创建Context的实例变量。
Class Vehicle
{
Context mContext;
初始化上下文...
public Vehicle(Context mContext)
{
this.mContext = mContext;
}
每当您调用startActivity时,就这样做。.
mContext.startActivity(intent);