一旦适配器类中的数据发生更改,就更新活动的文本视图 [英] Updating textview on activity once data in adapter class is changed

查看:21
本文介绍了一旦适配器类中的数据发生更改,就更新活动的文本视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的仪表板活动中有 textview txtQuantity.我为包含已售产品的自定义适配器编写了单独的类.

I am having textview txtQuantity in my dashboard activity. I wrote separate class for custom adapter which will contain sold products.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dashboard);

    list = (ListView) findViewById(R.id.listSoldItems);

    txtAmount = (TextView) findViewById(R.id.txtAmount);
    txtItems = (TextView) findViewById(R.id.txtItems);

    // init listview
    adapter = new Sold_item_adaptor(Dashboard.this, soldItemsList);
    list.setAdapter(adapter);

我可以使用适配器从列表中删除项目.删除项目的代码写在适配器类中.

I can remove items from list using adapter. Code for removing items is written in adapter class.

public View getView(final int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_row_sold_item, null);

    TextView txtListItem = (TextView) vi.findViewById(R.id.txtListItem);
    final TextView txtQuantity = (TextView) vi.findViewById(R.id.txtQuantity);
    ImageView imgCancel = (ImageView) vi.findViewById(R.id.imgCancel);

    HashMap<String, String> mapData = new HashMap<String, String>();
    mapData = data.get(position);

    txtListItem.setText(mapData.get("product"));
    txtQuantity.setText(mapData.get("qty"));

    imgCancel.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            doButtonOneClickActions(txtQuantity, position);
        }
    });

    return vi;
}

private void doButtonOneClickActions(TextView txtQuantity, int rowNumber) {
    // Do the actions for Button one in row rowNumber (starts at zero)
    data.remove(rowNumber);
    notifyDataSetChanged();

}

在我的仪表板活动中,我维护所选项目的数量和总量.现在,如果我从列表视图中删除项目,来自自定义适配器的代码会删除该项目,但如何在仪表板活动中获得通知/信号以更新数量.

On my dashboard activity I am maintaining number of items selected, total amount. Now if I remove item from listview, code from custom adapter removes the item but how can I get notification / signal on dashboard activity to update quantity.

推荐答案

通过提供简单的回调.

为此在您的适配器中编写一个简单的接口

For this to work write a simple interface in your adapter

public interface OnDataChangeListener{
    public void onDataChanged(int size);
}

并为侦听器添加一个 setter(也在适配器中)

and add a setter for the listener (also in the adapter)

OnDataChangeListener mOnDataChangeListener;
public void setOnDataChangeListener(OnDataChangeListener onDataChangeListener){
    mOnDataChangeListener = onDataChangeListener;
}

现在向适配器中的以下块添加附加代码

now add additional code to the following block in the adapter

private void doButtonOneClickActions(TextView txtQuantity, int rowNumber) {
    ...
    if(mOnDataChangeListener != null){
        mOnDataChangeListener.onDataChanged(data.size());
    }
}

在您的仪表板活动中,您需要注册监听器

in your dashboard activity you then need to register the listener

protected void onCreate(Bundle savedInstanceState) {
    ...
    adapter.setOnDataChangeListener(new Sold_item_adaptor.OnDataChangeListener(){
        public void onDataChanged(int size){
            //do whatever here
        }
    });
}

就是这样;)

这篇关于一旦适配器类中的数据发生更改,就更新活动的文本视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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