Guest User

Untitled

a guest
Jan 23rd, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.94 KB | None | 0 0
  1. public class TransactionAdapter extends BaseAdapter implements OnCheckedChangeListener {
  2.  
  3.     private LayoutInflater inflater;
  4.     private Account account;
  5.     private List<Transaction> transactions = new LinkedList<Transaction>();
  6.     private Context context;
  7.  
  8.     private Set<Integer> selection = new HashSet<Integer>();
  9.    
  10.     /* Some basic adapter logic */
  11.  
  12.     public int countSelected() {
  13.         return selection.size();
  14.     }
  15.  
  16.     public void clearSelection() {
  17.         selection.clear();
  18.         notifyDataSetChanged();
  19.     }
  20.  
  21.     public void checkedStateChanged(int position, boolean checked) {
  22.         if (checked && !selection.contains(position)) {
  23.             selection.add(position);
  24.         } else if (!checked && selection.contains(position)) {
  25.             selection.remove(position);
  26.         }
  27.         notifyDataSetChanged();
  28.     }
  29.  
  30.     @Override
  31.     public View getView(int location, View view, ViewGroup parent) {
  32.  
  33.         if (view == null) {
  34.             view = this.inflater.inflate(R.layout.row_transaction, null);
  35.             CheckBox checkbox = (CheckBox) view.findViewById(R.id.row_transaction_checkbox);
  36.             checkbox.setOnCheckedChangeListener(this);
  37.         }
  38.  
  39.         TextView date = (TextView) view.findViewById(R.id.row_transaction_date);
  40.         TextView description = (TextView) view.findViewById(R.id.row_transaction_description);
  41.         TextView amount = (TextView) view.findViewById(R.id.row_transaction_amount);
  42.         CheckBox checkbox = (CheckBox) view.findViewById(R.id.row_transaction_checkbox);
  43.  
  44.         NumberFormat amountFormat = NumberFormat.getCurrencyInstance();
  45.  
  46.         if (location == 0) { // First is the balance
  47.             /* Not usefull code */
  48.         } else {
  49.             Transaction transaction = (Transaction) getItem(location);
  50.  
  51.             if (transaction != null) {
  52.                 DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context);
  53.  
  54.                 date.setText(dateFormat.format(transaction.getDate()));
  55.  
  56.                     description.setText(transaction.getShownDescription(context));
  57.  
  58.                 amount.setText(amountFormat.format(transaction.getAmount()));
  59.                
  60.  
  61.                 checkbox.setVisibility(View.VISIBLE);
  62.                 checkbox.setTag(location);
  63.  
  64.                 if (selection.contains((Integer) location)) {
  65.                     checkbox.setChecked(true);
  66.                     view.setBackgroundColor(context.getResources().getColor(android.R.color.holo_blue_light));
  67.                 } else {
  68.                     checkbox.setChecked(false);
  69.                     view.setBackgroundColor(context.getResources().getColor(android.R.color.background_light));
  70.                 }
  71.  
  72.             }
  73.         }
  74.  
  75.         return view;
  76.     }
  77.    
  78.     // Not used at the moment but i want to have a callback to my activity to manually start the action bar
  79.     public void setOnCheckedChangeListener(OnCheckedChangeListener callback) {
  80.         this.callback = callback;
  81.     }
  82.  
  83.     @Override
  84.     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  85.         if (isChecked) {
  86.             selection.add((Integer) buttonView.getTag());
  87.         } else {
  88.             selection.remove((Integer) buttonView.getTag());
  89.         }
  90.  
  91.         ListView list = (ListView) buttonView.getParent().getParent();
  92.         if (list != null) {
  93.             list.setItemChecked((Integer) buttonView.getTag(), isChecked);
  94.         }
  95.        
  96.         if (callback != null) { //Call the activity to start the action bar
  97.             callback.onCheckedChanged(buttonView, isChecked);
  98.         }
  99.     }
  100.  
  101. }
  102.  
  103.  
  104. // In my activity
  105.     private boolean contextModeActivated = false;
  106.     public MultiChoiceModeListener multiChoiceMode = new MultiChoiceModeListener() {
  107.         @Override
  108.         public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
  109.             transactionAdapter.checkedStateChanged(position, checked);
  110.            
  111.             NumberFormat amountFormat = NumberFormat.getCurrencyInstance();
  112.             mode.setTitle(amountFormat.format(transactionAdapter.getTotalAmountSelected()));
  113.         }
  114.  
  115.         @Override
  116.         public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
  117.  
  118.             Intent intent;
  119.  
  120.             switch (item.getItemId()) {
  121.             case R.id.menu_conciliate:
  122.                 account.addToBalance(transactionAdapter.getTotalAmountSelected());
  123.                 transactionAdapter.removeConciliateTransaction();
  124.                 account.save();
  125.                 mode.finish();
  126.                 return true;
  127.             case R.id.menu_add_transaction:
  128.                 intent = new Intent(getActivity(), AddTransaction.class);
  129.                 intent.putExtra(AddTransaction.ACCOUNT_ID_TAG, getActivity().getActionBar()
  130.                         .getSelectedNavigationIndex());
  131.  
  132.                 startActivityForResult(intent, MainActivity.ADD_TRANSACTION_REQUEST);
  133.                 return true;
  134.  
  135.             case R.id.menu_settings:
  136.                 intent = new Intent(getActivity(), Preference.class);
  137.                 startActivityForResult(intent, MainActivity.PREFERENCE_REQUEST);
  138.                 return true;
  139.  
  140.             default:
  141.                 return false;
  142.             }
  143.         }
  144.  
  145.         @Override
  146.         public boolean onCreateActionMode(ActionMode mode, Menu menu) {
  147.             MenuInflater inflater = mode.getMenuInflater();
  148.             inflater.inflate(R.menu.transaction_context_menu, menu);
  149.             return true;
  150.         }
  151.  
  152.         @Override
  153.         public void onDestroyActionMode(ActionMode mode) {
  154.             transactionAdapter.clearSelection();
  155.         }
  156.  
  157.         @Override
  158.         public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
  159.             return false;
  160.         }
  161.     };
  162.  
  163.     @Override
  164.     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  165.         Intent intent = new Intent(getActivity(), AddTransaction.class);
  166.         intent.putExtra(AddTransaction.ACCOUNT_ID_TAG, getActivity().getActionBar().getSelectedNavigationIndex());
  167.         intent.putExtra(AddTransaction.TRANSACTION_ID_TAG, transactionAdapter.getTransaction(position).getId());
  168.  
  169.         startActivityForResult(intent, MainActivity.ADD_TRANSACTION_REQUEST);
  170.     }
  171.  
  172.     @Override
  173.     public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
  174.         ((CheckBox) view.findViewById(R.id.row_transaction_checkbox)).setChecked(true);
  175.         return true;
  176.     }
  177.  
  178.     @Override
  179.     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  180.         if (transactionAdapter.countSelected() > 0 && !contextModeActivated) {
  181.             getActivity().startActionMode(multiChoiceMode);
  182.             contextModeActivated = true;
  183.         } else if (transactionAdapter.countSelected() == 0 && contextModeActivated){
  184.             // Close the context menu ??
  185.             contextModeActivated = false;
  186.         }
  187.     }
Advertisement
Add Comment
Please, Sign In to add comment