【问题标题】:How to display a Dialog fragment inside location listener to trigger another activity如何在位置侦听器中显示对话框片段以触发另一个活动
【发布时间】:2015-04-12 16:30:22
【问题描述】:

我有一个应用程序位置侦听器,当用户到达列表中任意点的指定距离内时,它会弹出 toast 警报。我想调用一个对话框片段而不是 toast 弹出窗口,当用户选择“是”时,将用户转移到一个测验活动中,该活动根据他们的位置向他们提问。

简而言之,我希望能够在我的位置侦听器活动中显示此对话框片段,而不是 toast。到目前为止我已经尝试过

 DialogFragment dialog = new LocationDialog();  
                 showDialog(dialog);

代替 toast 警报,但我收到错误“对于 QLocationListener 类型的方法 showDialog(DialogFragment) 未定义。我一直在按照各种教程、指南和 Google 的 android 文档兜圈子,但无济于事,所以一些指导将不胜感激。

我的所有代码都作为独立应用程序运行我只是在努力通过对话框片段将位置侦听器 + 主要活动链接到测验。我还希望能够将位置信息打包传递给测验,以便它知道要显示哪些问题和答案,但这是另一天的任务......

主要活动

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.location.LocationManager;
import android.os.Bundle;

public class MainActivity extends Activity {

private static final long MINIMUM_DISTANCECHANGE_FOR_UPDATE = 1; 
private static final long MINIMUM_TIME_BETWEEN_UPDATE = 1000; 
private LocationManager locationManager;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    QLocationListener qLL = new QLocationListener();
    qLL.parentActivity = this;


    // create the hard-coded list of points of interest

    ArrayList<QuizContent> pointList = new ArrayList<QuizContent>();

    // ExampleA
    QuizContent MapPoint = new QuizContent(25,5, "example question?", "a", "b","c","d", "a");

    // ExampleB
    QuizContent MapPoint2 = new QuizContent(26,5, "example question?", "a", "b","c","d", "a");

    // ExampleC
    QuizContent MapPoint3 = new QuizContent(27,5, "example question?", "a", "b","c","d", "a");


    pointList.add(MapPoint);
    pointList.add(MapPoint2);
    pointList.add(MapPoint3);


    // now set up the location manager and listener
    qLL.pointList = pointList;
    locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 
                    MINIMUM_TIME_BETWEEN_UPDATE, 
                    MINIMUM_DISTANCECHANGE_FOR_UPDATE,
                    qLL
    );



}    
}

QLocation 监听器

import java.util.ArrayList;

import android.app.AlertDialog;
import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.widget.Toast;
import android.widget.TextView;


public class QLocationListener implements LocationListener {


public MainActivity parentActivity ;

// this is my list of quiz content (questions, answers, locations)
public ArrayList<QuizContent> pointList;


// this method is called when the location is changed
public void onLocationChanged(Location location) {

  // now measure distance from all locations in quiz list
    for (int i=0;i<pointList.size();i++){
        QuizContent gp = pointList.get(i);
        Location fixedLoc = new Location("one");

        Double lat = Double.valueOf(String.valueOf(gp.getLatitude()));
        Double lng = Double.valueOf(String.valueOf(gp.getLongitude()));

        fixedLoc.setLatitude(lat);
        fixedLoc.setLongitude(lng);
        Log.i("location",lat+" "+location.getLatitude());
        Log.i("location",lng+" "+location.getLongitude());

        // calculate distance
        float distance = location.distanceTo(fixedLoc);

            if (i == 0) { // this is location a

             if (distance < 10) {


                 DialogFragment dialog = new LocationDialog();  
                 showDialog(dialog);


             }
         }


         if (i == 1) { // this is location b

             if (distance < 10) {
                    Toast.makeText(parentActivity.getBaseContext(), 
                            "Welcome to location b", Toast.LENGTH_LONG).show();
             }
         }

         if (i == 3) { // this is location c

             if (distance < 10) {
                    Toast.makeText(parentActivity.getBaseContext(), 
                            "Welcome to location c", Toast.LENGTH_LONG).show();
             }
         }

    }

}
public void onProviderDisabled(String s) {
}
public void onProviderEnabled(String s) {            
}
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}
}

对话框片段

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class LocationDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
    alertDialog.setMessage("Quiz Location Found, answer the question?")
           .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   startActivity(new Intent(getActivity(), Quiz.class));
                   // take the quiz!
               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });


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

}

}

【问题讨论】:

    标签: android android-alertdialog android-dialogfragment locationlistener dialogfragment


    【解决方案1】:

    您需要在 Activity/App 上下文中调用 showDialog

    试试:

    来自活动:

     QLocationListener qLocationListener= new QLocationListener(this)
    

    在 QLocationListener 中:

     Activity act;
    
    
     QLocationListener(Activity a)
         {
              //Constructor
             act=a;
         }
    

    最后是你调用 showDialog 的地方:

      act.showDialog(dialog);
    

    【讨论】:

    • 感谢您的回复。我已经尝试了您的建议,现在我收到消息“方法 showDialog(DialogFragment) 未定义类型 Context”。似乎它只是在推动错误。
    • 我的错。它应该是活动。已编辑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多