英文:
Displaying toast from a non activity class
问题
以下是您要翻译的内容:
"I want to display toasts from a non-activity class which is my RecyclerView
Adapter.
What can I do to achieve this?
I want to set toasts in the onLoadingStateChanged()
switch statements.
I have tried some old codes but they don't seem to work.
I don't want RecylerView
Adapter to be in the MainActivity
"
"My Adapter Activity:
public class TalesAdapter extends FirestorePagingAdapter<TalesDetails, TalesAdapter.TalesViewHolder> {
public TalesAdapter(@NonNull FirestorePagingOptions<TalesDetails> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull TalesViewHolder holder, int position, @NonNull TalesDetails model) {
holder.bind(model);
}
@NonNull
@Override
public TalesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.talesrecyclerview, parent, false);
return new TalesViewHolder(view);
}
@Override
protected void onLoadingStateChanged(@NonNull LoadingState state) {
switch (state) {
case LOADING_INITIAL:
case LOADING_MORE:
//toast here
break;
case LOADED:
//toast here
case FINISHED:
//toast here
break;
case ERROR:
//toast here
break;
}
}
public class TalesViewHolder extends RecyclerView.ViewHolder {
private TextView Title;
private TextView Matter;
private TextView Name;
public TalesViewHolder(View itemView) {
super(itemView);
Name = itemView.findViewById(R.id.tvName);
Title = itemView.findViewById(R.id.tvTitle);
Matter = itemView.findViewById(R.id.tvMatter);
}
public void bind(TalesDetails tales) {
Name.setText(tales.name);
Title.setText(tales.title);
Matter.setText(tales.matter);
}
}
}
如果您有任何其他翻译需求,请告诉我。
英文:
I want to display toasts from a non-activity class which is my RecyclerView
Adapter.
What can I do to achieve this?
I want to set toasts in the onLoadingStateChanged()
switch statements.
I have tried some old codes but they don't seem to work.
I don't want RecylerView
Adapter to be in the MainActivity
My Adapter Activity:
public class TalesAdapter extends FirestorePagingAdapter<TalesDetails, TalesAdapter.TalesViewHolder> {
public TalesAdapter(@NonNull FirestorePagingOptions<TalesDetails> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull TalesViewHolder holder, int position, @NonNull TalesDetails model) {
holder.bind(model);
}
@NonNull
@Override
public TalesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.talesrecyclerview, parent, false);
return new TalesViewHolder(view);
}
@Override
protected void onLoadingStateChanged(@NonNull LoadingState state) {
switch (state) {
case LOADING_INITIAL:
case LOADING_MORE:
//toast here
break;
case LOADED:
//toast here
case FINISHED:
//toast here
break;
case ERROR:
//toast here
break;
}
}
public class TalesViewHolder extends RecyclerView.ViewHolder {
private TextView Title;
private TextView Matter;
private TextView Name;
public TalesViewHolder(View itemView ) {
super(itemView);
Name = itemView.findViewById(R.id.tvName);
Title = itemView.findViewById(R.id.tvTitle);
Matter = itemView.findViewById(R.id.tvMatter);
}
public void bind(TalesDetails tales){
Name.setText(tales.name);
Title.setText(tales.title);
Matter.setText(tales.matter);
}
}
}
答案1
得分: 1
你有两种可能性:
1 - 创建 Context
变量
private Context context;
public TalesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.talesrecyclerview, parent, false);
return new TalesViewHolder(view);
}
2 - 使用 implementation 'com.blankj:utilcodex:1.29.0'
ToastUtils.showShort("YOUR TEXT HERE");
英文:
You have two possibilities :
1 - Create Context
variable
private Context context;
public TalesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
context = parent.getContext();
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.talesrecyclerview, parent, false);
return new TalesViewHolder(view);
}
2 - Using implementation 'com.blankj:utilcodex:1.29.0'
ToastUtils.showShort("YOUR TEXT HERE");
答案2
得分: 0
声明一个名为"YourActivity"的类的变量类型,并在构造TalesAdapter
时传递该活动引用。
public class TalesAdapter extends FirestorePagingAdapter<TalesDetails, TalesAdapter.TalesViewHolder> {
Context mContext = null;
public TalesAdapter(@NonNull FirestorePagingOptions<TalesDetails> options, Context mContext) {
this.mContext = mContext;
super(options);
}
@Override
protected void onLoadingStateChanged(@NonNull LoadingState state) {
switch (state) {
case LOADING_INITIAL:
case LOADING_MORE:
Toast.makeText(mContext, "你的消息", Toast.LENGTH_SHORT).show();
break;
case LOADED:
// 在此处显示toast
case FINISHED:
// 在此处显示toast
break;
case ERROR:
// 在此处显示toast
break;
}
}
}
英文:
Declare a Variable type of YourActivity class and pass the activity reference by the time of constructing TalesAdapter
public class TalesAdapter extends FirestorePagingAdapter<TalesDetails, TalesAdapter.TalesViewHolder> {
Context mContext = null;
public TalesAdapter(@NonNull FirestorePagingOptions<TalesDetails> options, Context mContext) {
this.mContext = mContext;
super(options);
}
@Override
protected void onLoadingStateChanged(@NonNull LoadingState state) {
switch (state) {
case LOADING_INITIAL:
case LOADING_MORE:
Toast.makeText(mContext, "your message", Toast.LENGTH_SHORT).show()
break;
case LOADED:
//toast here
case FINISHED:
//toast here
break;
case ERROR:
//toast here
break;
}
}
}
答案3
得分: 0
你可以在适配器中创建一个构造函数,像这样:
Context mContext;
public TalesAdapter(@NonNull FirestorePagingOptions<TalesDetails> options, Context mContext) {
this.mContext = mContext;
super(options);
}
在你的活动中,无论是 MainActivity.java
还是其他任何地方,在使用这个适配器时,可以这样写:
Context mContext = this;
adapter = new TalesAdapter(mContext);
mRecycler.setAdapter(adapter);
英文:
You can Create a Constructor in the Adapter
Like:
Context mContext;
public TalesAdapter(@NonNull FirestorePagingOptions<TalesDetails> options, Context mContext) {
this.mContext = mContext;
super(options);
}
@Override
protected void onLoadingStateChanged(@NonNull LoadingState state) {
switch (state) {
case LOADING_INITIAL:
case LOADING_MORE:
Toast.makeText(mContext, "Taost", Toast.LENGTH_SHORT).show();
break;
case LOADED:
Toast.makeText(mContext, "Taost", Toast.LENGTH_SHORT).show();
break;
case FINISHED:
Toast.makeText(mContext, "Taost", Toast.LENGTH_SHORT).show();
break;
case ERROR:
Toast.makeText(mContext, "Taost", Toast.LENGTH_SHORT).show();
break;
}
}
And in your Activity whether it is MainActivity.java
or any while this Adapter in RecyclerView or ListView
Context mContext = this;
adapter = new TalesAdapter(mContext);
mRecycler.setAdapter(adapter)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论