Updating textview on activity once data in adapter class is changed

后端 未结 4 1728
南笙
南笙 2020-12-05 09:07

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

 protected void onCrea         


        
相关标签:
4条回答
  • 2020-12-05 09:08

    As I understand the question, you are interested in a way to update UI outside the adapter after doButtonOneClickActions();

    The simplest way would be to use https://github.com/greenrobot/EventBus

    Examples can be found here http://awalkingcity.com/blog/2013/02/26/productive-android-eventbus/

    If you do not want to do this, you can create a callback http://android-er.blogspot.dk/2013/09/implement-callback-function-with.html

    0 讨论(0)
  • 2020-12-05 09:14

    Override notifyDataSetChanged() in your adapter class ... and do what ever you want ...

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
       // Your code to nofify
    }
    
    0 讨论(0)
  • 2020-12-05 09:23

    the main idea is:

    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,txtAmount);
    list.setAdapter(adapter);
    

    in your adapter:

    public class MyAdapter extends ArrayAdapter<SoldItemsList > {
    
    private Context context;
    private mTotalQty;
    private TextView mTxtAmountAdapter;
    
    
     public OfferAdapter(Context context, int resource,SoldItemsList object,TextView txtAmount ) {
        super(context, resource, objects);
        this.context = context;
        this.mTxtAmountAdapter = txtAmount;
    
    
    }
    
    //...
    
    imgCancel.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        doButtonOneClickActions(position);
        // update totalAmount
         mTxtAmountAdapter.setText(Integer.valueOf(totalAmount).toString()));
    
    }
    });
    
    imgPlus.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        qtyClickAction(position);
         // update totalQty
        mTxtAmountAdapter.setText(Integer.valueOf(totalAmount).toString()));
    
    }
    });
    
    0 讨论(0)
  • 2020-12-05 09:26

    By providing a simple callback.

    For this to work write a simple interface in your adapter

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

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

    That's about it ;).

    0 讨论(0)
提交回复
热议问题