Updating the text activity type after changing data in the adapter class

I have a textview txtQuantity in my panel activity. I wrote a separate class for the user adapter that will contain the products sold.

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 the list using the adapter. Code for deleting items is written to the 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(); } 

In my activity on the dashboard, I support the number of selected items, the total amount. Now, if I remove an item from the list, the code from the user adapter removes the item, but how can I get a notification / signal about the activity of the toolbar to update the quantity.

+8
android android-activity android-listview listview
source share
4 answers

Providing a simple callback.

To do this, write a simple interface in your adapter

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

and add the installer for the listener (also in the adapter)

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

now add additional code to the next block in the adapter

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

in your activity on the dashboard you need to register a listener

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

What about it;).

+29
source share

Main idea:

 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())); } }); 
+9
source share

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

The easiest way is 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
source share

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

 @Override public void notifyDataSetChanged() { super.notifyDataSetChanged(); // Your code to nofify } 
0
source share

All Articles