Updating textview on activity once data in adapter class is changed
Asked Answered
O

4

10

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.

Osteotome answered 7/8, 2014 at 16:15 Comment(1)
keep quantity as part of data in arraylist(of listview u updating)Darby
A
34

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

Abaxial answered 7/8, 2014 at 16:26 Comment(2)
It seems very correct technically, but the variable mOnDataChangeListener becomes very often null. Why could this happen?Intelligent
@fivos Perhaps you are looking for the error at the wrong place? The code above never sets the variable to null by itself. If you can't find the error yourself create a new question at SO. Perhaps sombody or I can help you.Abaxial
C
10

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

}
});
Cleanup answered 7/8, 2014 at 16:26 Comment(0)
S
1

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

@Override
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
   // Your code to nofify
}
Simmer answered 7/8, 2014 at 16:26 Comment(0)
A
0

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

Araucaria answered 7/8, 2014 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.