Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TransactionAdapter extends BaseAdapter implements OnCheckedChangeListener {
- private LayoutInflater inflater;
- private Account account;
- private List<Transaction> transactions = new LinkedList<Transaction>();
- private Context context;
- private Set<Integer> selection = new HashSet<Integer>();
- /* Some basic adapter logic */
- public int countSelected() {
- return selection.size();
- }
- public void clearSelection() {
- selection.clear();
- notifyDataSetChanged();
- }
- public void checkedStateChanged(int position, boolean checked) {
- if (checked && !selection.contains(position)) {
- selection.add(position);
- } else if (!checked && selection.contains(position)) {
- selection.remove(position);
- }
- notifyDataSetChanged();
- }
- @Override
- public View getView(int location, View view, ViewGroup parent) {
- if (view == null) {
- view = this.inflater.inflate(R.layout.row_transaction, null);
- CheckBox checkbox = (CheckBox) view.findViewById(R.id.row_transaction_checkbox);
- checkbox.setOnCheckedChangeListener(this);
- }
- TextView date = (TextView) view.findViewById(R.id.row_transaction_date);
- TextView description = (TextView) view.findViewById(R.id.row_transaction_description);
- TextView amount = (TextView) view.findViewById(R.id.row_transaction_amount);
- CheckBox checkbox = (CheckBox) view.findViewById(R.id.row_transaction_checkbox);
- NumberFormat amountFormat = NumberFormat.getCurrencyInstance();
- if (location == 0) { // First is the balance
- /* Not usefull code */
- } else {
- Transaction transaction = (Transaction) getItem(location);
- if (transaction != null) {
- DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context);
- date.setText(dateFormat.format(transaction.getDate()));
- description.setText(transaction.getShownDescription(context));
- amount.setText(amountFormat.format(transaction.getAmount()));
- checkbox.setVisibility(View.VISIBLE);
- checkbox.setTag(location);
- if (selection.contains((Integer) location)) {
- checkbox.setChecked(true);
- view.setBackgroundColor(context.getResources().getColor(android.R.color.holo_blue_light));
- } else {
- checkbox.setChecked(false);
- view.setBackgroundColor(context.getResources().getColor(android.R.color.background_light));
- }
- }
- }
- return view;
- }
- // Not used at the moment but i want to have a callback to my activity to manually start the action bar
- public void setOnCheckedChangeListener(OnCheckedChangeListener callback) {
- this.callback = callback;
- }
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if (isChecked) {
- selection.add((Integer) buttonView.getTag());
- } else {
- selection.remove((Integer) buttonView.getTag());
- }
- ListView list = (ListView) buttonView.getParent().getParent();
- if (list != null) {
- list.setItemChecked((Integer) buttonView.getTag(), isChecked);
- }
- if (callback != null) { //Call the activity to start the action bar
- callback.onCheckedChanged(buttonView, isChecked);
- }
- }
- }
- // In my activity
- private boolean contextModeActivated = false;
- public MultiChoiceModeListener multiChoiceMode = new MultiChoiceModeListener() {
- @Override
- public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
- transactionAdapter.checkedStateChanged(position, checked);
- NumberFormat amountFormat = NumberFormat.getCurrencyInstance();
- mode.setTitle(amountFormat.format(transactionAdapter.getTotalAmountSelected()));
- }
- @Override
- public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
- Intent intent;
- switch (item.getItemId()) {
- case R.id.menu_conciliate:
- account.addToBalance(transactionAdapter.getTotalAmountSelected());
- transactionAdapter.removeConciliateTransaction();
- account.save();
- mode.finish();
- return true;
- case R.id.menu_add_transaction:
- intent = new Intent(getActivity(), AddTransaction.class);
- intent.putExtra(AddTransaction.ACCOUNT_ID_TAG, getActivity().getActionBar()
- .getSelectedNavigationIndex());
- startActivityForResult(intent, MainActivity.ADD_TRANSACTION_REQUEST);
- return true;
- case R.id.menu_settings:
- intent = new Intent(getActivity(), Preference.class);
- startActivityForResult(intent, MainActivity.PREFERENCE_REQUEST);
- return true;
- default:
- return false;
- }
- }
- @Override
- public boolean onCreateActionMode(ActionMode mode, Menu menu) {
- MenuInflater inflater = mode.getMenuInflater();
- inflater.inflate(R.menu.transaction_context_menu, menu);
- return true;
- }
- @Override
- public void onDestroyActionMode(ActionMode mode) {
- transactionAdapter.clearSelection();
- }
- @Override
- public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
- return false;
- }
- };
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Intent intent = new Intent(getActivity(), AddTransaction.class);
- intent.putExtra(AddTransaction.ACCOUNT_ID_TAG, getActivity().getActionBar().getSelectedNavigationIndex());
- intent.putExtra(AddTransaction.TRANSACTION_ID_TAG, transactionAdapter.getTransaction(position).getId());
- startActivityForResult(intent, MainActivity.ADD_TRANSACTION_REQUEST);
- }
- @Override
- public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
- ((CheckBox) view.findViewById(R.id.row_transaction_checkbox)).setChecked(true);
- return true;
- }
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- if (transactionAdapter.countSelected() > 0 && !contextModeActivated) {
- getActivity().startActionMode(multiChoiceMode);
- contextModeActivated = true;
- } else if (transactionAdapter.countSelected() == 0 && contextModeActivated){
- // Close the context menu ??
- contextModeActivated = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment