2012-06-13 79 views
-1

在我的Android應用程序中,我有一個跟蹤器活動,在該活動中,我根據所選日期從sqlite數據庫中檢索練習信息(名稱,句號,燃燒的卡路里),並以線性佈局顯示這些信息,用戶選擇新的日期檢索到的數據顯示在另一個「新」佈局顯示在舊的佈局上方,但實際上我想要做的是在同一個佈局上顯示新的檢索到的數據「用新檢索的數據更改佈局內容」 ,我試圖刪除所有視圖的方法,但它沒有工作,因爲數據出現幾分鐘,然後DIS出現如何更改佈局的內容?

我怎麼能做到這一點:當用戶選擇一個新的日期新的檢索數據顯示在同一個佈局「通過新的數據刷新舊數據」而不是以新的佈局顯示它們。我該怎麼做?請幫助我..

Java代碼

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

}} 

回答

0

看起來你需要編輯你的問題,如果你讓我們看到任何代碼示例。

如果這只是一個練習的幾個文本,它應該足以在佈局xml中給出這些視圖ID,以便它們可以在您的活動中被引用。

然後,您可以在OnCreate中使用findViewById獲取您的視圖,並且當您收到新練習的數據時,您可以使用例如TextView.setText()。

+0

謝謝,我已經嘗試了你在我發佈我的問題之前所說的話,並嘗試這種方法「在活動中創建文本視圖」,問題仍然存在。 你知道另一種方法嗎? – Roba

0

如果你有一個佈局,例如,一個這樣的:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/name_textview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:id="@+id/calories_burned_textview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 
</RelativeLayout> 

然後在你的活動,你必須從你的SQLite數據庫訪問你的數據,你可以在這個佈局中修改現有textviews通過尋找該視圖和使用setText方法。

// load new data occurs above, now want to set 
TextView name = (TextView)findViewById(R.id.name_textview); 
name.setText(newName); 

TextView calsBurned = (TextView)findViewById(R.id.calories_burned_textview); 
calsBurned.setText(newCalsBurned); 

這是可能的,從它聽起來像你的描述,你是通過代碼的每一部分負荷按鈕被點擊時添加這些textviews的活動。您可以執行此操作,只需保留對您添加的這些文本視圖的引用,並且稍後在加載新條目時使用setText()(但不要第二次創建文本視圖)。

+0

我有類似的問題,我用同樣的方法,你說,但問題仍然是數據出現幾分鐘,然後它的dis出現 – user

+0

除去removeAllViews()方法它將在這種情況下使用? – user