Click here to Skip to main content
15,903,729 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
In my Android application i have a tracker activity in which i retrieve the exercises information(name , period , burned calories) from the sqlite data base based on the selected date and display these information in a linear layout , and my problem that as the user select new date the retrieved data are displayed in another "new " layout appear above the old one but what actually i want to do is to display the new retrieved data on the same layout " change the layout content with the new retrieved data ", i have tried the remove all views method but it didn't work since the data appear for few minutes then dis appear

How i can do this: when the user select a new date the new retrieved data displayed on the same layout " refresh the old data by the new one " not to display them in anew layout . how i can do that ? please help me...

java 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 LinearLayout  details;  
private int year,month,day;
private double  tot_excals_burned;
private Calendar  localCalendar; 
private static final int DATE_DIALOG_ID=0;
private  boolean  has_ex_details;
private boolean  has_meal_details=false;
private Cursor exercises_cursor;


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();
            RefreshNoDataImage();
        }
    });

    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();
            RefreshNoDataImage();

        }
    });


    details=(LinearLayout)findViewById(R.id.ll_details);
    notes=(LinearLayout)findViewById(R.id.ll_notes);
    excercises_LL=(LinearLayout)findViewById(R.id.ll_exercises);
    nodata=(ImageView)findViewById(R.id.nodata_imgV);



    RefreshExercisesData();
    RefreshNoDataImage();


}


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()
{   



    tot_excals_burned=0;

    DBAdapter db = new DBAdapter(this);
    db.open();
    String selected_date= date_btn.getText().toString();
    Log.e("date", selected_date);


    exercises_cursor = db.getExerciseInfo(selected_date);

    if(exercises_cursor.getCount() !=0 )
    {

        has_ex_details=true;

        details.setVisibility(0);
        nodata.setVisibility(8);
        notes.setVisibility(0);

        //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 (exercises_cursor.moveToFirst())
        {
            do 
            {
                content_LL=new LinearLayout(this);
                ex_name=new TextView(this);
                ex_name.setText( exercises_cursor.getFloat(1)+","  +exercises_cursor.getString(0) + "min ");
                ex_name.setTextColor(R.color.black);
                content_LL.addView(ex_name,0);

                ex_BCals=new TextView(this);
                ex_BCals.setText(Round(exercises_cursor.getFloat(2)) +" ");
                ex_BCals.setTextColor(R.color.color_black);
                content_LL.addView(ex_BCals,1);

                tot_excals_burned = tot_excals_burned+exercises_cursor.getFloat(2);

                excercises_LL.addView(content_LL, i);


                i++; 

            }
            while (exercises_cursor.moveToNext());

        }
        tot_ex_cals_value.setText(Round(tot_excals_burned) );

    }
    else if(exercises_cursor.getCount()==0 ||tot_excals_burned==0)
    {  

        has_ex_details=false;


        RefreshNoDataImage();

    }
    exercises_cursor.close();
    exercises_cursor.deactivate();
    db.close();

}


private void RefreshNoDataImage()
{    
    if(has_ex_details==false && has_meal_details==false)
    {       

        notes.setVisibility(8);
        excercises_LL.setVisibility(8);
        nodata.setImageResource(R.drawable.bg_nodata);
        nodata.setVisibility(View.VISIBLE);


    }

    else
        nodata.setVisibility(8);


}




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();
        RefreshNoDataImage();

    }

};


private  String Round(double num) {
    return String.format("%.1f%n", num);

}}
<pre lang="java">
Posted
Updated 13-Jun-12 10:56am
v3

1 solution

Here is how to replace one View with another:
Java
View oldView = findViewById(R.id.firstView);
ViewGroup parent = (ViewGroup) oldView.getParent();

int index = parent.indexOfChild(oldView);
parent.removeView(oldView);

View newView = getLayoutInflater().inflate(R.layout.newView, false);
parent.addView(newView, index);

Error handling omitted.
 
Share this answer
 
Comments
nice dream 14-Jun-12 4:32am    
thanks for helping me , but can you explain more about how i can use them in my case because in my activity i want to change the content of the same text views with new returned data from my sqlite data base , please help me since iam new in android
Niklas L 14-Jun-12 5:38am    
You need to review your code and clean it up. For example:
//excercises_LL.removeAllViews();
excercises_LL.setWeightSum(1.0F);
excercises_LL.setVisibility(0);

Why have you removed removeAllViews()?
Why do you set a weight sum when you don't know what that sum is?
Why do you use numeric values, when you should use named constants like View.VISIBLE, View.HIDDEN and View.GONE?

You are not making your life easy.

Now, all you need to do is to set the texts of your TextViews again using setText(). To do this you need to make your content_LL identifiable. Use the id of the table record (_id) and set it as the views id. Then you can find it again using excerise_LL.findViewById(). If you don't have a numeric key, you could make use of the setTag() method to make it identifiable.
nice dream 14-Jun-12 5:50am    
I have used the excercises_LL.removeAllViews() statement in my code because when i removed it the following happens: when the user select a date the exercises information " text views " didn't appear , but after clicking 3 dates all the data that retrieved and didn't appear previously it appears at once an under each others

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900