【问题标题】:Start new Activity on MapOverlay Tapping在 MapOverlay Tapping 上启动新 Activity
【发布时间】:2011-12-17 17:24:52
【问题描述】:

我是 Android 新手。我正在构建一个具有 5 个选项卡的选项卡式应用程序。我使用不同的视图组,每个选项卡都有根据需要和子活动的可响应视图组。第一个选项卡是具有不同子活动的 TabGroupHome。TabGroupHome首先启动 GoogleMapActivity(具有overlayItems)作为其子活动。这些叠加层代表了不同的用户。我从一个 JSONArray 中获取这些用户的数据,该 JSONArray 由服务器上的一个 php 文件返回,该文件具有不同用户的数据。当我启动我的应用程序时,它的外观->

launching_view!

它的顶部有一个带有不同按钮的操作栏(例如,列表、配置文件和刷新)。当我单击列表时,它会显示所有用户的列表 ->

list of Users! & user_profile!!

& 当我点击 listItem 时,它会显示上面指出的相应用户的完整个人资料

我坚持的地方是-> ???我想在点击 MapPin 时显示此用户资料...

我的代码是->

public class GoogleMapActivity extends MapActivity implements ActionBar {

Button btnOnMapList, btnProfileHome, btnRefresh;
Intent intent;

private JSONArray jArray;
private JSONObject jFan_Data;
private ItemBean bean;
private FansData fansdata;//reference of FansData class that return me JSONArray 

HelloItemizedOverlay itemizedOverlay;//..........

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    btnProfileHome = (Button) findViewById(R.id.btn_profile_home);
    btnOnMapList = (Button) findViewById(R.id.btn_list_home);
    btnRefresh = (Button) findViewById(R.id.btn_refresh_home);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.googlemapactivity);
    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);
    mapView.displayZoomControls(true);
    // mapView.setSatellite(true);

    /**TG_1 -> Here i write code to get my current location(e.g., through LocationManager) 
     * after getting location, i write my location`s latitude & longitude into 
     * shearedPreference */

    //geading preference to get my unique id
    SharedPreferences myUid=GoogleMapActivity.this.getSharedPreferences("uid", MODE_WORLD_READABLE);
    String myId=myUid.getString("myId", "");
    Log.i("MyUid", myId);

/** calling FansData class to get all users data. Currently i am providing my hard codded location  but i have to get it from LocationManager */    
    fansdata=new FansData();
    jArray=fansdata.jFanDataArray(1000, 12.9716060, 77.5903760, "h9ow0");
    System.out.println(jArray.toString());


/** to showing Users on map as pins */
           List<Overlay> mapOverlays = mapView.getOverlays();
       Drawable drawable = this.getResources().getDrawable(
            R.drawable.small_football_icon);
HelloItemizedOverlay itemizedOverlay = new HelloItemizedOverlay(
            drawable, getParent());
    for(int i=0;i<jArray.length();i++){
        try {
            jFan_Data=jArray.getJSONObject(i);

              GeoPoint geoPoint = new GeoPoint((int) (jFan_Data.getDouble("lat")* 1E6),
            (int) (jFan_Data.getDouble("lang")* 1E6));

            OverlayItem overlayitem = new OverlayItem(geoPoint, jFan_Data.getString("name"),
            jFan_Data.getString("uniqid"));
            itemizedOverlay.addOverlay(overlayitem);

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    mapOverlays.add(itemizedOverlay);

}


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

@Override
public void onHomeList(View view) {
    Intent in = new Intent(getParent(), MapPinsList.class);
    TabGroupActivity prnt = (TabGroupActivity) getParent();
    prnt.startChildActivity("MapPinsList", in);

}

@Override
public void onHomeProfile(View view) {
    Intent in = new Intent(getParent(), ProfileActivity.class);
    TabGroupActivity prnt = (TabGroupActivity) getParent();
    prnt.startChildActivity("ProfileActivity", in);

}

@Override
public void onHomeRefresh(View view) {
    // TODO Auto-generated method stub

}

@Override
public void onListMap(View view) {
    // TODO Auto-generated method stub

}

@Override
public void onListProfile(View view) {
    // TODO Auto-generated method stub

}

}

我的 HelloItemizedOverlay 类是->

public class HelloItemizedOverlay extends ItemizedOverlay {

private static final int VIEW_PROFILE = 1;
private static final int SEND_MASSAGE = 2;
OverlayItem item;

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

Context mContext;

public HelloItemizedOverlay(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
}

public HelloItemizedOverlay(Drawable defaultMaker, Context context) {
    // super(defaultMaker);
    super(boundCenterBottom(defaultMaker));
    mContext = context;
}

@Override
public boolean onTap(int index) {

    // Option to select on clicking pin
    final String[] option = new String[] { "View Profile", "Send Massage",
            "Cancle" };

    // to hold option
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,
            android.R.layout.select_dialog_item, option);

