简体   繁体   中英

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);

        }
    }
}

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");

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;
        }

    }



}

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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