使用Loader在AlertDialog中显示数据?

时间:2015-06-04 13:23:31

标签: android sqlite alertdialog loader

我正在寻找一种方法来显示一些使用AlertDialog中的SQLite DB中的加载器检索的数据。单击ListView中的名称会将DB条目ID传输到DialogFragment,并且基于此,应加载其他数据。 这样的加载确实有效但似乎在显示AlertDialog之后发生,因为消息框仅将Null显示为值。有人知道是否有可能将数据传输到这样的AlertDialog?谢谢你的帮助!

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;


@SuppressLint("SimpleDateFormat") 
public class BirthdayDetailsMessage extends DialogFragment implements
LoaderCallbacks<Cursor> {

    static String DATE_FORMAT;
    static final String CALENDAR = "calendar";

    private long mRowId;
    private String mName;
    private String mDate;
    private boolean mYearKnown;
    private String mAge;
    private Calendar mCalendar;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mCalendar = Calendar.getInstance();        

        DATE_FORMAT = PrefDateFormat.getPrefDateFormat(getActivity());

        Bundle arguments = getArguments();

        mRowId = arguments.getLong(ReminderProvider.COLUMN_ROWID); 

        getLoaderManager().initLoader(0, null, this);  
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){   

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setMessage("Birthday: "+ mDate +"\n\n"+"Age: "+ mAge)
        .setTitle(mName)
        .setCancelable(false)
        .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                                            dialog.cancel();    
                    }               
                }); 

       return builder.create();
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(getActivity(), ContentUris.withAppendedId(
                ReminderProvider.CONTENT_URI, mRowId), null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor reminder) {

        if (reminder.getCount() == 0) {
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    ((OnFinishEditor) getActivity()).finishEditor();
                }
            });
            return;
        }

        mName = reminder.getString(reminder
                .getColumnIndexOrThrow(ReminderProvider.COLUMN_NAME));

        mYearKnown = (reminder.getInt(reminder
                .getColumnIndexOrThrow(ReminderProvider.COLUMN_YEAR))==0? false:true);

        Long dateInMillis = reminder.getLong(reminder
                .getColumnIndexOrThrow(ReminderProvider.COLUMN_DATE));
        Date date = new Date(dateInMillis);
        mCalendar.setTime(date); 

        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        mDate = dateFormat.format(mCalendar.getTime());

        if (mYearKnown == true){
            mAge = String.valueOf(GetDiffYears.getDiffYears(mCalendar));
        }else{
            mAge = "age not known";
        }        

    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        // nothing to reset for this fragment
    }   

}

编辑:

我删除了onCreateDialog方法并改为使用以下视图

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.dialog_fragment, container,
            false);
    getDialog().setTitle("Name");   

    Date = (TextView) v.findViewById(R.id.date);
    Age = (TextView) v.findViewById(R.id.age);


    getLoaderManager().initLoader(0, null, this);

    return v;
}

private void updateValues() {
    getDialog().setTitle(mName);
    Date.setText("Geburtstag: "+ mDate);
    Age.setText("Alter: " + mAge);      
}

在onLoadFinished中包含updateValues(),并为消息体创建了一个布局XML ......这样它似乎正常工作。

1 个答案:

答案 0 :(得分:0)

您要做的是使用Loader中的回调方法来创建对话框。我不会将Loader放在对话框类中,而是放在调用类中或作为单独的类。然后在onLoadFinished中调用/创建对话框。实现与AsyncTask类似。

或者,您可以使用某些默认文本创建对话框,并在您的调用类中保留对它的引用。然后在onLoadFinished更新对话框的值。我很确定一个可行,但我还没有测试过。