How to addsong in User class using Playlist arrays? - java

addSong(title:String,filePath: String, artist:String):int
adds the song to the play list and returns 0 if added and –1 if the song can not be added because the list is full.
*** I keep on getting errors like method addsong(String, String, String) is not applicable for agrument int and also the getArtist method is not defined. How do I go upon it? Any suggestions or solutions to make it work?
PLaylist Class:
public class Playlist {
//Instance Variables
private int numOfSongs;
private Songs[] songList;
// Constructors
public Playlist(int maxNumofSongs){
this.numOfSongs = 0;
this.songList = new Songs[maxNumofSongs];
}
//Getters
public Songs[] getSongList(){
return songList;
}
//Methods
public void addSong(String title, String filePath, String artist){
Songs p = new Songs(title, filePath, artist);
addSong(p);
}
public void addSong(Songs p){
songList[this.numOfSongs] = p;
this.numOfSongs++;
}
public Songs getSong(int pos){
if (pos <= this.songList.length)
return this.songList[pos];
else
return null;
}
public int getSongByTitle(String title){
int pos = -1;
for (int i = 0; i < this.numOfSongs; i++)
if (this.songList[i].getTitle() == title)
pos = i;
return pos;
}
public String toString(){
String playlistDesc = "";
playlistDesc += "Number of Songs added in Playlist: "+ numOfSongs;
return playlistDesc;
}
}
User CLass:
public class User {
//Instance Variables
private String name;
private String email;
private Playlist favoriteSongs;
Songs[] songs = this.favoriteSongs.getSongList();
//Constructors
public User(String name, String email, Playlist favoriteSongs){
this.name = name;
this.email = email;
this.favoriteSongs = favoriteSongs;
}
public User(String name, String email){
this.name = name;
this.email = email;
}
//Setters
public void setPlayList(Playlist list){
this.favoriteSongs = list;
}
//Get song title by inputting the position in the playlist array
public String getSongTitle(int pos){
if (pos < songs.length){
Songs s = songs[pos];
//you can then get the title of the song using
s.getTitle();
}
return songs[pos].getTitle();
}
//Add new song to the playlist
public int addSong(String title, String filePath, String artist){
for(int i = 0; i < songs.length; i++) {
if (addSong(i) == songs[i].getTitle().getArtist()){ // or what ever you want to compare
return 0;
}
// if you do not found any thing
return -1;
}
}
//Counts how many songs with the same artist
public int artistSongCount(String artist){
int count = 0;
for (int i=0; i < songs.length; i++)
if (this.songs[i].getArtist() == artist)
count++;
return count;
}
//Print out details of user
public String toString(){
String userOutput = "";
userOutput += "Name: "+ name;
userOutput += "Email: "+ email;
return userOutput;
}
}

Related

ListView OnItemClickListener Song

