【问题标题】:Update ActionBar from Adapter从适配器更新 ActionBar
【发布时间】:2016-09-05 07:42:47
【问题描述】:

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

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都是正确的,它也设置了文本,但没有任何变化或出现错误!!请帮我改变当自定义视图中的按钮单击时,文本视图作为来自基本适配器的菜单栏中的篮子通知。谢谢 我的自定义视图列表有这个基本适配器:

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>

【问题讨论】:

    标签: android notifications android-actionbar baseadapter


    【解决方案1】:

    您可以使用interface 实现此目的。

    在Adapter类中添加接口

    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中实现这个接口。

    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
        }
    }
    

    【讨论】:

    • 非常感谢,在测试这个答案之前还有一个问题:我应该如何在主要活动中将 AdapterCallBack adapterCallback 传递给 Goods_ImageAdapter?
    • 我正在设置适配器 = new Goods_ImageAdapter(this, Goods /* Json data */, this);在 AsyncTask 的 onPostExecute 中,它让我出错:
    • 适配器应该在onCreate()中初始化。创建 JSON 数据的模型,而不是传递 JSON 数据,而是传递 JSON 模型的 ArrayList。而在onPostExecute()中,解析JSON并将数据添加到ArrayList并调用adapter.notifyDatasetChanged()
    • 很高兴为您提供帮助:)
    • 工作正常。非常感谢
    【解决方案2】:

    我正在从 Recycler 适配器的 ActionBar 中打印我的数组长度大小

    ((Show_request) mContext).getSupportActionBar().setTitle("Total Stone:- " + array.length());
    

    使用这个你可以在 RecyclerView 适配器中获取 ActionBar

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多