MapView Balloons to a DetailView

ぃ、小莉子 提交于 2019-12-13 01:00:03

问题


In my MapView there are shown Balloons like this:http://www.codemobiles.com/forum/code-mobile-topic-4180.html (it´s more or less the same code) What I want: If I press Balloon 1 it should open the Detail View with the Information of 1 and when I press the Ballon 2 it should open the Detail View with the informations of 2

I added this to the Itemized Overlay: c.startActivity(new Intent(c.getApplicationContext(), QuoteDetail.class));

So when I press Balloon 1 its open the DetailView with Information 1 But when I press Balloon 2 its also open the DetailView with Information 1 How can I change this?

Here is my code: QuoteDetail:

private ImageView mImageView;
private TextView mQuote;
private TextView mInfo;
private TextView mEmail;
private TextView mTel;
private TextView mWww;
private int mPosition;
private DataSource mDataSource;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.quote_detail);

    Intent i = getIntent();
    mPosition = i.getIntExtra("position", 0);

    mDataSource = new DataSource();
    mImageView = (ImageView) findViewById(R.detail.image);
    mQuote = (TextView) findViewById(R.detail.quote);
    mInfo = (TextView) findViewById(R.detail.info);
    mEmail = (TextView) findViewById(R.detail.email);
    mTel = (TextView) findViewById(R.detail.tel);
    mWww = (TextView) findViewById(R.detail.www);

    mImageView.setImageResource(mDataSource.getmPhotoHdPool().get(mPosition));
    mQuote.setText(getResources().getString(mDataSource.getmQuotePool().get(mPosition)));
    mInfo.setText(getResources().getString(mDataSource.getmInfoPool().get(mPosition)));
    mEmail.setText(getResources().getString(mDataSource.getmEmailPool().get(mPosition)));
    mTel.setText(getResources().getString(mDataSource.getmTelPool().get(mPosition)));
    mWww.setText(getResources().getString(mDataSource.getmWwwPool().get(mPosition)));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;  }





    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_me:  startActivity(new Intent(this, AboutmeActivity.class));;
                                break;

            case R.id.menu_settings:  startActivity(new Intent(this, SettingsActivity.class));;
            break;
        }
        return true;  }

}

QuoteReaderActvity:

public class QuoteAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflator;
    private DataSource mDataSource;

    public QuoteAdapter(Context c) {
        mContext = c;
        mInflator = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mDataSource = new DataSource();
    }


    public int getCount() {
        return mDataSource.getDataSourceLength();
    }


    public Object getItem(int position) {
        return position;
    }


    public long getItemId(int position) {
        return position;
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView thumbnail;
        TextView quote;

        if(convertView == null) {
            convertView = mInflator.inflate(R.layout.list_item_layout, parent, false);
        }

        thumbnail = (ImageView) convertView.findViewById(R.list.thumb);
        thumbnail.setImageResource(mDataSource.getmPhotoPool().get(position));

        quote = (TextView) convertView.findViewById(R.list.text);
        quote.setText(mDataSource.getmQuotePool().get(position));
        return convertView;
    }
}

// Rest of QuoteReaderActivty class...

private ListView mListView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mListView = (ListView) findViewById(R.id.quotes_list);
    mListView.setAdapter(new QuoteAdapter(this));

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            Intent i = new Intent(QuoteReaderActivity.this, QuoteDetail.class);
            i.putExtra("position", position);
            startActivity(i);
        }


    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;  }


    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_me:  startActivity(new Intent(this, AboutmeActivity.class));;
                                break;

            case R.id.menu_settings:  startActivity(new Intent(this, SettingsActivity.class));;
            break;
        }
        return true;  }

}

DateSource:

public ArrayList<Integer> getmPhotoHdPool() {
    return mDetail;
}

public ArrayList<Integer> getmWwwPool() {
    return mWww;
}

public ArrayList<Integer> getmTelPool() {
    return mTel;
}

public ArrayList<Integer> getmInfoPool() {
    return mInfo;
}

public ArrayList<Integer> getmEmailPool() {
    return mEmail;
}
public ArrayList<Integer> getmPhotoPool() {
    return mList;
}

public ArrayList<Integer> getmQuotePool() {
    return mText;
}

