开发者

android display image on specific row of listview

I have a listview containing 25 different items. When I click on an item, a corresponding sound file gets added to an array, starting with index 0 of course, and as more items get selected, each corresponding sound file gets added to the next index of the array. So, when the play button is pressed, the sound file associated with index 0 plays, then the sound for index 1, and so on until there are no more indexes. With that little bit of (possibly unne开发者_运维知识库cessary) background info, how would I add an image to the listview of the item currently playing? Like an ipod, I want to display an icon on the right hand side on the row of the currently playing sound. I have an icon, and I can display it, but I'm not sure how to tell it on which row to display, based on the sound/array index playing. I have read different tutorials on adding images to listviews, but nothing really about adding just one image to a specific row.


To solve your problem, I think you may need to implement your own subclass of BaseAdapter by overriding the abstract methods. Like SimpleAdapter and SimpleCursorAdapter, the new adapter bind data( sound file in your case) to the list, but your BaseAdpater should be capable of finding what kind of view should appear in each position of the list, and be capable of generating the proper list view item.

You need override methods of BaseAdapter to meet your needs. The most important are followings:

int getViewTypeCount();

In your case, the list contains two kinds of item: playing or not in playing, so this method should return 2.

int getItemViewType(int position);

You need put some logic here to check if the sound file at the parameter position is playing? If the item at position is playing, you should return a constant int like TYPE_PLAYING, otherwise return the TYPE_NONPLAYING(of course you need to define these constants ahead).

View getView(int position, View convertView, ViewGroup parentview);

This method is which has the most TODOs, I think you can following steps below:

  1. Find the type of item at position, either TYPE_PLAYING or TYPE_NONPLAYING

  2. Get a instance of LayoutInflater to inflate different layout file two different type of item, The TYPE_PLAYING item should include the image to indicate that item's corresponding file in is playing. Note you need a Context variable to retrieve the LayoutInflater. You can declare a Context member in the Adapter and initialize it in the constructor of Adapter.

  3. Bind other information of each file: file name, time length, etc.

If the playing file is changed by clicking the list item or other interaction, don't forget to call BaseAdapter's notifyDatasetChanged() method to refresh the list.

May it helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