Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. public class ExpenseCursorAdapter extends CursorAdapter {
  2. private NumberFormat formatter = NumberFormat.getCurrencyInstance();
  3. Context context;
  4. private DatabaseHelper db;
  5.  
  6. public ExpenseCursorAdapter(Context context, Cursor c) {
  7. super(context, c);
  8. }
  9.  
  10.  
  11. @Override
  12. public View newView(Context context, Cursor cursor, ViewGroup parent) {
  13. // when the view will be created for first time,
  14. // we need to tell the adapters, how each item will look
  15. LayoutInflater inflater = LayoutInflater.from(parent.getContext());
  16. View retView = inflater.inflate(R.layout.expense_single_row_item, parent, false);
  17.  
  18. return retView;
  19. }
  20.  
  21. @Override
  22. public void bindView(View view, Context context, Cursor cursor) {
  23. // here we are setting our data
  24. // that means, take the data from the cursor and put it in views
  25.  
  26. TextView textViewParticipantId = (TextView) view.findViewById(R.id.tv_expense_participant);
  27. textViewParticipantId.setText(cursor.getString(cursor.getColumnIndex("name")));
  28.  
  29. TextView textViewExpenseDescription = (TextView) view.findViewById(R.id.tv_expense_description);
  30. textViewExpenseDescription.setText(cursor.getString(cursor.getColumnIndex("description")));
  31.  
  32. TextView textViewExpenseAmount = (TextView) view.findViewById(R.id.tv_expense_amount);
  33. textViewExpenseAmount.setText(formatter.format(Double.valueOf(cursor.getDouble(cursor.getColumnIndex("amount")))));
  34.  
  35.  
  36.  
  37. }
  38.  
  39. }
  40.  
  41. <?xml version="1.0" encoding="utf-8"?>
  42. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  43. android:layout_width="match_parent"
  44. android:layout_height="wrap_content"
  45. android:stretchColumns="1,2,3" >
  46.  
  47. <TableRow
  48. android:id="@+id/tableRow1"
  49. android:layout_width="match_parent"
  50. android:layout_height="wrap_content"
  51. android:weightSum="10" >
  52.  
  53. <CheckBox
  54. android:id="@+id/cb_expense_row"
  55. android:layout_width="wrap_content"
  56. android:layout_height="wrap_content"
  57. android:layout_gravity="center_vertical"
  58. android:button="@drawable/selector_check"
  59. android:focusable="false"
  60. android:padding="2dip"
  61. android:weightSum="1"/>
  62.  
  63. <TextView
  64. android:id="@+id/tv_expense_participant"
  65. android:layout_width="0dp"
  66. android:layout_height="wrap_content"
  67. android:paddingBottom="5dp"
  68. android:weightSum="3"
  69. android:gravity="left"/>
  70.  
  71. <TextView
  72. android:id="@+id/tv_expense_description"
  73. android:layout_width="0dp"
  74. android:layout_height="wrap_content"
  75. android:paddingBottom="5dp"
  76. android:weightSum="3"
  77. android:gravity="left"/>
  78.  
  79. <TextView
  80. android:id="@+id/tv_expense_amount"
  81. android:layout_width="0dp"
  82. android:layout_height="wrap_content"
  83. android:paddingBottom="5dp"
  84. android:weightSum="3"
  85. android:gravity="left" />
  86. </TableRow>
  87.  
  88. private static final String TAG = ExpenseActivity.class.getSimpleName();
  89. private ListView listView;
  90. private ExpenseCursorAdapter customAdapter;
  91. private DatabaseHelper databaseHelper;
  92. private long eventId;
  93. @Override
  94. protected void onCreate(Bundle savedInstanceState) {
  95. super.onCreate(savedInstanceState);
  96. setContentView(R.layout.activity_expense);
  97. ActionBar actionBar = getSupportActionBar();
  98. actionBar.setDisplayHomeAsUpEnabled(true);
  99.  
  100.  
  101. eventId = getIntent().getLongExtra("tag_event_id", -1);
  102.  
  103. databaseHelper = new DatabaseHelper(this);
  104.  
  105.  
  106. listView = (ListView) findViewById(R.id.expense_list_data);
  107. listView.setOnItemClickListener(new OnItemClickListener() {
  108.  
  109.  
  110. @Override
  111. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  112. Log.d(TAG, "clicked on item: " + position+" - ID: "+id);
  113. }
  114. });
  115.  
  116.  
  117.  
  118. new Handler().post(new Runnable() {
  119. @Override
  120. public void run() {
  121.  
  122. customAdapter = new ExpenseCursorAdapter(ExpenseActivity.this, databaseHelper.getAllExpensesByEventCursor(eventId));
  123. listView.setAdapter(customAdapter);
  124.  
  125. }
  126. });
  127.  
  128. listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  129. }
  130. ...
  131. private void removeExpenses() {
  132. Log.d(TAG, "Remove Expense button clicked on");
  133.  
  134. DatabaseHelper db = new DatabaseHelper(this);
  135.  
  136. SparseBooleanArray checkedItemPositions = listView.getCheckedItemPositions();
  137.  
  138. int itemCount = listView.getCount();
  139.  
  140. for (int i = itemCount - 1; i >=0; i--) {
  141. if (checkedItemPositions.valueAt(i)) {
  142.  
  143. //db.deleteExpense(listView.getItemIdAtPosition(checkedItemPositions.keyAt(i)));
  144. db.deleteExpense(listView.getItemIdAtPosition(i));
  145.  
  146. }
  147.  
  148. customAdapter.notifyDataSetChanged();
  149. }
  150.  
  151. checkedItemPositions.clear();
  152.  
  153. listView.setAdapter(customAdapter);
  154.  
  155. listView.clearChoices();
  156.  
  157. if (db != null){
  158. db.close();
  159. }
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement