简体   繁体   English

如何从另一个类设置Android工具栏的标题?

[英]How to set the title of Android toolbar from another class?

I would like to do something like this: 我想做这样的事情:

if (total != 0) {
    percentage = String.valueOf((count * 100) / total);
    Log.e("Percentage", percentage);
    myActivity.getSupportActionBar().setTitle("Progress: " + percentage + "%.");
}

However, I can't call getSupportActionBar() because it says Non-static method 'getSupportActionBar()' cannot be referenced from a static context . 但是,我不能调用getSupportActionBar()因为它说Non-static method 'getSupportActionBar()' cannot be referenced from a static context How can I solve this? 我该如何解决?

Update 更新资料

This is the method inside my public class where I'd like to call the above code; 这是我的公共类中的方法,我想调用上面的代码;

     @Override
public void onBindViewHolder(final ViewHolder holder, int position) {
    holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
         @Override
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

         }
         });
         }

Casting your activity can resolve your problem. 投放活动可以解决您的问题。 Suppose your Activity Name is YourCustomActivity. 假设您的活动名称是YourCustomActivity。 Now cast the Activity like below. 现在,按如下所示投射活动。 Hope it will work. 希望它能工作。

    YourCustomActivity activity=(YourCustomActivity)myActivity;
    activity.getSupportActionBar().setTitle("");

Please Let me know if it works or not. 请让我知道它是否有效。

You can pass activity parameter to another class and use activities funcitons like below; 您可以将活动参数传递给另一个类,并使用如下所示的活动功能;

public class CityModel {

    Activity activity;

    public CityModel(Activity activity) {
        this.activity = activity;
    }

    private void doIt() {
        ((YourActivity) activity).changeTitle("New Title");
    }

}

And define this class instance like below; 并如下定义该类实例;

 public class YourActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CityModel model = new CityModel(this); // this-> Your activity.
     }

     public void changeTitle(String newTitle){
       getSupportActionBar().setTitle(newTitle);
     }
}

Hope it helps. 希望能帮助到你。

Thanks to other answers, but none of them fit what I wanted. 多亏了其他答案,但没有一个适合我的要求。

I solved it by moving the code to the activity in question and setting the title from there. 我通过将代码移至相关活动并从此处设置标题来解决了该问题。 However, the main change was to post the percentage value from a listener method, in my case: onItemCheckBoxChecked . 但是,主要更改是从侦听器方法发布百分比值,在我的示例中为: onItemCheckBoxChecked This meant that every time a user checked a box (could be other listeners in case of a textview or something else) the percentage was calculated in real time. 这意味着每次用户选中一个框时(在文本视图或其他情况下,可能是其他侦听器),该百分比都是实时计算的。 I also made the percentage variable public. 我还公开了百分比变量。

Because getSupportActionBar() is not static method. 因为getSupportActionBar()不是静态方法。 If you are using this method in static class or static method then you will get below error. 如果您在静态类或静态方法中使用此方法,则将得到以下错误。

Non-static method getSupportActionBar() cannot be referenced from a static context 不能从静态上下文引用非静态方法getSupportActionBar()

If you want to common method for it you can make one think create public method in your activity and call in your fragments . 如果您想要通用方法,可以考虑在活动中创建公共方法并调用fragments

public void setToolBar(String title) {
        getSupportActionBar().setTitle(title);
}

If you have Activity, simply you can call below code. 如果您有“活动”,则只需调用以下代码即可。

getSupportActionBar().setTitle(title);

Edit My Code : 编辑我的代码:

public static void setToolbar(final AppCompatActivity activity,String title){
    activity.getSupportActionBar().setTitle(title);
}

Edit Code : My RecyclerViewAdapter class : 编辑代码: 我的RecyclerViewAdapter类:

public class ExploreGalleryAdapter extends RecyclerView.Adapter<ExploreGalleryAdapter.ViewHolder> {

    private AppCompatActivity activity;
    private ArrayList<ExploreGalleryModel> exploreGalleryModelArrayList;
    //    private String status;


    public ExploreGalleryAdapter(AppCompatActivity activity, final ArrayList<ExploreGalleryModel> exploreGalleryModelArrayList, final String status) {
        this.activity = activity;
        this.exploreGalleryModelArrayList = exploreGalleryModelArrayList;
//        this.status = status;
    }

  public void onBindViewHolder(final ViewHolder holder, int position) {
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

activity.getSupportActionBar().setTitle(title);

     }
            });

}

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

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