更新的TextView上的活动曾经在适配器类的数据被修改适配器、数据、TextView

2023-09-12 04:24:37 作者:心凉不过憔悴。

我有我的仪表盘活动的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);

我可以从列表中使用的适配器删除项目。 $ C $下删除项目是写在适配器类。

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

}

在我的仪表盘的活动,我保持选定的项目,总金额数。现在,如果我从列表视图中删除的项目,从定制适配器code删除项目,但我怎样才能得到通知/信号仪表盘上的活动更新量。

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.

推荐答案

通过提供一个简单的回调。

By providing a simple callback.

有关这项工作的适配器编写一个简单的界面

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

现在的适配器添加额外的code以下块

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

这就是它。)

That's about it ;).