Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AddMonthlyExpenses extends AppCompatActivity {
- ArrayList<ListObj> groupList= new ArrayList<ListObj>();;
- List<String> childList;
- Map<ListObj, List<String>> laptopCollection;
- ExpandableListView listview;
- ExpandableListAdapter expListAdapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.add_monthly_expenses);
- laptopCollection = new LinkedHashMap<ListObj, List<String>>();
- listview = (ExpandableListView) findViewById(R.id.exlistView);
- expListAdapter = new ExpandableListAdapter(getApplication(), groupList, laptopCollection);
- listview.setAdapter(expListAdapter);
- retrieveList(name);
- }
- public void retrieveList(String name) {
- database = mdb.getReadableDatabase();
- Cursor cursor = database.rawQuery("SELECT * FROM " + MyDatabaseHelper.TABLE__TASK + " WHERE Name = ? ", new String[]{name}, null);
- if (cursor != null && cursor.getCount() > 0) {
- while (cursor.moveToNext()) {
- groupList = new ArrayList<ListObj>();
- int iD = cursor.getInt(cursor.getColumnIndex("ID"));
- String month = cursor.getString(cursor.getColumnIndex("Month"));
- double budget = cursor.getDouble(cursor.getColumnIndex("Budget"));
- groupList.add(new ListObj(iD,month,budget));
- createCollection(); // for child items
- if (expListAdapter != null) {
- expListAdapter.add(iD, month, budget);
- listview.setAdapter(expListAdapter);
- }
- }
- }
- }
- private void createCollection() {
- String[] options = {"Edit","Delete"};
- for (ListObj laptop : groupList) {
- loadChild(options);
- laptopCollection.put(laptop, childList);
- }
- }
- private void loadChild(String[] laptopModels) {
- childList = new ArrayList<String>();
- for (String model : laptopModels)
- childList.add(model);
- }
- }
- public class ExpandableListAdapter extends BaseExpandableListAdapter {
- private Context context;
- Map<ListObj, List<String>> laptopCollections;
- private ArrayList<ListObj> laptops;
- private LayoutInflater mInflater;
- public ExpandableListAdapter(Context context, ArrayList<ListObj>laptops, Map<ListObj, List<String>> laptopCollections) {
- this.context = context;
- this.laptopCollections = laptopCollections;
- this.laptops = laptops;
- mInflater = LayoutInflater.from(context);
- }
- public Object getChild(int groupPosition, int childPosition) {
- return laptopCollections.get(laptops.get(groupPosition)).get(childPosition);
- }
- public void add(int id, String month, double budget) {
- String[] splited = month.split("\s+");
- ListObj obj = new ListObj(id, month, budget);
- obj.setYear(splited[1]);
- obj.setMonth(splited[0]);
- obj.setBudget(budget);
- obj.setID(id);
- laptops.add(obj);
- this.notifyDataSetChanged();
- }
- public int getChildrenCount(int groupPosition) {
- return laptopCollections.get(laptops.get(groupPosition)).size();
- }
- public Object getGroup(int groupPosition) {
- return laptops.get(groupPosition);
- }
- public int getGroupCount() {
- return this.laptops.size();
- }
- public long getGroupId(int groupPosition) {
- return groupPosition;
- }
- }
Add Comment
Please, Sign In to add comment