gpt4 book ai didi

android - 从适配器更新 ActionBar

转载 作者:太空狗 更新时间:2023-10-29 16:30:50 27 4
gpt4 key购买 nike

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

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

但是当我在主要 Activity 中使用该功能的非静态并单击简单按钮时,它工作正常。但我也不能在基本适配器中调用非静态函数 :( 。我在静态模式下逐行检查函数,所有 id 都是正确的,它也设置了文本,但没有任何变化或出现错误!!请帮助我改变当自定义 View 中的按钮单击时, TextView 作为来自基本适配器的菜单栏中篮子的通知。谢谢我的自定义 View 列表有这个基本适配器:

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>

最佳答案

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

在Adapter类中添加一个接口(interface)

public interface AdapterCallBack
{
public void onChangeBadgeCount();
}

然后在构造函数中,传递一个对此接口(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()

并在MainActivity中实现这个接口(interface)。

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

关于android - 从适配器更新 ActionBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39325875/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com