简体   繁体   English

显示来自非活动类的吐司

[英]Displaying toast from a non activity class

I want to display toasts from a non-activity class which is my RecyclerView Adapter.我想显示来自我的RecyclerView适配器的非活动类的吐司。

What can I do to achieve this?我能做些什么来实现这一目标?

I want to set toasts in the onLoadingStateChanged() switch statements.我想在onLoadingStateChanged() switch 语句中设置onLoadingStateChanged()

I have tried some old codes but they don't seem to work.我尝试了一些旧代码,但它们似乎不起作用。

I don't want RecylerView Adapter to be in the MainActivity我不希望RecylerView Adapter 出现在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);

        }
    }
}

You have two possibilities :你有两种可能性:

1 - Create Context variable 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 - Using implementation 'com.blankj:utilcodex:1.29.0' 2 - 使用implementation 'com.blankj:utilcodex:1.29.0'

ToastUtils.showShort("YOUR TEXT HERE");

Declare a Variable type of YourActivity class and pass the activity reference by the time of constructing TalesAdapter声明一个 Variable 类型的 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, "your message", Toast.LENGTH_SHORT).show()
                break;

            case LOADED:
             //toast here
            case FINISHED:
             //toast here
                break;

            case ERROR:
             //toast here

                break;
        }

    }



}

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在您的活动中,无论是MainActivity.java还是 RecyclerView 或 ListView 中的此适配器

Context mContext = this;
adapter = new TalesAdapter(mContext);
mRecycler.setAdapter(adapter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM