如何改变布局的内容? [英] How to change the content of the layout?

查看:174
本文介绍了如何改变布局的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我有一个跟踪活动中,我从基于选定日期的SQLite数据库检索演习信息(姓名,时间,消耗的热量),并显示在一个线性布局这些信息,我的问题作为用户选择检索的数据显示在另一个新的布局新的日期显示上方旧的,但究竟是什么我想要做的是显示在相同的布局,新检索到的数据换用新检索的数据布局内容我曾尝试删除所有意见的方法,但由于数据出现几分钟,然后解散似乎没有奏效

我怎样才能做到这一点:当用户选择一个新的日期相同的布局显示新检索到的数据刷新了新的旧的数据不以重新布局显示它们。我怎么能做到这一点?请帮我...

的Java code

 公共类跟踪扩展BaseActivity
  {
私人按钮date_btn;
私人的ImageButton left_btn;
私人的ImageButton right_btn;
私人ImageView的无数据;
私人TextView的ex_name;
私人TextView的ex_BCals;私人的LinearLayout excercises_LL;
私人的LinearLayout content_LL;
私人的LinearLayout说明;
私人的LinearLayout细节;
私人诠释的年,月,日;
私人双tot_excals_burned;
私人日历localCalendar;
私有静态最终诠释DATE_DIALOG_ID = 0;
私人布尔has_ex_details;
私人布尔has_meal_details = FALSE;
私人光标exercises_cursor;
公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    的setContentView(R.layout.tracker);
    date_btn =(按钮)findViewById(R.id.btn_date);
    date_btn.setText(FormatDate());
    date_btn.setOnClickListener(新View.OnClickListener(){        公共无效的onClick(视图v){
            localCalendar = Calendar.getInstance();
            年= localCalendar.get(1);
            一个月= localCalendar.get(2);
            天= localCalendar.get(5);
            的ShowDialog(DATE_DIALOG_ID);
        }
    });
    left_btn =(的ImageButton)findViewById(R.id.btn_left);
    left_btn.setOnClickListener(新View.OnClickListener(){        公共无效的onClick(视图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(新View.OnClickListener(){        公共无效的onClick(视图v){
            localCalendar.add(5,1);
            date_btn.setText(FormatDate(localCalendar,EEEE,D / MMM / YYYY));            RefreshExercisesData();
            RefreshNoDataImage();        }
    });
    细节=(的LinearLayout)findViewById(R.id.ll_details);
    注释=(的LinearLayout)findViewById(R.id.ll_notes);
    excercises_LL =(的LinearLayout)findViewById(R.id.ll_exercises);
    无数据=(ImageView的)findViewById(R.id.nodata_imgV);    RefreshExercisesData();
    RefreshNoDataImage();
}
私人字符串FormatDate()
{    localCalendar = Calendar.getInstance();
    返回新的SimpleDateFormat(EEEE,D / MMM / YYYY)格式(localCalendar.getTime());}私人字符串FormatDate(INT年,月整型,诠释天)
{
    localCalendar = Calendar.getInstance();
    localCalendar.set(年,月,日);    返回新的SimpleDateFormat(EEEE,D / MMM / YYYY)格式(localCalendar.getTime());
}私人字符串FormatDate(挂历,字符串格式)
{    返回新的SimpleDateFormat(格式).format(calendar.getTime());}
私人无效RefreshExercisesData()
{    tot_excals_burned = 0;    DBAdapter DB =新DBAdapter(本);
    db.open();
    。字符串selected_date = date_btn.getText()的toString();
    Log.e(DATE,selected_date);
    exercises_cursor = db.getExerciseInfo(selected_date);    如果(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布局=(的LinearLayout)exc_LayoutInflater.inflate(R.layout.tracker_header_item,NULL);
        TextView的tot_ex_cals_value =((的TextView)(布局).findViewById(R.id.tv_tot_cals_value));
        TextView的exs_title =((的TextView)(布局).findViewById(R.id.tv_item_title));
        exs_title.setText(练习);        (布局).setPadding(0,36,0,0);
        excercises_LL.addView((视图)的布局,0);
        INT I = 1;        如果(exercises_cursor.moveToFirst())
        {
            做
            {
                content_LL =新的LinearLayout(本);
                ex_name =新的TextView(本);
                ex_name.setText(exercises_cursor.getFloat(1)+,+ exercises_cursor.getString(0)+分);
                ex_name.setTextColor(R.color.black);
                content_LL.addView(ex_name,0);                ex_BCals =新的TextView(本);
                ex_BCals.setText(圆形(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);
                我++;            }
            而(exercises_cursor.moveToNext());        }
        tot_ex_cals_value.setText(圆形(tot_excals_burned));    }
    否则,如果(exercises_cursor.getCount()== 0 || tot_excals_burned == 0)
    {        has_ex_details = FALSE;
        RefreshNoDataImage();    }
    exercises_cursor.close();
    exercises_cursor.deactivate();
    db.close();}
私人无效RefreshNoDataImage()
{
    如果(has_ex_details ==假放;&安培; has_​​meal_details ==假)
    {        notes.setVisibility(8);
        excercises_LL.setVisibility(8);
        nodata.setImageResource(R.drawable.bg_nodata);
        nodata.setVisibility(View.VISIBLE);
    }    其他
        nodata.setVisibility(8);
}
保护对话框onCreateDialog(INT ID)
{
    开关(ID){    案例DATE_DIALOG_ID:
        返回新DatePickerDialog(这一点,mDateSetListener,this.year,this.month,this.day);
    }
    返回null;
}私人DatePickerDialog.OnDateSetListener mDateSetListener =新DatePickerDialog.OnDateSetListener()
{
    公共无效onDateSet(DatePicker的paramDatePicker,年整型,诠释monthofYear,诠释DAYOFMONTH)
    {
        Tracker.this.year =年;
        月= monthofYear;
        天= DAYOFMONTH;
        date_btn.setText(FormatDate(年,月,日));
        RefreshExercisesData();
        RefreshNoDataImage();    }};
私人字符串圆(双NUM){
    返回的String.format(%1F%N,NUM);}}


解决方案

它,因为你定义这些变量为静态:

 公共静态INT图标;
公共静态字符串data_text;
公共静态字符串文本;

由于这些变量只有一个实例是为该类的所有实例创建的结果。所以,当你创建一个新的简介每次,他们将覆盖新值。您需要删除静态从变量声明关键字:

 公众诠释图标;
公共字符串data_text;
公共字符串文本;

然后,你不能访问他们为静态的,所以你需要访问这样的:

 档案PLI =数据[位置]
 holder.imgIcon.setImageResource(pli.icon);
 holder.Datatx​​t.setText(pli.data_text);
 holder.txt.setText(pli.text);

看看这个,如果你想了解更多关于静态 http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

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

}}

解决方案

Its because you defined these variables as static:

public static int icon;
public static String data_text;
public static String text;

As a result only one instance of those variables are created for all instances of that class. So when you create a new Profile each time, they are overwritten with new values. You need to remove the static keyword from variable declarations:

public int icon;
public String data_text;
public String text;

Then you cannot access them as static so you need to access like this:

 Profile pli = data[position];
 holder.imgIcon.setImageResource(pli.icon);
 holder.Datatxt.setText(pli.data_text);
 holder.txt.setText(pli.text);

Check out this if you want to learn more about static: http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

这篇关于如何改变布局的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