I am writing an Audio Player application in which I have to automatically change focus to the next element of the list, when a new song starts, I can play the next song, but I can not select this song.
<ListView android:id="@+id/list_slidermenu" android:layout_height="0dp" android:layout_width="match_parent" android:layout_weight="8" android:layout_gravity="start" android:scrollbars="none" android:fastScrollEnabled="true" android:choiceMode="singleChoice" android:listSelector="@drawable/list_selector" />
ListView to listen for clicks:
listview.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
Method used to automatically switch to the next song
public void next() { if (positionGlobal == (audiosArrayList.size()-1)) { positionGlobal = 0; strNURL = audiosArrayList.get(positionGlobal).getUrl().toString(); strNTITLE = audiosArrayList.get(positionGlobal).getTitle().toString(); current_position = 0; mediaPlayer.seekTo(current_position); if(mediaPlayer!=null && mediaPlayer.isPlaying()) try { mediaPlayer.stop(); } catch (Exception e) { } play(); textTrack.setText(strNTITLE); } else if(positionGlobal < audiosArrayList.size()) { positionGlobal = positionGlobal+1; strNURL = audiosArrayList.get(positionGlobal).getUrl().toString(); strNTITLE = audiosArrayList.get(positionGlobal).getTitle().toString(); current_position = 0; mediaPlayer.seekTo(current_position); if(mediaPlayer!=null && mediaPlayer.isPlaying()) try { mediaPlayer.stop(); } catch (Exception e) { } play(); textTrack.setText(strNTITLE); } }
Adapter Class:
public class AudioAdapter extends BaseAdapter { ArrayList<Audio> actorList; LayoutInflater vi; int Resource; ViewHolder holder; Context context; public AudioAdapter(Context context, int resource, ArrayList<Audio> objects) { this.context = context; this.vi = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.Resource = resource; this.actorList = objects; } @Override public int getCount() { // TODO Auto-generated method stub return actorList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return actorList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // convert view = design View v = convertView; if (v == null) { holder = new ViewHolder(); v = vi.inflate(Resource, null); holder.tvName = (TextView) v.findViewById(R.id.title); v.setTag(holder); } else { holder = (ViewHolder) v.getTag(); } holder.tvName.setText(actorList.get(position).getTitle()); return v; } static class ViewHolder { public TextView tvName; } }
source share