private void setupPhotoPool() {
    mList.add(R.drawable.tcs_1);
    mList.add(R.drawable.tcs_1);
    mList.add(R.drawable.tcs_1);
    mList.add(R.drawable.tcs_1);
    mList.add(R.drawable.tcs_1);
    mList.add(R.drawable.tcs_1);
    mList.add(R.drawable.tcs_1);
    mList.add(R.drawable.tcs_1);
    mList.add(R.drawable.tcs_1);
    mList.add(R.drawable.tcs_1);
    mList.add(R.drawable.tcs_1);

}

private void setupQuotePool() {
    mText.add(R.string.quote_1);
    mText.add(R.string.quote_2);
    mText.add(R.string.quote_3);
    mText.add(R.string.quote_4);
    mText.add(R.string.quote_5);
    mText.add(R.string.quote_6);
    mText.add(R.string.quote_7);
    mText.add(R.string.quote_8);
    mText.add(R.string.quote_9);
    mText.add(R.string.quote_10);
    mText.add(R.string.quote_001);


}

private void setupEmailPool() {
    mEmail.add(R.string.email_1);
    mEmail.add(R.string.email_2);
    mEmail.add(R.string.email_3);
    mEmail.add(R.string.email_4);
    mEmail.add(R.string.email_5);
    mEmail.add(R.string.email_6);
    mEmail.add(R.string.email_7);
    mEmail.add(R.string.email_8);
    mEmail.add(R.string.email_9);
    mEmail.add(R.string.email_10);
    mEmail.add(R.string.email_001);


}

private void setupTelPool() {
    mTel.add(R.string.tel_1);
    mTel.add(R.string.tel_2);
    mTel.add(R.string.tel_3);
    mTel.add(R.string.tel_4);
    mTel.add(R.string.tel_5);
    mTel.add(R.string.tel_6);
    mTel.add(R.string.tel_7);
    mTel.add(R.string.tel_8);
    mTel.add(R.string.tel_9);
    mTel.add(R.string.tel_10);
    mTel.add(R.string.tel_001);


}

private void setupWwwPool() {
    mWww.add(R.string.www_1);
    mWww.add(R.string.www_2);
    mWww.add(R.string.www_3);
    mWww.add(R.string.www_4);
    mWww.add(R.string.www_5);
    mWww.add(R.string.www_6);
    mWww.add(R.string.www_7);
    mWww.add(R.string.www_8);
    mWww.add(R.string.www_9);
    mWww.add(R.string.www_10);
    mWww.add(R.string.www_001);


}
private void setupPhotoHDPool() {
    mDetail.add(R.drawable.caming_hd_1);
    mDetail.add(R.drawable.caming_hd_1);
    mDetail.add(R.drawable.camping_hd_1);
    mDetail.add(R.drawable.camping_hd_1);
    mDetail.add(R.drawable.camping_hd_1);
    mDetail.add(R.drawable.camping_hd_1);
    mDetail.add(R.drawable.camping_hd_1);
    mDetail.add(R.drawable.camping_hd_1);
    mDetail.add(R.drawable.camping_hd_1);
    mDetail.add(R.drawable.camping_hd_1);
    mDetail.add(R.drawable.camping_hd_1);
    mDetail.add(R.drawable.camping_hd_1);}


       private void setupInfoPool() {
            mInfo.add(R.string.detail_1);
            mInfo.add(R.string.detail_2);
            mInfo.add(R.string.detail_3);
            mInfo.add(R.string.detail_4);
            mInfo.add(R.string.detail_5);
            mInfo.add(R.string.detail_6);
            mInfo.add(R.string.detail_7);
            mInfo.add(R.string.detail_8);
            mInfo.add(R.string.detail_9);
            mInfo.add(R.string.detail_10);
            mInfo.add(R.string.detail_001);





}

public int getDataSourceLength() {
    return mList.size();
}

public DataSource() {
    mList = new ArrayList<Integer>();
    mText = new ArrayList<Integer>();
    mInfo = new ArrayList<Integer>();
    mDetail = new ArrayList<Integer>();
    mEmail = new ArrayList<Integer>();
    mTel = new ArrayList<Integer>();
    mWww = new ArrayList<Integer>();
    setupPhotoPool();
    setupQuotePool();
    setupInfoPool();
    setupPhotoHDPool();
    setupEmailPool();
    setupTelPool();
    setupWwwPool();




}

}


