在数据结构中聚合来自数据库的数据

时间:2011-11-22 13:31:09

标签: java android

我有一张表格,其中包括:

vaccine_date text ,vaccine_name text ,mother_id text  , child_id text ,address  text

在同一个孩子的同一天,这个表可以有多种疫苗,所以该列在同一日期重复使用不同的疫苗名称。

我必须创建一个数据结构(HashMap),它应该将日期作为关键字,并将所有疫苗名称作为Arraylist作为迄今为止的值。

我没有出路。写了一些代码:

HashMap<String ,ArrayList> hmap = new HashMap<String,ArrayList>();
        String [] vaclist =new String [10];
        String [] disdatelist = new String[20];
        //ArrayList<String> ashu =new ArrayList<String>(); 

        Cursor curdate=database.query(true,"calendar_updation", new String []{"vaccine_date"}, "child_id"+"="+ "?", new String []{childnme},null, null, null, null);
        while (curdate.moveToNext()){
            String distinctvacdate = curdate.getString(curdate.getColumnIndex("vaccine_date"));
            ArrayList<String> ashu_distinctvacdate =new ArrayList<String>();
            hmap.put(distinctvacdate, ashu_distinctvacdate);
        }
        // Addition of this part ends here 
        Cursor cur=database.query(true,"calendar_updation", new String []{"vaccine_date","vaccine_name","address"}, "child_id"+"="+ "?", new String []{childnme},null, null, null, null);
        while(cur.moveToNext()){
             // Addition for arraylist starts here 
            while (hmap.isEmpty()){
              if (hmap.equals(cur.getString(cur.getColumnIndex("vaccine_date")))){
                  String vaccinename = cur.getString(cur.getColumnIndex("vaccine_name"));  
                  ashu.add(vaccinename);
              }
              hmap.put(key, ashu);

            }

1 个答案:

答案 0 :(得分:0)

是的,我的控制块错了。当我想到我的控制块时,我做对了。

在这里我们改变了代码:

private String child_id;
        private String address;

        public String getAddress() {
            return address;
        }




        public void setAddress(String address) {
            this.address = address;
        }

        final public HashMap<String,ArrayList<String>> childVaccineListByDate=new  HashMap<String,ArrayList<String>>();


        public void addVaccine(String date, String vaccineName)
        {
            ArrayList<String> listOfVaccineNames;

            if(childVaccineListByDate.containsKey(date) )
            {
                listOfVaccineNames=childVaccineListByDate.get(date);
                if(!listOfVaccineNames.contains(vaccineName))
                {
                    listOfVaccineNames.add(vaccineName);
                }

            }else
            {
                listOfVaccineNames=new ArrayList<String>();
                listOfVaccineNames.add(vaccineName);
                childVaccineListByDate.put(date,listOfVaccineNames);
            }

        }




        public HashMap<String, ArrayList<String>> getChildVaccineListByDate() {
            return childVaccineListByDate;
        }




        public String getChild_id() {
            return child_id;
        }

        public void setChild_id(String child_id) {
            this.child_id = child_id;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result
                    + ((child_id == null) ? 0 : child_id.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            ChildVaccine other = (ChildVaccine) obj;
            if (child_id == null) {
                if (other.child_id != null)
                    return false;
            } else if (!child_id.equals(other.child_id))
                return false;
            return true;
        }