I've created a ListView (myList). By pressing on one of the items on the ListView, the app is supposed to direct the user to the PlaySongActivity page.
I used a searchById function to try to match the ID of the song and the song in my database.( get ID of the song and match the song ID in database to play the same song) However, my teacher told me I am searching by the ID of the ListView, not the song.
So is there any way I can either search by the song title or possibly add an ID to each item in the ListView?
I'm a beginner in coding and have searched for hours and found no solution on the internet :(
private SongCollection mySongCollection = new SongCollection();
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(SearchSong.this, PlaySongActivity.class);
String resourceId = AppUtil.getResourceId(SearchSong.this, myList);
Song selectedSong = mySongCollection.searchById(resourceId);
AppUtil.popMessage(SearchSong.this, "Streaming music: " + selectedSong.getTitle());
intent.putExtra("id", selectedSong.getId());
intent.putExtra("title", selectedSong.getTitle());
intent.putExtra("artiste", selectedSong.getArtiste());
intent.putExtra("fileLink", selectedSong.getFileLink());
intent.putExtra("coverArt", selectedSong.getCoverArt());
startActivity(intent);
}
});
SongCollection.class codes
package com.example.musix;
public class SongCollection {
private Song[] allSongs = new Song[9];
public SongCollection (){
prepareSongs();
}
private void prepareSongs(){
Song theWayYouLookTonight = new Song ("S1001", "The Way You Look Tonight", "Michael Buble", "a5b8972e764025020625bbf9c1c2bbb06e394a60?cid=2afe87a64b0042dabf51f37318616965", 4.66, "michael_buble_collection");
Song billiejean = new Song ("S1002", "Billie Jean", "Michael Jackson", "4eb779428d40d579f14d12a9daf98fc66c7d0be4?cid=2afe87a64b0042dabf51f37318616965", 4.9, "billie_jean");
Song somethingJustLikeThis = new Song("S1003", "Something Just Like This","The Chainsmokers","499eefd42a24ec562c464bd7acfad7ed41eb9179?cid=2afe87a64b0042dabf51f37318616965", 4.13, "something_just_like_this");
Song southOfTheBorder = new Song("S1004", "South of the Border","Ed Sheeran","7b43dd0c94b0af0c0401381a683d2f4833180ba3?cid=2afe87a64b0042dabf51f37318616965", 3.41, "south_of_the_border");
Song oldTownRoad = new Song("S1005", "Old Town Road","Lil Nas X","3bc62106123fcafad475271e72e74cd7f519ab83?cid=2afe87a64b0042dabf51f37318616965", 1.9, "old_town_road");
Song noGuidance = new Song("S1006", "No Guidance", "Chris Brown", "7c3bc7b4d1741a001463b570fe29f922d9c42bd6?cid=2afe87a64b0042dabf51f37318616965", 4.34, "no_guidance");
Song closer = new Song("S1007", "Closer", "The Chainsmokers", "8d3df1c64907cb183bff5a127b1525b530992afb?cid=2afe87a64b0042dabf51f37318616965", 4.08, "closer");
Song sideface = new Song("S1008", "側臉", "于果", "c8cc891a7cacb36857ea15c8fcfc4da6e4b1583d?cid=2afe87a64b0042dabf51f37318616965", 3.63, "sideface");
Song kebukeyi = new Song("S1009", "可不可以", "张紫豪", "2d790215acf7c4e6c5e093255b94a936064f75ed?cid=2afe87a64b0042dabf51f37318616965", 4.01, "kebukeyi");
allSongs[0]= theWayYouLookTonight;
allSongs[1]= billiejean;
allSongs[2]= somethingJustLikeThis;
allSongs[3]= southOfTheBorder;
allSongs[4]= oldTownRoad;
allSongs[5]= noGuidance;
allSongs[6]= closer;
allSongs[7]= sideface;
allSongs[8]= kebukeyi;
}
public Song searchById (String id){
Song selectedSong = null;
for(int index=0; index<allSongs.length; index++){
selectedSong = allSongs[index];
if(selectedSong.getId().equals(id)){
return selectedSong;
}
}
return selectedSong;
}
//create a method to retrieve the next song
public Song getNextSong(String currentSongId){
Song nextSong = null;
for(int x = 0; x < allSongs.length; x++){
String tempSongId = allSongs[x].getId();
if(tempSongId.equals(currentSongId) && (x < allSongs.length -1)){
nextSong = allSongs[x+1];
break;
}
}
return nextSong;
}
//create a method to retrieve the previous song
public Song getPrevSong(String currentSongId){
Song PrevSong = null;
for(int x = 0; x < allSongs.length; x++){
String tempSongId = allSongs[x].getId();
if(tempSongId.equals(currentSongId) && (x > 0)){
PrevSong = allSongs[x-1];
break;
}
}
return PrevSong;
}
//create a method to get random song
public Song getRandomSong(){
Song randomSong = null;
int max = 2;
int min = 0;
int randomNum = (int)(Math.random()*4);
randomSong = allSongs[randomNum];
return randomSong;
}
}
Song.class codes
package com.example.musix;
public class Song {
//private attributes are hidden from other classes/files
private String id;
private String title;
private String artiste;
private String fileLink;
private double songLength;
private String coverArt;
public Song(String _id, String _title, String _artiste, String _fileLink, double _songLength, String _coverArt){
this.id = _id;
this.title = _title;
this.artiste = _artiste;
this.fileLink = _fileLink;
this.songLength = _songLength;
this.coverArt = _coverArt;
}
//encapsulation
//SET methods for setting/changing of the values of the attributes
public void setId(String id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setArtiste(String artiste) {
this.artiste = artiste;
}
public void setFileLink(String fileLink) {
this.fileLink = fileLink;
}
public void setSongLength(double songLength) {
this.songLength = songLength;
}
public void setCoverArt(String coverArt) {
this.coverArt = coverArt;
}
//GET methods allows us to retrieve values of the attributes
public String getId() {
return this.id;
}
public String getTitle() {
return this.title;
}
public String getArtiste() {
return this.artiste;
}
public String getFileLink() {
return this.fileLink;
}
public double getSongLength() {
return this.songLength;
}
public String getCoverArt() {
return this.coverArt;
}
}
codes for getResourceId
public final class AppUtil
{
public static void popMessage(Context context, String message)
{
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
public static String getResourceId(Context context, View view)
{
String id = context.getResources().getResourceEntryName(view.getId());
return id;
}
String resourceId = AppUtil.getResourceId(SearchSong.this, myList);
Song selectedSong = mySongCollection.searchById(resourceId);
resourceId is going to be the id of the element of the list view (eg. first element id = 0, 2nd id = 1 and so on).
public Song searchById (String id){
Song selectedSong = null;
for(int index=0; index<allSongs.length; index++){
selectedSong = allSongs[index];
if(selectedSong.getId().equals(id)){
return selectedSong;
}
}
return selectedSong;
}
Should be:
public Song searchById (String id){
//we are returning the song selected by the index of its Arrays
Song selectedSong = allSongs[Integer.parseInt(id)];
return selectedSong;
}
Why?:
Your returning the actual songid, but in
Song selectedSong = mySongCollection.searchById(resourceId); <-- resourceId is already the Id stored in the database and not the index of mySongCollection.
intent.putExtra("id", selectedSong.getId());
you are using already the actuals song id. This doesen't make sense as you can already identify the actual song.
So either apply these changes or change this line:
intent.putExtra("id", resourceId);

How to implement parcelable with my custom class containing Hashmap and SparseArray?

I need to implement parcelable in my custom class "ArtistInfo"
with the following structure:
public class ArtistInfo implements Parcelable {
private String artist;
// album name to list of ids of songs
private HashMap> albumInfo;
// song id to songInfo
private SparseArray songsMap;
protected ArtistInfo(Parcel in) {
artist = in.readString();
}
public static final Creator CREATOR = new Creator() {
#Override
public ArtistInfo createFromParcel(Parcel in) {
return new ArtistInfo(in);
}
#Override
public ArtistInfo[] newArray(int size) {
return new ArtistInfo[size];
}
};
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public void addSongsInfoToAlbum(List songsInfo, String album) {
if (albumInfo == null) {
albumInfo = new HashMap();
}
if (songsMap == null) {
songsMap = new SparseArray();
}
List songsIds = new ArrayList();
for (SongInfo songInfo : songsInfo) {
songsIds.add(songInfo.getId());
songsMap.put(songInfo.getId(), songInfo);
}
List songsIdsForAlbum = getSongIdsForAlbum(album);
songsIdsForAlbum.addAll(songsIds);
albumInfo.put(album, songsIdsForAlbum);
}
private List getSongIdsForAlbum(String album) {
if (albumInfo == null) {
return new ArrayList();
}
List songsIds = albumInfo.get(album);
return songsIds == null ? new ArrayList() : songsIds;
}
public HashMap> getAlbumInfo() {
return albumInfo;
}
public SparseArray getSongsMap() {
if (songsMap == null) {
songsMap = new SparseArray();
}
return songsMap;
}
#Override
public String toString() {
return "ArtistInfo{" +
"artist='" + artist + '\'' +
", albumInfo=" + albumInfo.toString() +
", songsMap=" + songsMap.toString() +
'}';
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(artist);
}
}
And following is the structure of the "SongInfo" class used in the above class:
public class SongInfo implements Parcelable {
private Integer id;
private String name;
private String url;
public SongInfo(Integer id, String name, String url) {
this.id = id;
this.name = name;
this.url = url;
}
protected SongInfo(Parcel in) {
if (in.readByte() == 0) {
id = null;
} else {
id = in.readInt();
}
name = in.readString();
url = in.readString();
}
public static final Creator CREATOR = new Creator() {
#Override
public SongInfo createFromParcel(Parcel in) {
return new SongInfo(in);
}
#Override
public SongInfo[] newArray(int size) {
return new SongInfo[size];
}
};
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
if (id == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeInt(id);
}
dest.writeString(name);
dest.writeString(url);
}
}
Now as you can see there is no problem in implementing the Parcelable interface in the SongInfo class, but I am not able to understand how to read and write the albumInfo and songsMap variables in the Constructor and writeToParcel method respectively. Can someone please help me understand how should I go ahead with that. Thanks!
The idea is iterate through each item in albumInfo and songsMap then add it into Parcelable.
Write to parcel.
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(artist);
// Write album info
dest.writeInt(albumInfo.size());
for (Map.Entry<String, List<Integer>> item : albumInfo.entrySet()) {
dest.writeString(item.getKey());
dest.writeList(item.getValue());
}
// Write song map
dest.writeInt(songsMap.size());
for (int i = 0; i < songsMap.size(); i++) {
int key = songsMap.keyAt(i);
dest.writeInt(key);
dest.writeParcelable(songsMap.get(key), flags);
}
}
Read from parcel
protected ArtistInfo(Parcel in) {
artist = in.readString();
// Read album info
albumInfo = new HashMap<>();
int albumInfoSize = in.readInt();
for (int i = 0; i < albumInfoSize; i++) {
String key = in.readString();
List<Integer> value = new ArrayList<>();
in.readList(value, null);
albumInfo.put(key, value);
}
// Read song map
songsMap = new SparseArray<>();
int songsMapSize = in.readInt();
for (int i = 0; i < songsMapSize; i++) {
int key = in.readInt();
SongInfo value = in.readParcelable(SongInfo.class.getClassLoader());
songsMap.put(key, value);
}
}

getParcelableArrayExtra null pointer

I am using Parcelable for a custom Song object I have in my class. However, when ever I am trying to get an arraylist extra of type Song I always get a null pointer exception. I have no problem getting string extras from the intent but when I try to get this ArrayList I need I always get null. I am also getting a null when I just try to pass a Song object, so I am assuming there is some issue with it but I cannot figure it out for the life of me.
This is my song class
import android.os.Parcel;
import android.os.Parcelable;
public class Song implements Parcelable {
private String uri;
private String title;
private String artist;
private String album;
private String length;
private int count;
private int source;
public Song () {
}
public Song (String title, String artist, String album, String uri, String length, int count,
int source) {
this.uri = uri;
this.title = title;
this.artist = artist;
this.album = album;
this.length = length;
this.count = count;
this.source = source;
}
public String getUri() {
return uri;
}
public String getArtist() {
return artist;
}
public String getTitle() {
return title;
}
public String getLength() {
return length;
}
public int getCount() {return count;}
#Override
public String toString() {
return title + " - " +artist;
}
#Override
public boolean equals(Object o){
if (o instanceof Song) {
Song song = (Song) o;
if (title.equals(song.title) && length.equals(song.length)) {
return true;
}
}
return false;
}
public int compareTo(Song s) {
if (this.count < s.getCount()) {
return -1;
}
else if(this.count > s.getCount()){
return 1;
}
return 0;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public int getSource() {
return source;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.uri);
dest.writeString(this.title);
dest.writeString(this.artist);
dest.writeString(this.album);
dest.writeString(this.length);
dest.writeInt(this.count);
dest.writeInt(this.source);
}
protected Song(Parcel in) {
this.uri = in.readString();
this.title = in.readString();
this.artist = in.readString();
this.album = in.readString();
this.length = in.readString();
this.count = in.readInt();
this.source = in.readInt();
}
public static final Creator<Song> CREATOR = new Creator<Song>() {
#Override
public Song createFromParcel(Parcel source) {
return new Song(source);
}
#Override
public Song[] newArray(int size) {
return new Song[size];
}
};
}
This is the line I use to package the arraylist
Intent intent = new Intent();
Log.d("UTILS ", " size: " +queue.size()); // Making sure it is not null before passing
intent.setClass(context, PlayerService.class);
intent.putParcelableArrayListExtra(PlayerService.EXTRA_TRACK_LIST, queue);
context.startService(intent);
This is retrieving the arraylist in PlayerService class
public static final String EXTRA_TRACK_LIST = "EXTRA_TRACK_LIST";
private ArrayList<Song> trackList;
.
.
.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent == null || intent.getAction() == null) {
Log.d(TAG, "unspecified command");
return START_STICKY;
}
trackList=intent.getParcelableArrayListExtra(PlayerService.EXTRA_TRACK_LIST);
if (trackList == null) {
Log.d("TRACKLIST", "IS NULL");
} else {
Log.d(TAG, "size: "+trackList.size());
}
.
.
.
// more irrelevant code
your parameter is wrong
public static final String EXTRA_TRACK_LIST = "EXTRA_TRACK_LIST";
so change like this
trackList = intent.getParcelableArrayListExtra(PlayerService.EXTRA_TRACK_LIST);
Is queue a Queue/List or similar? Or is it an array? If array, should be queue.length, right? From List to array try this. This should work.
intent.putParcelableArrayListExtra(PlayerService.EXTRA_TRACK_LIST, queue);
If not, let me know to keep trying. Also, you could also pass it as String. You could use GSON to convert it to JSON string. After converted to String, pass it as a single String.
Intent intent = new Intent(ActivityA.this, Service.class);
intent.putExtra(new Gson().toJson(queue), PlayerService.EXTRA_TRACK_LIST);
context.startService(intent);
And to retrieve that object again:
Song song = new Gson().fromJson(intent.getStringExtra(PlayerService.EXTRA_TRACK_LIST), Song.class);
Or, if list (I believe you case):
Type listType = new TypeToken<ArrayList<Song.class>>(){}.getType();
List<Song> songList = new Gson().fromJson(intent.getStringExtra(PlayerService.EXTRA_TRACK_LIST), listType);

