我无法从 Android Studio 中的内部 class onClick 方法启动 Intent
I can't start an Intent from inner class onClick method in Android Studio
我有一个 Adapter class 和一个 ViewHolde inner class。我想开始一个意图来传递 RecipeDisplay class 它应该显示哪个食谱。我的计划是获取列表的位置,将其传递给 RecipeDisplay 并从存储在 strings.xml.
中的 String[] 中读取相关元素
我想从内部 class onClick 方法传递意图,但我遇到了与连接相关的错误。你能帮我看看哪个是正确的contect吗?
我得到的错误:无法解析构造函数'Intent(com.viktorjava.myrecipes.ListAdapter.ItemViewHolder, java.lang.Class<com.viktorjava.myrecipes.RecipeDisplay>)'
我试过了
- Intent intent = new Intent(this, RecipeDisplay.class);
- Intent intent = new Intent(ListAdapter.this, RecipeDisplay.class);
- Intent intent = new Intent(ListAdapter.ItemViewHolder.this, RecipeDisplay.class);
但其中 none 个有效。
代码如下:
package com.viktorjava.myrecipes;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.LinkedList;
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ItemViewHolder> {
private final LinkedList<String> titleList;
private final LinkedList<String> shortList;
private LayoutInflater inflater;
public ListAdapter(Context context, LinkedList<String> titleList, LinkedList<String> shortList) {
inflater = LayoutInflater.from(context);
this.titleList = titleList;
this.shortList = shortList;
}
@NonNull
@Override
public ListAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = inflater.inflate(R.layout.list_item, parent, false);
return new ItemViewHolder(itemView, this);
}
@Override
public void onBindViewHolder(@NonNull ListAdapter.ItemViewHolder holder, int position) {
String currentTitle = titleList.get(position);
String currentShort = shortList.get(position);
holder.titleView.setText(currentTitle);
holder.shortView.setText(currentShort);
}
@Override
public int getItemCount() {
return titleList.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final TextView titleView;
public final TextView shortView;
final ListAdapter listAdapter;
ItemViewHolder(View itemView, ListAdapter listAdapter) {
super(itemView);
titleView = itemView.findViewById(R.id.recipetitle);
shortView = itemView.findViewById(R.id.recipeshort);
this.listAdapter = listAdapter;
}
@Override
public void onClick(View view) {
int position = getLayoutPosition();
Intent intent = new Intent(ListAdapter.ItemViewHolder.this, RecipeDisplay.class);
}
}
}
您应该使用 dependency injection
,只需使用方法参数将 context
传递给此 view holder
,然后使用注入的 context
而不是 ListAdapter.ItemViewHolder.this
像这样改变你的意图。
Intent intent = new Intent(context, RecipeDisplay.class);
context.startActivity(intent)
首先,将从构造函数获取的上下文对象保存到上下文变量中。
Context context;
public ListAdapter(Context context, LinkedList<String> titleList, LinkedList<String> shortList) {
this.context = context;
inflater = LayoutInflater.from(context);
this.titleList = titleList;
this.shortList = shortList;
}
然后使用该上下文启动 activity。
Intent intent = new Intent(context, RecipeDisplay.class);
context.startActivity(intent);
我有一个 Adapter class 和一个 ViewHolde inner class。我想开始一个意图来传递 RecipeDisplay class 它应该显示哪个食谱。我的计划是获取列表的位置,将其传递给 RecipeDisplay 并从存储在 strings.xml.
中的 String[] 中读取相关元素我想从内部 class onClick 方法传递意图,但我遇到了与连接相关的错误。你能帮我看看哪个是正确的contect吗?
我得到的错误:无法解析构造函数'Intent(com.viktorjava.myrecipes.ListAdapter.ItemViewHolder, java.lang.Class<com.viktorjava.myrecipes.RecipeDisplay>)'
我试过了
- Intent intent = new Intent(this, RecipeDisplay.class);
- Intent intent = new Intent(ListAdapter.this, RecipeDisplay.class);
- Intent intent = new Intent(ListAdapter.ItemViewHolder.this, RecipeDisplay.class);
但其中 none 个有效。
代码如下:
package com.viktorjava.myrecipes;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.LinkedList;
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ItemViewHolder> {
private final LinkedList<String> titleList;
private final LinkedList<String> shortList;
private LayoutInflater inflater;
public ListAdapter(Context context, LinkedList<String> titleList, LinkedList<String> shortList) {
inflater = LayoutInflater.from(context);
this.titleList = titleList;
this.shortList = shortList;
}
@NonNull
@Override
public ListAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = inflater.inflate(R.layout.list_item, parent, false);
return new ItemViewHolder(itemView, this);
}
@Override
public void onBindViewHolder(@NonNull ListAdapter.ItemViewHolder holder, int position) {
String currentTitle = titleList.get(position);
String currentShort = shortList.get(position);
holder.titleView.setText(currentTitle);
holder.shortView.setText(currentShort);
}
@Override
public int getItemCount() {
return titleList.size();
}
class ItemViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final TextView titleView;
public final TextView shortView;
final ListAdapter listAdapter;
ItemViewHolder(View itemView, ListAdapter listAdapter) {
super(itemView);
titleView = itemView.findViewById(R.id.recipetitle);
shortView = itemView.findViewById(R.id.recipeshort);
this.listAdapter = listAdapter;
}
@Override
public void onClick(View view) {
int position = getLayoutPosition();
Intent intent = new Intent(ListAdapter.ItemViewHolder.this, RecipeDisplay.class);
}
}
}
您应该使用 dependency injection
,只需使用方法参数将 context
传递给此 view holder
,然后使用注入的 context
而不是 ListAdapter.ItemViewHolder.this
像这样改变你的意图。
Intent intent = new Intent(context, RecipeDisplay.class);
context.startActivity(intent)
首先,将从构造函数获取的上下文对象保存到上下文变量中。
Context context;
public ListAdapter(Context context, LinkedList<String> titleList, LinkedList<String> shortList) {
this.context = context;
inflater = LayoutInflater.from(context);
this.titleList = titleList;
this.shortList = shortList;
}
然后使用该上下文启动 activity。
Intent intent = new Intent(context, RecipeDisplay.class);
context.startActivity(intent);