2014-01-09 31 views
1

嗨我想知道爲什麼在我的應用程序中更新按鈕的文本行爲有所不同。更新按鈕上的文字

我調用一個片段來獲取信息,該按鈕應該更新,我從片段中進行接口回調以更新活動更新按鈕文本的全局變量。

問題是,按鈕所在的活動不刷新按鈕來顯示新的文本,但如果我通過它的工作片段來做到這一點,使按鈕無效或強制刷新,如果我做它通過活動。

下面是當你按下按鈕誰然後調用誰顯示一個列表,當你點擊一個選項按鈕應該改變它的文本,無論你已選用片段調用活動:

public void onClick_testSite(View view) 
{ 
    // create the fragment 
    SelectChartTypeDialogFragment chartType = new SelectChartTypeDialogFragment(); 
    // register you as a delegate for the callback 
    chartType.delegate = this; 
    // show the list 
    chartType.show(getFragmentManager(), "WEE"); 

    // fetch the button and set the text ** DOESNT WORK ** 
    Button p1_button = (Button)findViewById(R.id.btn_pickChart); 
    p1_button.setText(response); 
    p1_button.invalidate(); 


} 

@Override 
public void processFinish(String response) 
{ 
    this.response = response; 
} 

這裏是處理對話框中的片段的一部分:

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) 
{ 
    // get the list from a enum 
    final List<ChartTypes> chartList = Arrays.asList(ChartTypes.values()); 
    // The array containing the different choices 
    ArrayAdapter<ChartTypes> adapter = new ArrayAdapter<ChartTypes>(
      getActivity(), android.R.layout.simple_list_item_1, chartList); 
    // Use the Builder class for convenient dialog construction 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

    // The dialog setting 
    builder.setTitle(R.string.pick_chart); 
    builder.setAdapter(adapter, new DialogInterface.OnClickListener() 
    { 
     public void onClick(DialogInterface dialog, int position) 
     { 
      // get the name of the enum based on the position, what one 
      // clicked on the dialog 
      String strName = chartList.get(position).name(); 

      // this sets the text in the activitys global variable 
      delegate.processFinish(strName); 
      // set the text in the fragment instead 
      //changeBtnText(strName); 

      //dialog.dismiss(); 
     } 
    }); 

    // Create the AlertDialog object and return it 
    return builder.create(); 
} 

public void changeBtnText(String newBtnxt) 
{ 
    Button button = (Button)getActivity().findViewById(R.id.btn_pickChart); 
    button.setText(newBtnxt); 
} 

我的問題是,爲什麼它更新GUI上的文本通過碎片而不是通過活動(運行應用程序時),即方法p1_button.setText(響應);?通過Raghunandan作爲解釋

編輯答案: 問題是,我不明白,onClick_testSite(View view)完成,即使你沒有點擊該對話框上的任何東西,我認爲這與chartType.show()函數調用等待它返回,然後繼續執行該功能的結尾。

+0

你可以顯示片段代碼已滿嗎? – Raghunandan

+0

那裏我已更新代碼 – Kkloe

+0

好的檢查我的編輯。它應該工作。我希望你已經有一個名爲AsyncRespone的接口。 – Raghunandan

回答

1

需要初始化public AsyncResponse delegate = null;

delegate =(AsyncResponse) getActivity(); 

我相信這是實現的方法

@Override 
public void processFinish(String response) 
{ 
    Log.i(".........",response); // check if the response is logged 
    // if you get the response your button text will be changed 
    // else you need to look at why the response is not logged. 
    p1_button.setText(response); 
} 

聲明Button p1_button爲實例變量(例如之前的onCreate)。

初始化響應在processFinish接收甚至在此之前

p1_button = (Button)findViewById(R.id.btn_pickChart); // in onCreate 

大概按鈕被刷新。然後你初始化響應變量。所以下次點擊按鈕時會設置按鈕文字。

您可以在onCreate之前聲明按鈕,並將其更新爲processFinish而不是上面的hwon。

+0

發送到活動,你可以在上面的代碼中看到: chartType.delegate =這一點; 代碼工作,它僅僅是GUI元素不刷新,直到我再次按下按鈕 – Kkloe

+0

好,它的工作原理,如果我做的迴應,我已經更新了我的問題你要問爲什麼它不正確地更新它(在當我從點擊事件中執行它時運行gui)。 – Kkloe

+0

@Kkloe你不要再按下按鈕。按鈕點擊時刷新按鈕。每次你點擊你顯示一個對話框片段。該響應僅在'processFinish'中獲得。如果調試,你可以瞭解自己的流量是 – Raghunandan

1

processFinish將值保存爲響應。但它不適用於帶有btn_pickChart id的TextView。

所以,你只需要保存的TextView的實例活動:

private Button mP1Button; 

protected void onCreate(Bundle savedInstanceState) { 
... 
    Button mP1Button = (Button) findViewById(R.id.btn_pickChart); 
... 
} 

並申請更改值時processFinish被稱爲:

public void processFinish(String response) { 
    this.response = response; 
    // The value will be setup to the view element. 
    mP1Button.setText(response); 
} 

爲了獲得最佳的方式不使用委託作爲字段。您可以使用getActivity()或getTargetFragment()來檢查instanceOf AsyncResponse並調用processFinish方法。

AsyncResponse asyncResponse = null; 
if(getActivity() instanceof AsyncResponse) { 
    asyncResponse = (AsyncResponse) getActivity(); 
} else if (getTargetFragment() instanceof AsyncResponse){ 
    asyncResponse = (AsyncResponse) getTargetFragment(); 
} 

if(asyncResponse != null){ 
    asyncResponse.processFinish(strName); 
} 

順便說一句,無需調用無效方法之後的setText,因爲它已經從這裏調用。

+0

是的,我看到一個人不應該使用的是公共領域的更新,就是我也不願意isntanceof使用,我知道什麼時候是必要的,你應該使用它,我不覺得這個問題是使用它的那種。 – Kkloe