문제

안녕하세요, 버튼의 텍스트를 업데이트하는 이유는 내 앱에서 다른 작동을 어떻게 작동합니까?

버튼을 업데이트 해야하는 정보를 가져 오는 조각을 호출합니다. Fragment에서 인터페이스 콜백을 수행하여 활동이 단추 텍스트를 업데이트하는 전역 변수를 업데이트합니다.

버튼이있는 활동이 새 텍스트를 표시하기 위해 버튼을 새로 고침하지 않지만 Fragment가 작동하지 않으면 버튼이 무효화되지 않거나 해당하시면 새로 고침을 수행하지 않거나 해당하는 경우 새로 고침을 해제하지 않음 활동을 통해.

여기에 목록을 보여주는 단편을 호출하고 옵션을 클릭하면 단추를 클릭하면 단추를 누르면 단추를 누른 상태에서 전화를 걸 수 있습니다.

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

내 질문은 Fragment를 통해 GUI의 텍스트를 업데이트하지만 활동을 통과하지 않지만 i.e 메소드 (응답);?

Raghunandan에서 설명한대로 답변 편집 : 문제는 내가 대화 상자에서 아무 것도 클릭하지 않았다는 것을 이해하지 못했다는 것이 었습니다.


도움이 되었습니까?

해결책

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 이전)

를 초기화합니다
p1_button = (Button)findViewById(R.id.btn_pickChart); // in onCreate
.

아마도 processFinish에서 응답이 수신되기 전에 버튼이 새로 고쳐집니다.그런 다음 응답 변수를 초기화합니다.따라서 버튼 텍스트가 다음에 버튼을 클릭하면됩니다.

OnCreate 앞에 버튼을 선언하고 processFinish 위의 Hwon을 대상으로 업데이트 할 수 있습니다.

다른 팁

processFinish 은 값을 응답 에 저장합니다.그러나 btn_pickchart ID로 텍스트보기에 적용되지 않습니다.

그래서, 활동을 위해 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 ()를 사용하여 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 후에 invalidate 메소드를 호출 할 필요가 없습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top