Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ExpenseCursorAdapter extends CursorAdapter {
- private NumberFormat formatter = NumberFormat.getCurrencyInstance();
- Context context;
- private DatabaseHelper db;
- public ExpenseCursorAdapter(Context context, Cursor c) {
- super(context, c);
- }
- @Override
- public View newView(Context context, Cursor cursor, ViewGroup parent) {
- // when the view will be created for first time,
- // we need to tell the adapters, how each item will look
- LayoutInflater inflater = LayoutInflater.from(parent.getContext());
- View retView = inflater.inflate(R.layout.expense_single_row_item, parent, false);
- return retView;
- }
- @Override
- public void bindView(View view, Context context, Cursor cursor) {
- // here we are setting our data
- // that means, take the data from the cursor and put it in views
- TextView textViewParticipantId = (TextView) view.findViewById(R.id.tv_expense_participant);
- textViewParticipantId.setText(cursor.getString(cursor.getColumnIndex("name")));
- TextView textViewExpenseDescription = (TextView) view.findViewById(R.id.tv_expense_description);
- textViewExpenseDescription.setText(cursor.getString(cursor.getColumnIndex("description")));
- TextView textViewExpenseAmount = (TextView) view.findViewById(R.id.tv_expense_amount);
- textViewExpenseAmount.setText(formatter.format(Double.valueOf(cursor.getDouble(cursor.getColumnIndex("amount")))));
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:stretchColumns="1,2,3" >
- <TableRow
- android:id="@+id/tableRow1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:weightSum="10" >
- <CheckBox
- android:id="@+id/cb_expense_row"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:button="@drawable/selector_check"
- android:focusable="false"
- android:padding="2dip"
- android:weightSum="1"/>
- <TextView
- android:id="@+id/tv_expense_participant"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:paddingBottom="5dp"
- android:weightSum="3"
- android:gravity="left"/>
- <TextView
- android:id="@+id/tv_expense_description"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:paddingBottom="5dp"
- android:weightSum="3"
- android:gravity="left"/>
- <TextView
- android:id="@+id/tv_expense_amount"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:paddingBottom="5dp"
- android:weightSum="3"
- android:gravity="left" />
- </TableRow>
- private static final String TAG = ExpenseActivity.class.getSimpleName();
- private ListView listView;
- private ExpenseCursorAdapter customAdapter;
- private DatabaseHelper databaseHelper;
- private long eventId;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_expense);
- ActionBar actionBar = getSupportActionBar();
- actionBar.setDisplayHomeAsUpEnabled(true);
- eventId = getIntent().getLongExtra("tag_event_id", -1);
- databaseHelper = new DatabaseHelper(this);
- listView = (ListView) findViewById(R.id.expense_list_data);
- listView.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- Log.d(TAG, "clicked on item: " + position+" - ID: "+id);
- }
- });
- new Handler().post(new Runnable() {
- @Override
- public void run() {
- customAdapter = new ExpenseCursorAdapter(ExpenseActivity.this, databaseHelper.getAllExpensesByEventCursor(eventId));
- listView.setAdapter(customAdapter);
- }
- });
- listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
- }
- ...
- private void removeExpenses() {
- Log.d(TAG, "Remove Expense button clicked on");
- DatabaseHelper db = new DatabaseHelper(this);
- SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions();
- int itemCount = listView.getCount();
- for (int i = itemCount - 1; i >=0; i--) {
- if (checkedItemPositions.valueAt(i)) {
- //db.deleteExpense(listView.getItemIdAtPosition(checkedItemPositions.keyAt(i)));
- db.deleteExpense(listView.getItemIdAtPosition(i));
- }
- customAdapter.notifyDataSetChanged();
- }
- checkedItemPositions.clear();
- listView.setAdapter(customAdapter);
- listView.clearChoices();
- if (db != null){
- db.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement