android - 如何通过将值传递给 AlertDialog 的 Intent 来转到下一个 Activity?

标签 android android-intent android-alertdialog

我想回到 Place.class 显示一个地方的完整信息..现在我可以回到 Place Activity 但信息只显示titleaddress.. 我认为这可能发生在onTab 方法中。它只有 2 件事被用于 Intent ......

intent.putExtra(Constants.COL_TITLE, oi.getTitle());
intent.putExtra(Constants.COL_ADDRESS, oi.getSnippet());

如果我想放入其他字段,即 content phone 等。如何将数据库中的数据放入此 Intent ?

intent.putExtra(Constants.COL_CON, ..........);

谢谢你的帮助

我这里有3个类(class)

xxx.class

public class xxx extends MapActivity {

    MapView mapView;
    MapController mapController;
    private static MyDB mDbHelper;
    private Cursor c;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aboutcm);

        mDbHelper = new MyDB(this);
        mDbHelper.createDatabase();
        mDbHelper.open();
        c = mDbHelper.getAttraction();

        mapView = (MapView) findViewById(R.id.mapview);
        mapController = mapView.getController();
        Drawable drawable = this.getResources().getDrawable(R.drawable.map_pin_3);
        List<Overlay> mapOverlays = mapView.getOverlays();
        PlaceItemizedOverlay itemizedoverlay = new PlaceItemizedOverlay(drawable, this);


        mapController.setZoom(13);
        mapView.setBuiltInZoomControls(true);


        c.moveToFirst(); 
        do {

            String title = c.getString(c
                    .getColumnIndex(Constants.COL_TITLE));
            String address = c.getString(c
                    .getColumnIndex(Constants.COL_ADDRESS));
            String content = c.getString(c
                    .getColumnIndex(Constants.COL_CONTENT));
            int latitude = (int) (c.getDouble(c
                    .getColumnIndex(Constants.COL_LA)) * 1E6);
            int longitude = (int) (c.getDouble(c
                    .getColumnIndex(Constants.COL_LONG)) * 1E6);


            itemizedoverlay.addOverlay(new OverlayItem(new GeoPoint(latitude, longitude), title,
                    address));
            mapOverlays.add(itemizedoverlay);

        } while (c.moveToNext());
        mDbHelper.close();

    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

}

PlaceItemizedOverlay.class

public class PlaceItemizedOverlay extends ItemizedOverlay<OverlayItem> {

    private Context mContext;
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

    public PlaceItemizedOverlay(Drawable defaultMarker, Context context)
    {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }
    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return mOverlays.size();    }


    @Override
    protected boolean onTap(int index) {

      final OverlayItem oi = mOverlays.get(index);
      AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
      dialog.setTitle(oi.getTitle());
      dialog.setMessage(oi.getSnippet());
      dialog.setNegativeButton("Back", null);
      dialog.setPositiveButton("See More Detail", new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
              Intent intent = new Intent(mContext, Place.class);
                intent.putExtra(Constants.COL_TITLE, oi.getTitle());
                intent.putExtra(Constants.COL_ADDRESS, oi.getSnippet());
                mContext.startActivity(intent); 
      }});
      dialog.show();
      return true;

    }


}

最佳答案

假设您要将字符串“123456789”从 xxx Activity 传递到 Place.class。然后你会使用 intent.putExtra("content", "123456789") 然后在 Place.class intent.getStringExtra("content") 会返回 "123456789"

Could i declare.... Intent i = new Intent(??????, Place.class); } And what should i put into ??????

您可以做三件事中的一件。首先,我的首选方法是在类主体中声明:

Context context;

然后在onCreate(...)声明:

context = this;

然后你可以这样做:

Intent i = new Intent(context, Place.class);

其次,您可以声明:

Intent i = new Intent(getApplicationContext(), Place.class);

第三,在你的类(class)主体中你可以声明:

Intent i;

然后在 onCreate(...) 中:

i = new Intent(this, Place.class);

使用第三种方法,您就可以在 alertDialog 中使用 startActivity(i)

关于android - 如何通过将值传递给 AlertDialog 的 Intent 来转到下一个 Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7297309/

相关文章:

android - 如何在 android 中使用不同的应用程序实现对我的应用程序的导航?

Flutter - 自动调整 AlertDialog 大小以适应列表内容

Android Studio - 解析 JSON 键

Android:以编程方式获取布局的宽度,在其 xml 中包含 fill_parent

android - 在 webrtc org.webrtc.SurfaceViewRenderer 中调用 SurfaceViewRenderer.pauseVideo() 后如何恢复视频

android - 将文件从内部存储附加到电子邮件

android - 警报消息未显示在警报对话框中?

android - 未显示警报对话标题和消息

android - 使用 Android map 进行车辆跟踪

android - 将大图像加载到android中的位图