从非Activity类显示toast。

huangapple go评论84阅读模式
英文:

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&lt;TalesDetails, TalesAdapter.TalesViewHolder&gt; {
public TalesAdapter(@NonNull FirestorePagingOptions&lt;TalesDetails&gt; 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 &#39;com.blankj:utilcodex:1.29.0&#39;

ToastUtils.showShort(&quot;YOUR TEXT HERE&quot;);

答案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&lt;TalesDetails, TalesAdapter.TalesViewHolder&gt; {
Context mContext = null;
public TalesAdapter(@NonNull FirestorePagingOptions&lt;TalesDetails&gt; 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, &quot;your message&quot;, 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&lt;TalesDetails&gt; 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, &quot;Taost&quot;, Toast.LENGTH_SHORT).show();
break;
case LOADED:
Toast.makeText(mContext, &quot;Taost&quot;, Toast.LENGTH_SHORT).show();
break;
case FINISHED:
Toast.makeText(mContext, &quot;Taost&quot;, Toast.LENGTH_SHORT).show();
break;
case ERROR:
Toast.makeText(mContext, &quot;Taost&quot;, 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)

huangapple
  • 本文由 发表于 2020年8月23日 22:03:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63547847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定