回答1:


I am also use this link, http://www.codemobiles.com/forum/code-mobile-topic-4180.html some codes are added,

MyMap.java class

public class MyMap extends MapActivity {

MapView mapView;
List<Overlay> mapOverlays;
Drawable drawable;
Drawable drawable2;
MyItemizedOverlay itemizedOverlay;
//MyItemizedOverlay itemizedOverlay2;

@Override
public void onCreate(Bundle savedInstanceState) {

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

    mapView = (MapView) findViewById(R.id.mapview);
    mapView.setBuiltInZoomControls(true);

    mapOverlays = mapView.getOverlays();

    // first overlay
    drawable = getResources().getDrawable(R.drawable.marker);
    itemizedOverlay = new MyItemizedOverlay(drawable, mapView);

    GeoPoint point = new GeoPoint((int) (51.5174723 * 1E6),
            (int) (-0.0899537 * 1E6));
    OverlayItem overlayItem = new OverlayItem(point, "Umar Shafique",
            "(here goes 1)");
    itemizedOverlay.addOverlay(overlayItem);

    GeoPoint point2 = new GeoPoint((int) (51.515259 * 1E6),
            (int) (-0.086623 * 1E6));
    OverlayItem overlayItem2 = new OverlayItem(point2, "Abdul Karim",
            "here goes 2");
    itemizedOverlay.addOverlay(overlayItem2);

    mapOverlays.add(itemizedOverlay);

    // second overlay
/*  drawable2 = getResources().getDrawable(R.drawable.marker2);
    itemizedOverlay2 = new MyItemizedOverlay(drawable2, mapView);

    GeoPoint point3 = new GeoPoint((int) (51.513329 * 1E6),
            (int) (-0.08896 * 1E6));
    OverlayItem overlayItem3 = new OverlayItem(point3, "Arslan Ilyas",
            "here goes 3");
    itemizedOverlay2.addOverlay(overlayItem3);

    GeoPoint point4 = new GeoPoint((int) (51.51738 * 1E6),
            (int) (-0.08186 * 1E6));
    OverlayItem overlayItem4 = new OverlayItem(point4, "Ahsan",
            "here goes 4");
    itemizedOverlay2.addOverlay(overlayItem4);

    mapOverlays.add(itemizedOverlay2);*/

    final MapController mc = mapView.getController();
    mc.animateTo(point2);
    mc.setZoom(16);

}

@Override
protected boolean isRouteDisplayed() {
    return false;
}

}

Here I comment some code, because here add two itemizedOverlay , so one is comment.

and MyItemizedOverlay.java class

public class MyItemizedOverlay extends BalloonItemizedOverlay<OverlayItem> {

private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
private Context c;

public MyItemizedOverlay(Drawable defaultMarker, MapView mapView) {
    super(boundCenter(defaultMarker), mapView);
    c = mapView.getContext();
}

public void addOverlay(OverlayItem overlay) {
    m_overlays.add(overlay);
    populate();
}

@Override
protected OverlayItem createItem(int i) {
    return m_overlays.get(i);
}

@Override
public int size() {
    return m_overlays.size();
}

@Override
protected boolean onBalloonTap(int index) {
    Toast.makeText(c, "onBalloonTap for overlay index " + index,
            Toast.LENGTH_LONG).show();

    if (index == 0) {
        c.startActivity(new Intent(c.getApplicationContext(),
                NewActivity.class));
    } else {
         c.startActivity(new Intent(c.getApplicationContext(),
         secondNewActivity.class));
    }
    return true;
}

}

Here modification code

  if (index == 0) {
        c.startActivity(new Intent(c.getApplicationContext(),
                NewActivity.class));
    } else {
         c.startActivity(new Intent(c.getApplicationContext(),
         secondNewActivity.class));
    }
    return true;
}

press balloon two , we can change index position. we can pass index position through your intent.

provide your code , that only Easy the Explain.




回答2:


In the Itemized Overlay onClick handler, add an intentExtra so you can pass identifying information to your QuoteDetail.class. More than likely you'll want to extend the ItemizedOverlay class with your own class, add member variable(s) that you can use to identify the item. Then simply access these member variables and pass them to QuoteDetail as intentExtras.



来源:https://stackoverflow.com/questions/11782102/mapview-balloons-to-a-detailview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!