从Adapter更新ActionBar

时间:2016-09-05 07:42:48

标签: android notifications android-actionbar baseadapter

我在操作栏上有一个带有计数通知的篮子图标,用于显示购物篮中的商品数量。我也有一个自定义视图,其中包含一个用于向购物篮添加商品的按钮。我希望当我点击按钮(在自定义视图中)时,篮子图标上的通知计数会增加。我在主活动中使用此功能来更新通知计数但通知不会更新。

public static void updateCountBasket(final int number , int id , View view ,Context context) {

        if (view.findViewById(id) == null) 
            return;
        TextView t = (TextView)view.findViewById(id);
        String dd = t.getText().toString();
        ((Activity) context).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (number == 0)
                    t.setVisibility(View.INVISIBLE);
                else {
                    t.setVisibility(View.VISIBLE);
                    t.setText(Integer.toString(number));
                }
            }
        });
}

但是当我在主要活动中使用非静态功能时,点击简单按钮,它可以正常工作。但是我也不能在基础适配器中调用非静态函数:(。我在静态模式下逐行检查所有ID是正确的,它也设置了文本,但没有任何改变或得到错误!!。请帮我改变当自定义视图中的按钮发出咔嗒声时,textview作为来自基本适配器的菜单栏中的篮子通知。谢谢 我有自定义视图列表的基础适配器:

public class Goods_ImageAdapter extends BaseAdapter {
private Context context;
private final JSONArray Goods;
private Bagde bagde;

public Goods_ImageAdapter(Context context ,  JSONArray Goods) {
    this.context = context;
    this.Goods = Goods;
    bagde = Bagde.getInstance();
}

@Override
public int getCount() {
    return Goods.length();
}

@Override
public Object getItem(int arg0) {
    return null;
}

@Override
public long getItemId(int arg0) {
    return 0;
}

public static String doubleToStringNoDecimal(double d) {
            DecimalFormat formatter = (DecimalFormat)                 NumberFormat.getInstance(Locale.US);;
    formatter .applyPattern("#,###");
    return formatter.format(d);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;
        final View bagde_c;
        if (convertView == null) {
            gridView = new View(context);
            gridView = inflater.inflate(R.layout.goodsitem, null);
            bagde_c = inflater.inflate(R.layout.badge_counter, null);

            try {
                TextView goods_textview_Name = (TextView) gridView.findViewById(R.id.goods_textview_Name);
                goods_textview_Name.setText(Goods.getJSONObject(position).getString("Name"));

                TextView goods_textview_Price = (TextView) gridView.findViewById(R.id.goods_textview_Price);
                     goods_textview_Price.setText(Goods.getJSONObject(position).getString("Price"));
            } catch (JSONException e) {
                e.printStackTrace();
            }

            try {
                ImageView goods_imageview = (ImageView) gridView.findViewById(R.id.goods_imageview);
                new ImageDownloader(goods_imageview).execute("http://IP/" + Goods.getJSONObject(position).getString("ImageFileName"));
            } catch (JSONException e) {
                e.printStackTrace();
            }

            final TextView goods_textview_bagdecounter = (TextView)      gridView.findViewById(R.id.goods_textview_bagdecounter);
            ImageView goods_buy_plus = (ImageView) gridView.findViewById(R.id.goods_buy_button_plus);
            try {
                goods_buy_plus.setTag(Goods.getJSONObject(position));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            goods_buy_plus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((Activity) context).runOnUiThread(new Runnable() {
                           @Override
                           public void run() {
                               String bagde_counter_text = goods_textview_bagdecounter.getText().toString();
                                goods_textview_bagdecounter.setText(String.valueOf(Integer.parseInt(bagde_counter_text) + 1));
                           }
                        });
            com.???.????.Goods_Activity.updateCountBasket(a number for     example,R.id.notif,inflater2.inflate(R.layout.badge_counter, null),context);
                }
            });

            ImageView goods_buy_minus = (ImageView)     gridView.findViewById(R.id.goods_buy_button_minus);
            try {
                goods_buy_minus.setTag(Goods.getJSONObject(position));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            goods_buy_minus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((Activity) context).runOnUiThread(new Runnable() {
                           @Override
                               public void run() {
                                     String bagde_counter_text =               goods_textview_bagdecounter.getText().toString();
                                if(Integer.parseInt(bagde_counter_text)> 0)
                                        goods_textview_bagdecounter.setText(String.valueOf(Integer.parseInt(bagde_counter_text) - 1));
                           }
                    });
            }
        });
        } else {
            gridView = (View) convertView;
        }

        return gridView;
}

}

这是(带有动作栏通知的篮子)badge_counter.xml:

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

<RelativeLayout
    android:id="@+id/badge_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/relative_layout_item_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/button"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:background="@drawable/download" />

        <TextView
            android:id="@+id/notif"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@android:drawable/ic_notification_overlay"
            android:text="33"
            android:textColor="#FFA"
            android:textSize="12sp"
            android:textStyle="bold" />

    </RelativeLayout>
</RelativeLayout>
</RelativeLayout>

2 个答案:

答案 0 :(得分:4)

您可以使用= form.input :type, :as => :radio_buttons, :hidden => true, :checked => ... = form.input :type, :as => [:radio_buttons, :hidden] :checked => ... = form.input :type, :as => :radio_buttons, :as => :hidden, :checked => ... 来实现此目的。

在Adapter类

中添加一个接口
interface

在构造函数中,传递对此接口的引用。

public interface AdapterCallBack
{
    public void onChangeBadgeCount();
}

每当您想在public Goods_ImageAdapter(Context context , JSONArray Goods, AdapterCallBack adapterCallback) { this.context = context; this.Goods = Goods; this.adapterCallback = adapterCallback; bagde = Bagde.getInstance(); } 设置徽章计数时,只需致电ActionBar

adapterCallback.onChangeBadgeCount()

中实现此界面
MainActivity

答案 1 :(得分:1)

我正在使用Recycler adapter

在ActionBar中打印我的数组长度大小
((Show_request) mContext).getSupportActionBar().setTitle("Total Stone:- " + array.length());

使用此功能,您可以在RecyclerView适配器中获取ActionBar

((your activity name) context object).getSupportActionBar().setTitle("message"/or pass variable);