如何动态地将内容添加到菜单项?

时间:2017-03-19 14:50:38

标签: android oncreateoptionsmenu

我正在创建应用程序,在顶部菜单栏中我创建了一个带有余额的钱包图标!我从我的prefrence取回平衡!!但每当我从服务器获得平衡的新更新时,我需要更新菜单图标中的余额!我怎么能实现它?

我正在使用firebase,我可以从服务器获取数据到我的活动,并可以优先保存!但是,除非我点击3个点,否则无法更新菜单图标!或改变活动!如何在没有任何点击或任何内容的情况下更新菜单

我的菜单代码是:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        MenuItem item = menu.findItem(R.id.badge);
        MenuItemCompat.setActionView(item, R.layout.feed_update_count);
        RelativeLayout layout = (RelativeLayout) MenuItemCompat.getActionView(item);
        mytext = (TextView) layout.findViewById(R.id.hotlist_hot);
        mytext.setText(String.valueOf(prefs.getString("wallet_amount","0")));
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Topup.this, "Clicked",Toast.LENGTH_LONG).show();
            }
        });
        return super.onCreateOptionsMenu(menu);
    }

@Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem item = menu.findItem(R.id.badge);
        MenuItemCompat.setActionView(item, R.layout.feed_update_count);
        RelativeLayout layout = (RelativeLayout) MenuItemCompat.getActionView(item);
        mytext = (TextView) layout.findViewById(R.id.hotlist_hot);
        mytext.setText(String.valueOf(prefs.getString("wallet_amount","0")));
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Topup.this,BankActivity.class);
                startActivity(intent);
            }
        });
        return super.onPrepareOptionsMenu(menu);

    }

我还需要在onResume函数上更新菜单

更新 Android - How to dynamically change menu item text outside of onOptionsItemsSelected or onCreateOptionsMenu

更新

我根据链接的答案使用了另一个功能:

private void updateMenuTitles() {
        MenuItem item = menu.findItem(R.id.badge);
        MenuItemCompat.setActionView(item, R.layout.feed_update_count);
        RelativeLayout layout = (RelativeLayout) MenuItemCompat.getActionView(item);
        mytext = (TextView) layout.findViewById(R.id.hotlist_hot);
        mytext.setText(String.valueOf(prefs.getString("wallet_amount","0")));
    }

OnResume功能我更新服务器的金额值,并将我的金额更新为参考,并将数据添加到首选项

Onresume功能

@Override


       public void onResume(){
            super.onResume();

            getAmount(); //Updates amount to preference variable wallet_amount here
            updateMenuTitles(); //Calling this function to update the textview in menu
            invalidateOptionsMenu();

        }

应用程序部队在updateMenuTitles

处关闭

菜单XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_gravity="center"
    android:clickable="true"
    style="@android:style/Widget.ActionButton">

    <ImageView
        android:id="@+id/hotlist_bell"
        android:src="@drawable/wallet"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:gravity="center"
        android:layout_margin="0dp"
        android:contentDescription="bell"
        />

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/hotlist_hot"
        android:layout_width="wrap_content"
        android:minWidth="20sp"
        android:textSize="16sp"
        android:textColor="#ffffffff"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="1221"
        android:layout_alignTop="@id/hotlist_bell"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="-3dp"
        android:paddingBottom="4dp"
        android:paddingRight="6dp"
        android:paddingLeft="6dp"
        android:background="@drawable/rounded_square"
        android:paddingTop="4dp"
        android:textStyle="normal|bold" />
</RelativeLayout>

菜单:

<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/badge"
        android:actionLayout="@layout/feed_update_count"
        android:icon="@drawable/shape_notification"
        android:title=""
        app:showAsAction="always" />
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_transaction"
        android:orderInCategory="100"
        android:title="@string/action_transactions"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_pref"
        android:orderInCategory="100"
        android:title="Log out"
        app:showAsAction="never" />
</menu>

0 个答案:

没有答案