如何从另一个片段中的某些更新字符串值更新AlertDialog中的TextView

时间:2014-01-21 05:10:42

标签: android android-fragments sharedpreferences textview alertdialog

我有一个实现传感器事件监听器的片段。它计算传感器在onSensorChange方法的runnable中运行的时间段。当我按下活动中的按钮时,会出现一个警告对话框。我希望更新警报对话框中其中一个textview的字符串值以及传感器事件侦听器的运行时间。

我到目前为止尝试过sharedPrefs并且没有取得任何成功......值永远不会更新

这是实现侦听器的片段的部分代码:

private class mRecordDataRunnable implements Runnable {
    private SensorEvent event;
    public mRecordDataRunnable(SensorEvent _event) {
        this.event = _event;

    }

    @SuppressLint("DefaultLocale")
    @Override
    public void run() {
    mTotalTime = currentTime - pastTime;

    txTotalTime = String.format("%02d:%02d:%02d", 
                        TimeUnit.MILLISECONDS.toHours(mTotalTime),
                        TimeUnit.MILLISECONDS.toMinutes(mTotalTime) - 
                        TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(mTotalTime)),
                        TimeUnit.MILLISECONDS.toSeconds(mTotalTime) - 
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(mTotalTime)));   

    String timeAKey = "com.company.name.timea";

        SharedPreferences prefs = getActivity().getSharedPreferences("com.company.name", Context.MODE_PRIVATE);
        prefs.edit().putString(timeAKey,txTotalTime).apply();
     }
}

警报对话框的代码:

public class DialogFragmentAwake extends DialogFragment {

public interface NoticeDialogListener {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
}

NoticeDialogListener mListener;
View view;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();      
    builder.setView(inflater.inflate(R.layout.dialog_fragment_awake, null))
           .setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {

                   mListener.onDialogPositiveClick(DialogFragmentAwake.this);
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
                   mListener.onDialogNegativeClick(DialogFragmentAwake.this);
               }
           })
           .setTitle(R.string.progress_entry);


    SharedPreferences prefs = getActivity().getSharedPreferences("com.company.name", Context.MODE_PRIVATE);
    String timeAKey = "com.company.name.timea";

    view = inflater.inflate(R.layout.dialog_fragment_awake, null);

    String newtime= prefs.getString(timeAKey, "hour" );
    TextView txTimeA = (TextView) view.findViewById(R.id.time_amount);
    txTimeA.setText(newtime);




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

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try {
        // Instantiate the NoticeDialogListener so we can send events to the host
        mListener = (NoticeDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString()
                + " must implement NoticeDialogListener");
    }
}
}

警报对话框的布局(dialog_fragment_awake.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:cacheColorHint="#00000000" >
   <TextView 
       android:id="@+id/time_amount"
   android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginLeft="2dp"   
    />
</RelativeLayout>

修改

这是我写入共享偏好文件的新代码:

            String timeAKey = "com.company.name.timea";

        SharedPreferences prefs = getActivity().getSharedPreferences("com.company.name", Context.MODE_PRIVATE);
        prefs.edit().putString(timeAKey,txTotalTime).apply();

我已经检查了共享的prefs文件并正确地正确地写了时间......

以下是我从sharedprefs文件中读取并更新textview的代码:

    SharedPreferences prefs = getActivity().getSharedPreferences("com.company.name", Context.MODE_PRIVATE);
    String timeAKey = "com.company.name.timea";

    view = inflater.inflate(R.layout.dialog_fragment_awake, null);

    String newtime= prefs.getString(timeAKey, "hour" );
    TextView txTimeA = (TextView) view.findViewById(R.id.time_amount);
    txTimeA.setText(newtime);

它正确地读取了正确的时间...我使用systemout确保...但textview仍然没有更新

0 个答案:

没有答案