Click here to Skip to main content
11,721,289 members (84,278 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Android
I am trying to display a user's exercise data on a specific date from a sqlite database like this :

Exercises 252.5
walking,30min 70
Running,30min 125.4
treadmill.30min 57.1

in each time the user select a date i retrieve the information of all exercises that are stored in that date and display them in this format , but these information appear only when i remove the excercises_LL.removeAllViews() statement from my code ,and in this case as the user select a date the returned data displayed above the previous one , but i want to do this : in each time the user choose a date the screen content is refreshed so it contains only the data of all exercises returned in that date otherwise " if there is no data returned from the data base( cursor is null)" i want to change the back ground to an image says that there is no data has been inserted .

how i can refresh the content of screen ? and change the screen background?
please help me how i can do that?



this is my code

public class Tracker extends BaseActivity
{
    private Button date_btn;
    private ImageButton  left_btn;
    private ImageButton  right_btn;
    private ImageView    nodata;
    private TextView ex_name;
    private TextView ex_BCals;
    
    private LinearLayout  excercises_LL;
    private  LinearLayout content_LL ;
    private LinearLayout  notes;
    
    private int year,month,day;
    private double  tot_excals_burned;
    private Calendar  localCalendar; 
    private static final int DATE_DIALOG_ID=0;
 
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.tracker);
    
    
    
        date_btn=(Button)findViewById(R.id.btn_date);
        date_btn.setText(FormatDate());
        date_btn.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
                localCalendar = Calendar.getInstance(); 
                year = localCalendar.get(1);
                month= localCalendar.get(2);
                day  =  localCalendar.get(5);
                showDialog(DATE_DIALOG_ID);
            }
    });
 

    left_btn=(ImageButton)findViewById(R.id.btn_left);
    left_btn.setOnClickListener(new View.OnClickListener() {
 
        public void onClick(View v) {
            localCalendar.add(5, -1);
            date_btn.setText(FormatDate(localCalendar,"EEEE, d/MMM/yyyy"));
            RefreshExercisesData();
 
        }
    });
 
    right_btn=(ImageButton)findViewById(R.id.btn_right) ;
    right_btn.setOnClickListener(new View.OnClickListener() {
 
        public void onClick(View v) {
            localCalendar.add(5, 1);
            date_btn.setText(FormatDate(localCalendar,"EEEE, d/MMM/yyyy"));
            RefreshExercisesData();
        }
    });
 

    RefreshExercisesData();
}
 
private String  FormatDate()
{   
 
    localCalendar = Calendar.getInstance();
    return new  SimpleDateFormat("EEEE, d/MMM/yyyy").format(localCalendar.getTime());
 
}
 
private String FormatDate(int year, int month, int day)
{   
    localCalendar = Calendar.getInstance();
    localCalendar.set(year, month, day);
 
    return new  SimpleDateFormat("EEEE, d/MMM/yyyy").format(localCalendar.getTime());
}
 
private String FormatDate(Calendar   calendar , String  format)
{
 
    return new  SimpleDateFormat(format).format(calendar.getTime());
 
}
 

 

 

private void RefreshExercisesData()
{   
    nodata=(ImageView)findViewById(R.id.no_data_image);
 
    excercises_LL=(LinearLayout)findViewById(R.id.ll_exercises);
    tot_excals_burned=0;
 
    DBAdapter db = new DBAdapter(this);
    db.open();
    String selected_date= date_btn.getText().toString();
    Log.e("date", selected_date);
 

    Cursor c = db.getExerciseInfo(selected_date);
 
    if(c !=null)
    {
 
        nodata.setVisibility(8);
        excercises_LL.removeAllViews();
        excercises_LL.setWeightSum(1.0F);
        excercises_LL.setVisibility(0);
        excercises_LL.setOrientation(LinearLayout.VERTICAL);
 
        LayoutInflater exc_LayoutInflater = (LayoutInflater)getApplicationContext().getSystemService("layout_inflater");
 

        LinearLayout layout = (LinearLayout)exc_LayoutInflater.inflate(R.layout.tracker_header_item,null);
        TextView   tot_ex_cals_value=((TextView)(layout).findViewById(R.id.tv_tot_cals_value));
        TextView   exs_title=((TextView)(layout).findViewById(R.id.tv_item_title)) ;
        exs_title.setText("Exercises ");
 
        (layout).setPadding(0, 36, 0, 0);
        excercises_LL.addView((View)layout, 0);
        int i = 1;
 
        if (c.moveToFirst())
        {
            do 
            {
                content_LL=new LinearLayout(this);
 
                ex_name=new TextView(this);
                ex_name.setText( " "+c.getFloat(1)+", " +c.getString(0) + " minute");
                ex_name.setTextColor(R.color.black);
                content_LL.addView(ex_name,0);
                ex_BCals=new TextView(this);
                ex_BCals.setText(Round(c.getFloat(2)) +" ");
                ex_BCals.setTextColor(R.color.black);
                tot_excals_burned = tot_excals_burned+c.getFloat(2);
 
                content_LL.addView(ex_BCals,1);
                excercises_LL.addView(content_LL, i);
 

                i++; 
 
            }
            while (c.moveToNext());
 
        }
        tot_ex_cals_value.setText(Round(tot_excals_burned) + " cals");
 
    }
    else
    {    
        nodata.setVisibility(0);
    }
    c.close();
    c.deactivate();
    db.close();
 
}
 

 
protected Dialog onCreateDialog(int id)
{
    switch (id) {
 
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, this.year, this.month, this.day);
    }
    return null;
}
 
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener()
{
    public void onDateSet(DatePicker paramDatePicker, int year, int monthofYear, int dayofMonth)
    {
        Tracker.this.year=year;
        month=monthofYear;
        day=dayofMonth;
        date_btn.setText(FormatDate(year,month,day));
        RefreshExercisesData();
 
    }
 
};
 

private  String Round(double num) {
    return String.format("%.1f%n", num);
 
}}
Posted 11-Jun-12 22:40pm
Edited 12-Jun-12 0:12am
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

To handle the image, I would use an ImageView in your layout, and the change its visibility using
imageView.setVisibility(showImage ? View.VISIBLE : View.GONE);
and wrap your text data in a LinearLayout and do the opposite on that.

About your other problem, I don't fully understand what you mean, but have you considered using a ListView and a custom ArrayAdapter? Seems to be the perfect match.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 515
1 OriginalGriff 279
2 Mika Wendelius 170
3 F-ES Sitecore 150
4 cyycoish 70
0 Sergey Alexandrovich Kryukov 2,209
1 OriginalGriff 969
2 F-ES Sitecore 660
3 Maciej Los 609
4 Richard MacCutchan 560


Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 12 Jun 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100