    //OverlayItem item = mOverlays.get(index);
    item = mOverlays.get(index);
    AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

    builder.setTitle(item.getTitle());
    // builder.setMessage(item.getSnippet());

    builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int i) {
            // TODO Auto-generated method stub
            if (i == 0) {

                String fId=item.getSnippet();
        Toast.makeText(mContext, "Profile View is on Progress...!"+fId,
                        Toast.LENGTH_SHORT).show();

                Intent in = new Intent(mContext, FanProfile.class);
                Bundle fBundle= new Bundle();

                fBundle.putString("fanId", fId);
                in.putExtras(fBundle);
                              in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      mContext.getApplicationContext().startActivity(in);

            } else if (i == 1) {
                           Toast.makeText(mContext,"Send Massage View is on Progress...!",
                        Toast.LENGTH_SHORT).show();
            } else if (i == 2) {
                dialog.cancel();
            }
        }
    });

    builder.show();
    // AlertDialog dialog= builder.create();

    return true;
}

public void addOverlay(OverlayItem overlayItem) {
    // for(int i=0;i<=size();i++){
    mOverlays.add(overlayItem);
    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();
}
}

“onTap”中的 FanProfile 与我单击用户列表的 listItem 时调用的活动相同!

上面的代码可以工作,但它会在我的 tabView 上启动视图,它隐藏了我的选项卡...

[这个]http://i.stack.imgur.com/lgT2G.png

我没有得到我犯错或做错的地方。

你的建议对我很有价值!!!

我将非常感谢您在哪里获得该问题的解决方案的指针或示例代码...

【问题讨论】:

    标签: android google-maps android-intent itemizedoverlay viewgroup


    【解决方案1】:

    一种可能的解决方案是在 FanProfile 活动中添加与上一个活动相同的选项卡

    【讨论】:

    • 如果我在 FanProfile 上添加选项卡,那么当我单击不是所需视图的 listItem 时也会看到这些选项卡。我需要当我单击 Map 的引脚时,它会在其视图上向我显示 FanProfile,就像它当前所做的那样->它将在包含选项卡的“TabActivity”上显示用户的配置文件,并且每个选项卡都有相应的 ViewGroup .正如我提到的,GoogleMapActivity 是“TabGroupHome”的子活动,并且在 ItemizeOverLay 类中我传递了 GoogleMapActivity 的上下文。但是如果我传递 TabGroupHome 的上下文,它可能会做我需要的事情..但是我是怎么做的呢?
    【解决方案2】:

    经过长时间的研发,我得到了解决方案,当我点击 MapPin 时显示 FanProfile 的正确方法是

        @Override
            public void onClick(DialogInterface dialog, int i) {
                // TODO Auto-generated method stub
                if (i == 0) {
                    //getting the unique-id of fan, whose pin is clicked on map`s pins
                    String fId=item.getSnippet();
                    Toast.makeText(mContext, "Profile View is on Progress...!"+fId,
                            Toast.LENGTH_SHORT).show();
                    //getting the parent context(cast the context of GoogleMapActivity into its Parent e.g.,"TabGroupActivity")
                           /** i cast the mContext into its parent Activity`s context which extends ViewGroup */  
                    tga=(TabGroupActivity)mContext;
    
                    //1.intent that start my "FanProfile" Activity 
                    Intent in = new Intent(tga, FanProfile.class);
    
                    //2.Bundle that hold data which i want to send to "FanProfile" Activity. 
                    Bundle fBundle= new Bundle();
    
                    //3.putting values into bundle
                    fBundle.putString("fanId", fId);
    
                    //4.Binding the bundle with the intent
                    in.putExtras(fBundle);
    
                    //5.Starting intent 
                    tga.startChildActivity("FanProfile", in);
    
    
                } else if (i == 1) {
                    Toast.makeText(mContext,
                            "Send Massage View is on Progress...!",
                            Toast.LENGTH_SHORT).show();
                } else if (i == 2) {
                    dialog.cancel();
                }
            }
    

    这很好用,现在我很高兴我得到了需要 -> this! 坦克很多@Rajdeep Dua 为你的宝贵建议!!!我将再次感谢“StackOverFlow”和你们所有人并希望大家给予同样的合作和建议......

    StackOverFlow 真是太棒了 -> 太棒了 ->

    太棒了... :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 2013-05-18
      • 2012-09-06
      • 1970-01-01
      相关资源
      最近更新 更多