Guest User

Untitled

a guest
Oct 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. public class AddMonthlyExpenses extends AppCompatActivity {
  2.  
  3. ArrayList<ListObj> groupList= new ArrayList<ListObj>();;
  4. List<String> childList;
  5. Map<ListObj, List<String>> laptopCollection;
  6. ExpandableListView listview;
  7. ExpandableListAdapter expListAdapter;
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.add_monthly_expenses);
  13. laptopCollection = new LinkedHashMap<ListObj, List<String>>();
  14. listview = (ExpandableListView) findViewById(R.id.exlistView);
  15. expListAdapter = new ExpandableListAdapter(getApplication(), groupList, laptopCollection);
  16. listview.setAdapter(expListAdapter);
  17. retrieveList(name);
  18. }
  19.  
  20. public void retrieveList(String name) {
  21. database = mdb.getReadableDatabase();
  22. Cursor cursor = database.rawQuery("SELECT * FROM " + MyDatabaseHelper.TABLE__TASK + " WHERE Name = ? ", new String[]{name}, null);
  23. if (cursor != null && cursor.getCount() > 0) {
  24. while (cursor.moveToNext()) {
  25. groupList = new ArrayList<ListObj>();
  26. int iD = cursor.getInt(cursor.getColumnIndex("ID"));
  27. String month = cursor.getString(cursor.getColumnIndex("Month"));
  28. double budget = cursor.getDouble(cursor.getColumnIndex("Budget"));
  29. groupList.add(new ListObj(iD,month,budget));
  30. createCollection(); // for child items
  31. if (expListAdapter != null) {
  32. expListAdapter.add(iD, month, budget);
  33. listview.setAdapter(expListAdapter);
  34. }
  35. }
  36. }
  37. }
  38.  
  39. private void createCollection() {
  40. String[] options = {"Edit","Delete"};
  41. for (ListObj laptop : groupList) {
  42. loadChild(options);
  43. laptopCollection.put(laptop, childList);
  44. }
  45. }
  46.  
  47. private void loadChild(String[] laptopModels) {
  48. childList = new ArrayList<String>();
  49. for (String model : laptopModels)
  50. childList.add(model);
  51. }
  52. }
  53.  
  54. public class ExpandableListAdapter extends BaseExpandableListAdapter {
  55.  
  56. private Context context;
  57. Map<ListObj, List<String>> laptopCollections;
  58. private ArrayList<ListObj> laptops;
  59. private LayoutInflater mInflater;
  60.  
  61. public ExpandableListAdapter(Context context, ArrayList<ListObj>laptops, Map<ListObj, List<String>> laptopCollections) {
  62. this.context = context;
  63. this.laptopCollections = laptopCollections;
  64. this.laptops = laptops;
  65. mInflater = LayoutInflater.from(context);
  66. }
  67.  
  68. public Object getChild(int groupPosition, int childPosition) {
  69. return laptopCollections.get(laptops.get(groupPosition)).get(childPosition);
  70. }
  71.  
  72. public void add(int id, String month, double budget) {
  73. String[] splited = month.split("\s+");
  74. ListObj obj = new ListObj(id, month, budget);
  75. obj.setYear(splited[1]);
  76. obj.setMonth(splited[0]);
  77. obj.setBudget(budget);
  78. obj.setID(id);
  79. laptops.add(obj);
  80. this.notifyDataSetChanged();
  81. }
  82.  
  83. public int getChildrenCount(int groupPosition) {
  84. return laptopCollections.get(laptops.get(groupPosition)).size();
  85. }
  86.  
  87. public Object getGroup(int groupPosition) {
  88. return laptops.get(groupPosition);
  89. }
  90.  
  91. public int getGroupCount() {
  92. return this.laptops.size();
  93. }
  94.  
  95. public long getGroupId(int groupPosition) {
  96. return groupPosition;
  97. }
  98. }
Add Comment
Please, Sign In to add comment