How do I set my Object to a JList?

I'm trying to create a JList that displays items of an object that I created.
The object is an items object and the items class looks like this:
public class items {
private ArrayList<Item> itemlistweapons = new ArrayList<Item>();
private ArrayList<Item> itemlistapparel = new ArrayList<Item>();
private ArrayList<Item> itemlistaid = new ArrayList<Item>();
private ArrayList<Item> itemlistmisc = new ArrayList<Item>();
public void additem(String name, String type){
if("Weapon".equals(type)){
itemlistweapons.add(new Item(name, type));
}
else if ("Apparel".equals(type)){
itemlistapparel.add(new Item(name, type));
}
else if ("Aid".equals(type)){
itemlistaid.add(new Item(name, type));
}
else if ("Misc.".equals(type)){
itemlistmisc.add(new Item(name, type));
}
}
public void dropitem(String name){
for(int i = 0; i < itemlistweapons.size(); i++){
if(itemlistweapons.get(i).getname().equals(name)){
itemlistweapons.remove(i);
}
}
for(int i = 0; i < itemlistapparel.size(); i++){
if(itemlistapparel.get(i).getname().equals(name)){
itemlistapparel.remove(i);
}
}
for(int i = 0; i < itemlistaid.size(); i++){
if(itemlistaid.get(i).getname().equals(name)){
itemlistaid.remove(i);
}
}
for(int i = 0; i < itemlistmisc.size(); i++){
if(itemlistmisc.get(i).getname().equals(name)){
itemlistmisc.remove(i);
}
}
}
public ArrayList<Item> getItemlistweapons() {
return itemlistweapons;
}
public ArrayList<Item> getItemlistapparel() {
return itemlistapparel;
}
public ArrayList<Item> getItemlistaid() {
return itemlistaid;
}
public ArrayList<Item> getItemlistmisc() {
return itemlistmisc;
}
#Override
public String toString(){
String items = "";
if (itemlistweapons.size() > 0){
items += "Weapons\n";
for (Item itemlist1 : itemlistweapons) {
items += itemlist1 + "\n";
}
}
if (itemlistapparel.size() > 0){
items += "Apparel\n";
for (Item itemlist2 : itemlistapparel) {
items += itemlist2 + "\n";
}
}
if (itemlistaid.size() > 0){
items += "Aid\n";
for (Item itemlist3 : itemlistaid) {
items += itemlist3 + "\n";
}
}
if (itemlistmisc.size() > 0){
items += "Misc.\n";
for (Item itemlist4 : itemlistmisc) {
items += itemlist4 + "\n";
}
}
return items;
}
}
So, how would I take the variable itemlistweapons and set it so that it displays on a JList. The itemlistweapons is an array list of the Item time, and the Item class looks like this:
public class Item {
private String name;
private String type;
public Item(String itemname, String itemtype){
name = itemname;
type = itemtype;
}
public String getname(){
return name;
}
#Override
public String toString(){
String iteminfo;
iteminfo = name;
return iteminfo;
}
}
Can someone please tell me how to take an ArrayList<Item> from an items object and put it into a JList?
I think you may have a 3rd class with the GUI, in there you have the Jlist object
then you can do:
JList list = new JList(itemlistweapons .toArray());
and repeat respectively to any arrayList you want to display there.

