从适配器更新ActionBar [英] Update ActionBar from Adapter

查看:87
本文介绍了从适配器更新ActionBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a basket icon with count notification on it in actionbar for showing number of goods in shopping basket. also I have a custom view containing a button for add goods to shopping basket. I want that when I click on button (in custom view) notification count on basket icon increases. I use this function in main activity to update notification count but notification does not update.

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作为基本适配器的菜单栏中篮子的通知. 我的自定义视图列表具有以下基本适配器:

but when I use non static of that function in main activity with simple button on click, it works fine. but I can't call non static function in base adapter too :( . I checked line by line of the function in static mode all ids are right and it set text too but nothing change or get error!!. please help me to change textview as notification of basket in menubar from base adapter when button in custom view clicks. thanks I have this base adapter for my custom view list:

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:

this is (basket with notification on action bar) 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>

推荐答案

您可以使用interface来实现.

在Adapter类中添加接口

Add an interface in Adapter class

public interface AdapterCallBack
{
    public void onChangeBadgeCount();
}

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

And in constructor, pass a reference to this interface.

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

每当要在ActionBar中设置徽章计数时,只需调用adapterCallback.onChangeBadgeCount().

And whenever you want to set badge count in ActionBar, just call adapterCallback.onChangeBadgeCount().

并在MainActivity中实现此接口.

public class MainActivity implements Goods_ImageAdapter.AdapterCallBack
{
    ... some code

    When your are initializing adapter, just pass this as a last parameter.
    Example:
    adapter = new Goods_ImageAdapter(this, Goods /* Json data */, this);
    ... 


    @override
    public void onChangeBadgeCount()
    {
        // Change your badge count here
    }
}

这篇关于从适配器更新ActionBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