سؤال

مرحبا ، كنت أتساءل لماذا يتصرف تحديث نص الزر بشكل مختلف في تطبيقي.

أدعو جزء الذي يحصل على المعلومات ما يجب تحديث الزر مع ، أفعل واجهة رد الاتصال من جزء لتحديث متغير عالمي أن النشاط بتحديث نص الزر.

تكمن المشكلة في أن النشاط الذي يوجد فيه الزر لا يقوم بتحديث الزر لإظهار النص الجديد ولكن إذا قمت بذلك من خلال الجزء الذي يعمل فيه ، فإن إبطال الزر لا يعمل أو يفرض التحديث إذا قمت بذلك من خلال النشاط.

هنا هو النشاط الذي يدعو عند الضغط على الزر الذي ثم يدعو جزء الذي يظهر قائمة وعند النقر على خيار الزر يجب تغيير النص إلى كل ما كنت قد اختارها:

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

سؤالي هو ، لماذا يقوم بتحديث النص على واجهة المستخدم الرسومية (عند تشغيل التطبيق) من خلال الجزء ولكن ليس من خلال النشاط ، أي الطريقة ص1_زر.سيتكست (استجابة);?

تحرير الإجابة كما أوضح راغوناندان:المشكلة هي أنني لم أفهم ذلك onClick_testSite(View view) اكتمل حتى لو لم تنقر على أي شيء في مربع الحوار ، اعتقدت أنه انتظر مع استدعاء وظيفة chartType.show() لكي تعود ثم تنتقل إلى نهاية الوظيفة.

هل كانت مفيدة؟

المحلول

تحتاج إلى تهيئة 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 كمتغير مثيل (على سبيل المثال قبل إنشاء).

تهيئة هذا

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

ربما يتم تحديث الزر حتى قبل تلقي الاستجابة في processFinish.ثم تقوم بتهيئة متغير الاستجابة.لذلك يتم تعيين نص الزر في المرة التالية التي تنقر فيها على الزر.

يمكنك الإعلان عن الزر قبل إنشائه وتحديثه في processFinish بدلا من ذلك هوون أعلاه.

نصائح أخرى

ال this.processFinish يحفظ القيمة ل إجابة.ولكن لا ينطبق على TextView مع btn_pickChart بطاقة تعريف.

لذلك، تحتاج فقط إلى حفظ مثيل TextView للنشاط:

private Button mP1Button;

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

وتطبيق القيمة المتغيرة متى this.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);
}

بالمناسبة، لا حاجة للاتصال بالرقم يبطل الطريقة بعد نص مجموعة لأنه تم استدعاؤه بالفعل من هنا.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top