How to display extra info in custom info adapter with Arraylist(populated with data from KSOAP Webservice) using hashmap

I made a custom info window that displays 5 info (Hospital name, Address, Fax, Mobile and Landline), I successfully displayed the Hospital and Address with the use of Title and Snippet when being pressed but not with the 3 last data. some blogs says I should use Hashmap to display extra data, but I am confused with their samples.
Code for marker:
ArrayList<MapConstants> _list = new ArrayList<MapConstants>(); _list.addAll(GlobalVariablesClass.FOR_MAPMARKER);
for (int i = 0; i < _list.size(); i++) {
MarkerOptions markerOption = (new MarkerOptions().position(new LatLng(Double.valueOf(_list.get(i).getLatitude()), Double.valueOf(_list.get(i).getLongitude()))).title(bla).snippet(bla).icon(bla);
}
I solved my problem By doing this:
1st: I created a Class in which I'll get stored the return of webservice and use it for the marker. (mapConstants.Class)
MapConstants.Class:
public class MapConstants {
private String CLINIC_NAME, CLINIC_CODE, UNIT_NO
,BLDG_NAME, STREET, COUNTRY, LATITUDE, LONGITUDE, LANDLINE, MOBILE, FAX;
public MapConstants()
{
}
public MapConstants (String ClinicName, String ClinicCode, String UnitNo, String BuildingName
, String Street, String Country, String Latitude, String Longitude, String Landline, String Mobile, String Fax)
{
CLINIC_NAME = ClinicName;
CLINIC_CODE = ClinicCode;
UNIT_NO = UnitNo;
BLDG_NAME = BuildingName;
STREET = Street;
COUNTRY = Country;
LATITUDE = Latitude;
LONGITUDE = Longitude;
LANDLINE = Landline;
MOBILE = Mobile;
FAX = Fax;
}
public String setClinicName() {
return this.CLINIC_NAME;
}
public String getClinicName() {
return this.CLINIC_NAME;
}
public String setClinicCode() {
return this.CLINIC_CODE;
}
public String getClinicCode() {
return this.CLINIC_CODE;
}
public String setUnitNo() {
return this.UNIT_NO;
}
public String getUnitNo() {
return this.UNIT_NO;
}
public String setBldgName() {
return this.BLDG_NAME;
}
public String getBldgName() {
return this.BLDG_NAME;
}
public String setStreet() {
return this.STREET;
}
public String getStreet() {
return this.STREET;
}
public String setCountry() {
return this.COUNTRY;
}
public String getCountry() {
return this.COUNTRY;
}
public String setLatitude() {
return this.LATITUDE;
}
public String getLatitude() {
return this.LATITUDE;
}
public String setLongitude() {
return this.LONGITUDE;
}
public String getLongitude() {
return this.LONGITUDE;
}
public String setMobile(){
return this.MOBILE;
}
public String getMobile(){
return this.MOBILE;
}
public String setLandline(){
return this.LANDLINE;
}
public String getLandline(){
return this.LANDLINE;
}
public String setFax(){
return this.FAX;
}
public String getFax(){
return this.FAX;
}
}
2nd: On the method where you make your marker(In my case it is addmarkersToMapNew()), I made a Hashmap that will store other data you need aside from title and Snippet.
addMarkersToMapNew():
private void addMarkersToMapNew()
{
//TODO addMarkersToMapNew
int markerCount = 0;
//ProgressDialog progDailog = null;
if (ConnectionEcardMessage.isConnectingToInternet(SearchMapFragmentActivity.this))
{
ArrayList<MapConstants> _list = new ArrayList<MapConstants>();
_list.addAll(GlobalVariablesClass.FOR_MAPMARKER);
Log.i("addMarkersToMapNew-size", ""+_list.size());
Log.i("addMarkersToMapNew", "ConnectionEcardMessage.isConnectingToInternet(SearchMapFragmentActivity.this)");
if (mMapMarkers == null)
{
Log.i("addMarkersToMapNew", "null");
mMapMarkers = new HashMap<String, MarkerOptions>();
extraMarkerInfo = new ArrayList<HashMap<String, String>>();
EcardMessage.appendLog("SearchMapFrag 6.5 - get curBranchDetails");
for (int i = 0; i < _list.size(); i++)
{
HashMap<String, String> contactHashmap = new HashMap<String, String>();
Log.d("myApp", "added branch " + _list.get(i).getClinicName());
MarkerOptions markerOption = (new MarkerOptions()
.position(
new LatLng(Double.valueOf(_list.get(i).getLatitude()),
Double.valueOf(_list.get(i).getLongitude())))
.title(_list.get(i).getClinicName()) .snippet(_list.get(i).getUnitNo()+""+_list.get(i).getBldgName()+","+_list.get(i).getStreet()+", "+_list.get(i).getCountry())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.address)));
Log.i("Latitude_At_Longitude", "EDI WOW");
contactHashmap.put("landline", _list.get(i).getLandline() );
Log.i("kay landline", _list.get(i).getLandline());
contactHashmap.put("mobile", _list.get(i).getMobile() );
Log.i("kay Mobile", _list.get(i).getMobile());
contactHashmap.put("fax", _list.get(i).getFax() );
Log.i("kay Fax", _list.get(i).getFax());
contactHashmap.put("title", _list.get(i).getClinicName());
Log.i("kay title", _list.get(i).getClinicName());
extraMarkerInfo.add(contactHashmap);
mMapMarkers.put("Marker" + ++markerCount, markerOption);
mMap.addMarker(markerOption);
}
}
}
3rd: After I saved the other Data that I need to display in Hashmap(extraMarkerInfo), I can now declare my Custom Info window:
Custom Info Window:
class MyInfoWindowAdapter implements InfoWindowAdapter
{
private final View myContentsView;
MyInfoWindowAdapter(){
myContentsView = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
}
#Override
public View getInfoContents(Marker marker) {
//TODO getInfoContents
TextView tvTitle = ((TextView)myContentsView.findViewById(R.id.title));
tvTitle.setText(marker.getTitle());
TextView tvSnippet = ((TextView)myContentsView.findViewById(R.id.snippet));
tvSnippet.setText(marker.getSnippet());
// private static HashMap<String, MarkerOptions> mMapMarkers = null;
String landline = "";
String mobile = "";
String fax = "";
for (int i = 0; i < extraMarkerInfo.size(); i++) {
if (extraMarkerInfo.get(i).get("title").toString().equalsIgnoreCase(marker.getTitle()))
{
landline = extraMarkerInfo.get(i).get("landline").toString();
Log.i("get_Landline", landline);
mobile = extraMarkerInfo.get(i).get("mobile").toString();
Log.i("get_Mobile", mobile);
fax = extraMarkerInfo.get(i).get("fax").toString();
Log.i("get_Fax", fax);
}
}
TextView tvLandline = ((TextView)myContentsView.findViewById(R.id.landline_number));
tvLandline.setText(landline);
TextView tvMobile = ((TextView)myContentsView.findViewById(R.id.mobile_number));
tvMobile.setText(mobile);
TextView tvFax = ((TextView)myContentsView.findViewById(R.id.fax_number));
tvFax.setText(fax);
return myContentsView;
}
#Override
public View getInfoWindow(Marker arg0) {
// TODO Auto-generated method stub
return null;
}
}

Categories