Sun
Sun

Reputation: 6888

How to change focus on next list item automatically

I am writing Audio Player app, in which i have to change focus on next list item automatically when a new song starts, i am able to play next song but unable to highlight that 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 on click listener:

     listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                long id) {
            // TODO Auto-generated method stub                          

            positionGlobal = position;  

            strNURL = audiosArrayList.get(positionGlobal).getUrl().toString();
            strNTITLE = audiosArrayList.get(positionGlobal).getTitle().toString();

            if(mediaPlayer!=null && mediaPlayer.isPlaying())
                try {
                    mediaPlayer.stop();
                  } catch (Exception e) { 

                  }
                play();
                textTrack.setText(strNTITLE);
            }
        });                         
    }

Method using to switch to next song automatically

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;

    }

}

Upvotes: 5

Views: 2098

Answers (4)

Lokesh
Lokesh

Reputation: 3334

try setItemChecked, focus your list view. without call nofifydatachanges

listview.setItemChecked(position, true);

and you also try

listview.setSelection(position);

Upvotes: 0

SweetWisher ツ
SweetWisher ツ

Reputation: 7306

You can achieve this through below list of steps :

  1. Set selected item with keeping track of current selection in Adapter
  2. Perform item click
  3. Scroll to selected item : smoothScrollToPosition(int position)

Code snippet :

mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
        // First set next position in positionGlobal   

        //highlight ListItem
        audioAdapter.setSelectedItem(position);

        // Then perform Click
        listview.performItemClick(listview, positionGlobal, listview.getItemIdAtPosition(positionGlobal));

        // Scroll to selected position
        listview.smoothScrollToPosition(positionGlobal);
    }
});

The Adapter class will look like:

public AudioAdapter(Context context, int resource, ArrayList<Audio> objects) {
    public void setSelectedItem(int selectedItem) {
        this.selectedItem = selectedItem;
        notifyDataSetChange();
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent){
          // convert view = design
          View v = convertView;
          // Your code

          // set selected Item
          if(position == selectedItem) {
            // you can define your own color of selected item here
          v.setBackgroundColor(StringUtils.getColorFromResources(R.color.light_blue));
        } else {
            // you can define your own default selector here
            v.setBackground(StringUtils.getDrawableFromResources(R.drawable.abs__list_selector_holo_light));
        }
     }
 }

Hope it helps ツ

Upvotes: 2

Antonio MG
Antonio MG

Reputation: 20410

Your code seems fine, just call notifyDataSetChange in your adapter to inform it that the data in the array has changed and it needs to reload it:

If you need to get items at previous position, just do:

if (position != 0) {
            positionGlobal = position - 1;  

            strNURL = audiosArrayList.get(positionGlobal).getUrl().toString();
            strNTITLE = audiosArrayList.get(positionGlobal).getTitle().toString();
}

Upvotes: 0

SANJAY GUPTA
SANJAY GUPTA

Reputation: 1604

For doing so you have to send value to adapter that will notify adapter that which position of listview will be set value.Only doing this way you can do that.And again you have to set adapter.

Upvotes: 0

Related Questions