如何在列表视图android上显示返回日期和未来日期内容

时间:2014-11-28 07:41:09

标签: android sql sqlite android-activity android-sqlite

我在列表视图中显示今天日期数据库中的内容,我想添加点击日期按钮的功能,显示上一日期的内容。但是它显示了两个日期的数据。它将它添加到相同的列表视图中。如何解决?

这是上一个日期函数

prevMonth.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                c.add(Calendar.DATE, -1);
                formattedDate = df.format(c.getTime());

                Log.v("PREVIOUS DATE : ", formattedDate);
                currentDate.setText(formattedDate);
                new getfooditems().execute();//calling method of different class which displays the data in the list
                //ListView lv = getListView();
            }
        });

getfooditems

class getfooditems extends AsyncTask<String, String, String> {

        private String foodname;
        private String totalcalorie;
        private String fat;
        private String carbohydrate;
        private String protein;
        private String foodtype;
        private String created_at;

        //private String category;


        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            CalorieDMealsDiaryActivity.this.setProgressBarIndeterminateVisibility(true);
        }


        @Override
        protected String doInBackground(String... params) {

            DatabaseHandler helper = new DatabaseHandler(getApplicationContext());
            SQLiteDatabase db = helper.getReadableDatabase();
            String query = "SELECT foodname,foodtype,totalcalorie,fat,carbohydrate,protein,created_at from fooditem WHERE name = ('"+fname+"') AND created_at = ('"+formattedDate+"')";

            Cursor cursor = db.rawQuery(query,null);

            // Building Parameters
            List<NameValuePair> parametres = new ArrayList<NameValuePair>();

            // there is some record available in the cursor 
            if (!cursor.isAfterLast()) {
                cursor.moveToFirst();

                while (!cursor.isAfterLast()) {
                    foodname = cursor.getString(0);
                    System.out.println("value of foodname in meals diary activity" + foodname);
                    foodtype = cursor.getString(1);
                    System.out.println("meals diary foodtype" + foodtype);
                    totalcalorie= cursor.getString(2);
                    fat = cursor.getString(3);
                    carbohydrate = cursor.getString(4);
                    protein = cursor.getString(5);
                    created_at = cursor.getString(6);
                    System.out.println("value of totalcalorie in meals diary activity " + totalcalorie);
                    Log.e("foodtype" , foodtype);



                    // creating new HashMap
                    HashMap<String, String> breakfastmap = new HashMap<String, String>();
                    HashMap<String, String> lunchmap = new HashMap<String, String>();
                    HashMap<String, String> dinnermap = new HashMap<String, String>();
                    HashMap<String, String> snacksmap = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    if(foodtype.equals("Breakfast")){
                        breakfastmap.put(TAG_FOODNAME, foodname);
                        Log.e("foodname","foodname:" + foodname);
                        breakfastmap.put(TAG_TOTALCALORIE, totalcalorie);
                        Log.e("totalcalorie","totalcalorie:" + totalcalorie);
                        breakfastmap.put(TAG_FAT, fat);
                        Log.e("fat","fat:" + fat);
                        breakfastmap.put(TAG_CARBOHYDRATE, carbohydrate);
                        Log.e("carbohydrate","carbohydrate:" + carbohydrate);
                        breakfastmap.put(TAG_PROTEIN, protein);
                        Log.e("protein","protein:" + protein);
                        breakfastmap.put(TAG_CREATED_AT, created_at);
                        Log.e("created_at","created_at:" + created_at);


                        // adding HashList to ArrayList
                        breakfastitems.add(breakfastmap);
                        cursor.moveToNext();

                    }
                    else if(foodtype.equals("Lunch")){
                        lunchmap.put(TAG_FOODNAME, foodname);
                        Log.e("foodname","foodname:" + foodname);
                        lunchmap.put(TAG_TOTALCALORIE, totalcalorie);
                        Log.e("totalcalorie","totalcalorie:" + totalcalorie);
                        lunchmap.put(TAG_FAT, fat);
                        Log.e("fat","fat:" + fat);
                        lunchmap.put(TAG_CARBOHYDRATE, carbohydrate);
                        Log.e("carbohydrate","carbohydrate:" + carbohydrate);
                        lunchmap.put(TAG_PROTEIN, protein);
                        Log.e("protein","protein:" + protein);
                        lunchmap.put(TAG_CREATED_AT, created_at);
                        Log.e("created_at","created_at:" + created_at);


                        // adding HashList to ArrayList
                        lunchitems.add(lunchmap);
                        cursor.moveToNext();
                    }
                    else if(foodtype.equals("Dinner")){
                        dinnermap.put(TAG_FOODNAME, foodname);
                        Log.e("foodname","foodname:" + foodname);
                        dinnermap.put(TAG_TOTALCALORIE, totalcalorie);
                        Log.e("totalcalorie","totalcalorie:" + totalcalorie);
                        dinnermap.put(TAG_FAT, fat);
                        Log.e("fat","fat:" + fat);
                        dinnermap.put(TAG_CARBOHYDRATE, carbohydrate);
                        Log.e("carbohydrate","carbohydrate:" + carbohydrate);
                        dinnermap.put(TAG_PROTEIN, protein);
                        Log.e("protein","protein:" + protein);
                        dinnermap.put(TAG_CREATED_AT, created_at);
                        Log.e("created_at","created_at:" + created_at);

                        // adding HashList to ArrayList
                        dinneritems.add(dinnermap);
                        cursor.moveToNext();
                    }
                    else if(foodtype.equals("Snacks")){
                        snacksmap.put(TAG_FOODNAME, foodname);
                        Log.e("foodname","foodname:" + foodname);
                        snacksmap.put(TAG_TOTALCALORIE, totalcalorie);
                        Log.e("totalcalorie","totalcalorie:" + totalcalorie);
                        snacksmap.put(TAG_FAT, fat);
                        Log.e("fat","fat:" + fat);
                        snacksmap.put(TAG_CARBOHYDRATE, carbohydrate);
                        Log.e("carbohydrate","carbohydrate:" + carbohydrate);
                        snacksmap.put(TAG_PROTEIN, protein);
                        Log.e("protein","protein:" + protein);
                        snacksmap.put(TAG_CREATED_AT, created_at);
                        Log.e("created_at","created_at:" + created_at);

                        // adding HashList to ArrayList
                        snacksitems.add(snacksmap);
                        cursor.moveToNext();
                    }
                    else{
                        System.out.println("Nonononono");
                    }
                }
            }

            // there is some record available in the cursor 

            cursor.close();

            return query;   

        }




        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String result) {
            CalorieDMealsDiaryActivity.this
            .setProgressBarIndeterminateVisibility(false);
            // updating UI from Background Thread
            runOnUiThread(new Runnable() {

                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     * */
                    System.out.println("foodtype in run" + foodtype);

                    System.out.println("I am inside run() hahahaha");
                    adapter = new SimpleAdapter(
                            CalorieDMealsDiaryActivity.this, breakfastitems,
                            R.layout.list_xml, new String[] {TAG_FOODNAME ,TAG_TOTALCALORIE,TAG_FAT,TAG_CARBOHYDRATE,TAG_PROTEIN}, new int[] { 
                                    R.id.name,R.id.uid, R.id.fatout,R.id.carbsout,R.id.proteinsout });

                    adapterlunch = new SimpleAdapter(
                            CalorieDMealsDiaryActivity.this, lunchitems,
                            R.layout.list_lunch_xml, new String[] {TAG_FOODNAME ,TAG_TOTALCALORIE,TAG_FAT,TAG_CARBOHYDRATE,TAG_PROTEIN}, new int[] { 
                                    R.id.name,R.id.uid, R.id.fatout,R.id.carbsout,R.id.proteinsout });

                    adapterdinner = new SimpleAdapter(
                            CalorieDMealsDiaryActivity.this, dinneritems,
                            R.layout.list_dinner_xml, new String[] {TAG_FOODNAME ,TAG_TOTALCALORIE,TAG_FAT,TAG_CARBOHYDRATE,TAG_PROTEIN}, new int[] { 
                                    R.id.name,R.id.uid, R.id.fatout,R.id.carbsout,R.id.proteinsout });

                    adaptersnacks = new SimpleAdapter(
                            CalorieDMealsDiaryActivity.this, snacksitems,
                            R.layout.list_snacks_xml, new String[] {TAG_FOODNAME ,TAG_TOTALCALORIE,TAG_FAT,TAG_CARBOHYDRATE,TAG_PROTEIN}, new int[] { 
                                    R.id.name,R.id.uid, R.id.fatout,R.id.carbsout,R.id.proteinsout });

                    listbreakfast.setAdapter(adapter);
                    listlunch.setAdapter(adapterlunch);
                    listdinner.setAdapter(adapterdinner);
                    listsnacks.setAdapter(adaptersnacks);


                }
            });
        }



    }

2 个答案:

答案 0 :(得分:0)

你做了很多复杂的事情我认为要获得当前或以前的日期。

只需你可以做到上一次。

    Calendar cal = Calendar.getInstance();
    int currentDate = cal.get(Calendar.DATE) -1;//prev date , remove -1 for current date.

当我们需要在月份日期和所有日期格式化日期时,我们正在使用SimpleDateFormate ...

这样做可以重新加载listview .... asynch任务声明上面的所有列表视图

onprepare       而不是初始化准备

中的所有列表 doinbackground中的

在使用前清除所有列表

而不是适应后期......

希望你能解决问题

答案 1 :(得分:0)

在从数据库中读取新记录之前,清除所有arraylist中的所有先前项目,如:

    breakfastitems.clear();
    lunchitems.clear();
    dinneritems.clear();
    snacksitems.clear();

它将解决您的问题