Re: [android-beginners] Using Methods in a Service from an Activity
Ok, I've found the problem, for anyone else having trouble. In the tutorial, onBind() returns null, I created a MediaBinder class within MediaService, that implemented my MediaInterface, calling the relevant methods in MediaService. This works nicely. On 19/04/2010 21:43, Mark Murphy wrote: Tom F M White wrote: I'm calling start a relatively long time later, several seconds. Do I need to call onServiceConnected() myself? No, it should be called shortly after you call bindService(). This suggests that your bindService() call is failing for some reason (e.g., your service is not registered in your manifest). Here is a sample project from one of my books showing the use of bindService(): http://github.com/commonsguy/cw-android/tree/master/Service/WeatherPlus/ -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en
[android-beginners] Images Reused in a ListView
I've made a custom ListAdaptor, with associated View, that displays an icon on the left of each list item, with three lines of text to the right. In MyView.onCreate(), the icons are populated using imageView.setImageDrawable(Drawable.createFromStream(URL)); each item on the list is it's own object, so each of these is done individually for each list item. However when viewing the list, the first few items display correctly, but as you scroll down the list, those images that were not visible initially are replaced with the same images used in the top few items, so 4/5 images end up reused for the entire list. The text for each list item is correct. What is going wrong? Tom -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en
Re: [android-beginners] Images Reused in a ListView
TripleText is just an object for storing 4 strings, 3 for the text, 1 for the image URL. Rest of the code follows, sorry it's quite long... TripleTextView: public class TripleTextView extends LinearLayout { private TextView mText1; private TextView mText2; private TextView mText3; private String imageURL; private ImageView image; private Drawable imageDrawable; public TripleTextView(Context context, TripleText tt) { super(context); this.setOrientation(HORIZONTAL); //add image imageURL = tt.getImageURL(); try { Log.v(TTV,Loading Drawable from: +imageURL); imageDrawable = Drawable.createFromStream(new URL(imageURL).openStream(), src); Log.v(TTV,Image created ok); } catch (MalformedURLException e) { Log.v(TTV,Malformed URL); } catch (IOException e) { Log.v(TTV,IO Exception); } image = new ImageView(context); image.setImageDrawable(imageDrawable); addView(image, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); LinearLayout subLayout = new LinearLayout(context); subLayout.setOrientation(VERTICAL); mText1 = new TextView(context); mText1.setText(tt.getText1()); subLayout.addView(mText1, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mText2 = new TextView(context); mText2.setText(tt.getText2()); subLayout.addView(mText2, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mText3 = new TextView(context); mText3.setText(tt.getText3()); subLayout.addView(mText3, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); addView(subLayout, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } public void setText1(String words) { mText1.setText(words); } public void setText2(String words) { mText2.setText(words); } public void setText3(String words) { mText3.setText(words); } public void setImageURL(String imageURL) { this.imageURL = imageURL; } } TripleListAdapter: public class TripleListAdapter extends BaseAdapter { private Context mContext; private ListTripleText mItems = new ArrayListTripleText(); public TripleListAdapter(Context context) { mContext = context; } public void addItem(TripleText it) { mItems.add(it); } public void setListItems(ListTripleText lit) { mItems = lit; } public int getCount() { return mItems.size(); } public Object getItem(int position) { return mItems.get(position); } public boolean areAllItemsSelectable() { return false; } public boolean isSelectable(int position) { try{ return mItems.get(position).isSelectable(); }catch (IndexOutOfBoundsException aioobe){ return false; } } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { TripleTextView ttv; if (convertView == null) { ttv = new TripleTextView(mContext, mItems.get(position)); } else { ttv = (TripleTextView) convertView; ttv.setText1(mItems.get(position).getText1()); ttv.setText2(mItems.get(position).getText2()); ttv.setText3(mItems.get(position).getText3()); ttv.setImageURL(mItems.get(position).getImageURL()); } return ttv; } } On 20/04/2010 15:25, Martin Obreshkov wrote: Can you paste some code On Tue, Apr 20, 2010 at 5:19 PM, Tom F M White fred...@gmail.com mailto:fred...@gmail.com wrote: I've made a custom ListAdaptor, with associated View, that displays an icon on the left of each list item, with three lines of text to the right. In MyView.onCreate(), the icons are populated using imageView.setImageDrawable(Drawable.createFromStream(URL)); each item on the list is it's own object, so each of these is done individually for each list item. However when viewing the list, the first few items display correctly, but as you scroll down the list, those images that were not visible initially are replaced with the same images used in the top few items, so 4/5 images end up reused for the entire list. The text for each list item is correct. What is going wrong? Tom -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android
Re: [android-beginners] Images Reused in a ListView
Thanks for your help. On 20/04/2010 15:37, Mark Murphy wrote: Tom F M White wrote: TripleText is just an object for storing 4 strings, 3 for the text, 1 for the image URL. Rest of the code follows, sorry it's quite long... TripleTextView: public class TripleTextView extends LinearLayout { private TextView mText1; private TextView mText2; private TextView mText3; private String imageURL; private ImageView image; private Drawable imageDrawable; public TripleTextView(Context context, TripleText tt) { super(context); this.setOrientation(HORIZONTAL); //add image imageURL = tt.getImageURL(); try { Log.v(TTV,Loading Drawable from: +imageURL); imageDrawable = Drawable.createFromStream(new URL(imageURL).openStream(), src); Log.v(TTV,Image created ok); } catch (MalformedURLException e) { Log.v(TTV,Malformed URL); } catch (IOException e) { Log.v(TTV,IO Exception); } image = new ImageView(context); image.setImageDrawable(imageDrawable); addView(image, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); LinearLayout subLayout = new LinearLayout(context); subLayout.setOrientation(VERTICAL); mText1 = new TextView(context); mText1.setText(tt.getText1()); subLayout.addView(mText1, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mText2 = new TextView(context); mText2.setText(tt.getText2()); subLayout.addView(mText2, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); mText3 = new TextView(context); mText3.setText(tt.getText3()); subLayout.addView(mText3, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); addView(subLayout, new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } public void setText1(String words) { mText1.setText(words); } public void setText2(String words) { mText2.setText(words); } public void setText3(String words) { mText3.setText(words); } public void setImageURL(String imageURL) { this.imageURL = imageURL; } } TripleListAdapter: public class TripleListAdapter extends BaseAdapter { private Context mContext; private ListTripleText mItems = new ArrayListTripleText(); public TripleListAdapter(Context context) { mContext = context; } public void addItem(TripleText it) { mItems.add(it); } public void setListItems(ListTripleText lit) { mItems = lit; } public int getCount() { return mItems.size(); } public Object getItem(int position) { return mItems.get(position); } public boolean areAllItemsSelectable() { return false; } public boolean isSelectable(int position) { try{ return mItems.get(position).isSelectable(); }catch (IndexOutOfBoundsException aioobe){ return false; } } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { TripleTextView ttv; if (convertView == null) { ttv = new TripleTextView(mContext, mItems.get(position)); } else { ttv = (TripleTextView) convertView; ttv.setText1(mItems.get(position).getText1()); ttv.setText2(mItems.get(position).getText2()); ttv.setText3(mItems.get(position).getText3()); ttv.setImageURL(mItems.get(position).getImageURL()); } return ttv; } } In getView(), in the case where convertView is not null, you are calling setImageURL(), then doing nothing with that value to actually load in the replacement image. Also, you are downloading the images on the main application thread. That's going to be a problem -- your UI will freeze, and eventually Android may kill it off with an application not responding error. Please download your images off the main application thread, such as via an AsyncTask. -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en
[android-beginners] Media Player App
Hi I'm making a simple media player app, that streams files from a site. Currently the fundamentals of my app work, it is downloading the track info, and playing the files fine. However, at the moment it isn't working in a user-friendly manner, and usually getting it to play more than one song is a big problem. Here is the basic structure: The main Activity is a ListActivity, on which the list of tracks on the site is displayed. Clicking these launches a Details Activity, which shows track info, artwork etc. and a play button. The play button launches the NowPlaying Activity, where the content is fetched and played. The idea is that once a track is playing, the user can continue to browse other tracks, and return to the NowPlaying Activity via the menu. At the moment, I can go back to the list, or to the Details Activity for the song that is playing, but loading up the Details Activity for another song causes the sound to stop, and then trying to play other songs doesn't work, they do not buffer or play. I'm pretty sure this is because rather than re-using the same Details/NowPlaying Activities to display different content, new ones are getting spawned but I'm not sure about this, or sure how I would overcome this. In particular it is important there is only one NowPlaying Activity, as this handles Notifications, and contains the MediaPlayer object, of which I only want one. I'm not really sure what to ask for, perhaps my explanation of the problem can help someone point out what I'm doing wrong. Thanks in advance Tom -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en
Re: [android-beginners] Media Player App
Thanks for your reply, I'm looking into the documentation for Services now. Tom On 19/04/2010 19:31, Justin Anderson wrote: First, whatever is playing the music should run as a service and not as an activity... I would probably create a NowPlayingService and a NowPlayingActivity. Activities don't run in the background. When they are no longer visible they are paused by the OS. Services are allowed to run in the background and that is exactly how the built-in music player on Android works. It has a service in the background that actually plays the music. You can find more about this here: http://developer.android.com/guide/topics/fundamentals.html#servlife If you are already doing that then I'm not sure offhand what the problem is exactly. You may need to provide more details unless someone else has any idea... -- There are only 10 types of people in the world... Those who know binary and those who don't. -- On Mon, Apr 19, 2010 at 10:47 AM, Tom F M White fred...@gmail.com mailto:fred...@gmail.com wrote: Hi I'm making a simple media player app, that streams files from a site. Currently the fundamentals of my app work, it is downloading the track info, and playing the files fine. However, at the moment it isn't working in a user-friendly manner, and usually getting it to play more than one song is a big problem. Here is the basic structure: The main Activity is a ListActivity, on which the list of tracks on the site is displayed. Clicking these launches a Details Activity, which shows track info, artwork etc. and a play button. The play button launches the NowPlaying Activity, where the content is fetched and played. The idea is that once a track is playing, the user can continue to browse other tracks, and return to the NowPlaying Activity via the menu. At the moment, I can go back to the list, or to the Details Activity for the song that is playing, but loading up the Details Activity for another song causes the sound to stop, and then trying to play other songs doesn't work, they do not buffer or play. I'm pretty sure this is because rather than re-using the same Details/NowPlaying Activities to display different content, new ones are getting spawned but I'm not sure about this, or sure how I would overcome this. In particular it is important there is only one NowPlaying Activity, as this handles Notifications, and contains the MediaPlayer object, of which I only want one. I'm not really sure what to ask for, perhaps my explanation of the problem can help someone point out what I'm doing wrong. Thanks in advance Tom -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com mailto:android-beginners%2bunsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en
[android-beginners] Using Methods in a Service from an Activity
Further to my previous email, I have sketched out how my Media Player will work with the actual MediaPlayer object in a separate service, MediaService. I can't work out how to make calls, and retrieve data from the service. Following tutorials online has lead me to this: MyApp.java - top level Activity, in onCreate(): ... MediaConnection conn = new MediaConnection(); StateApp state = (StateApp) getApplicationContext(); bindService( new Intent(this,MediaService.class), conn, Context.BIND_AUTO_CREATE); state.setMediaConnection(conn); ... MediaView.java - View for playing media files, in play button onClick(): ... StateApp state = (StateApp) context.getApplicationContext(); MediaConnection conn = state.getMediaConnection(); conn.start(mediaLocation); ... MediaConnection implements ServiceConnection: public class MediaConnection implements ServiceConnection, MediaInterface{ private Binder service; public void start(String loc){ MediaInterface i = (MediaInterface)service; i.start(loc); } public void onServiceDisconnected(ComponentName cn){ Log.i(INFO, Service unbound ); } public void onServiceConnected(ComponentName cn, IBinder b){ service = (Binder)b; Log.i(INFO, Service bound ); } } Which throws a NullPointerException at i.start(loc). Can anyone tell me what I'm doing wrong? -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en
Re: [android-beginners] Using Methods in a Service from an Activity
On 19/04/2010 21:36, Mark Murphy wrote: Tom F M White wrote: Further to my previous email, I have sketched out how my Media Player will work with the actual MediaPlayer object in a separate service, MediaService. I can't work out how to make calls, and retrieve data from the service. Following tutorials online has lead me to this: MyApp.java - top level Activity, in onCreate(): ... MediaConnection conn = new MediaConnection(); StateApp state = (StateApp) getApplicationContext(); bindService( new Intent(this,MediaService.class), conn, Context.BIND_AUTO_CREATE); state.setMediaConnection(conn); ... MediaView.java - View for playing media files, in play button onClick(): ... StateApp state = (StateApp) context.getApplicationContext(); MediaConnection conn = state.getMediaConnection(); conn.start(mediaLocation); ... MediaConnection implements ServiceConnection: public class MediaConnection implements ServiceConnection, MediaInterface{ private Binder service; public void start(String loc){ MediaInterface i = (MediaInterface)service; i.start(loc); } public void onServiceDisconnected(ComponentName cn){ Log.i(INFO, Service unbound ); } public void onServiceConnected(ComponentName cn, IBinder b){ service = (Binder)b; Log.i(INFO, Service bound ); } } Which throws a NullPointerException at i.start(loc). Can anyone tell me what I'm doing wrong? Off the cuff, I would guess that you are calling start() before Android calls onServiceConnected(). I'm calling start a relatively long time later, several seconds. Do I need to call onServiceConnected() myself? -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en
Re: [android-beginners] Using Methods in a Service from an Activity
On 19/04/2010 21:43, Mark Murphy wrote: Tom F M White wrote: I'm calling start a relatively long time later, several seconds. Do I need to call onServiceConnected() myself? No, it should be called shortly after you call bindService(). This suggests that your bindService() call is failing for some reason (e.g., your service is not registered in your manifest). Here is a sample project from one of my books showing the use of bindService(): http://github.com/commonsguy/cw-android/tree/master/Service/WeatherPlus/ Some strange behaviour, the service is being started ok, and bindService() is returning true, yet onServiceConnected() never seems to be called, and I can't seem to find a reason. -- You received this message because you are subscribed to the Google Groups Android Beginners group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en