Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7705655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:55:04+00:00 2026-05-31T23:55:04+00:00

A problem with a custom view adapter that implement listeners form button and chronometer.

  • 0

A problem with a custom view adapter that implement listeners form button and chronometer.
when the list is populated for the first time, everything works fine(i can click the play button and see the chronometer working fine). but when i scroll to the other items and return to the first elements the button listeners won’t work and the chronometer is reset to 0 and stop listening to the tickEvent.

Please refer the following code snippet :

<!-- language: lang-java -->

public class PodcastListAdapter extends BaseAdapter implements OnPreparedListener, Runnable, OnChronometerTickListener{

MediaPlayer mediaPlayer;
 Button start , stop , pause;
 private List<Button> starts, stops, pauses;
 TextView mobi ;
 Chronometer chrono;
 private List<Integer> usedPositions  = new ArrayList<Integer>();



 private static ProgressBar progressBar;
private List<Message> messageList;
private List<ProgressBar> progressBars;
private List<Chronometer> chronos;

private Context context;

public PodcastListAdapter(List<Message> tweetList, Context context) {
    this.messageList = tweetList;
    this.context = context;
    this.progressBars=new LinkedList<ProgressBar>();
    this.chronos=new LinkedList<Chronometer>();
    this.starts=new LinkedList<Button>();
    this.stops=new LinkedList<Button>();
    this.pauses=new LinkedList<Button>();
    IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();
    myService.setPListener(this);

}

public int getCount() {
    // TODO Auto-generated method stub
    return messageList.size();
}

public Message getItem(int position) {
    // TODO Auto-generated method stub
    return messageList.get(position);

}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    //LinearLayout itemLayout;
    RelativeLayout itemLayout;
    //ProgressBar progressBar;
     Log.d("debog", "onPrepared"+position);

    final Message message = messageList.get(position);

    itemLayout= (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.podcastmessage, parent, false);
    TextView tvDate = (TextView) itemLayout.findViewById(R.id.PodcastDate);
    tvDate.setText(message.getDate());

    TextView tvCategory = (TextView) itemLayout.findViewById(R.id.PodcastCategory);
    tvCategory.setText(message.getCategory());
    //progress Bar
    progressBar = (ProgressBar) itemLayout.findViewById(R.id.progressBar);
    progressBar.setTag(position);
    progressBars.add(position,progressBar);
    //progressBar.setMax(100);
    //progressBar.setProgress(50);
    //progress Bar Fin



    //récupérer le buttons
    start=(Button) itemLayout.findViewById(R.id.play_pc);

    start.setTag(position);
    starts.add(position,start);
    //associer les listners
      start.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                int pos=(Integer) v.getTag();
                starts.get(pos).setEnabled(false);
              stops.get(pos).setEnabled(true);
              pauses.get(pos).setEnabled(true);
              //start.setEnabled(false);
              startStreamingAudio(messageList.get(pos).getLink().toString(),pos) ;
              Log.d("StreamingAudio", "StartEvent");
              Toast.makeText(context.getApplicationContext(), "Veuillez attendre le chargement... ", 3000).show();


            }   
    });

    stop=(Button)itemLayout.findViewById(R.id.stop_pc); 
    stop.setTag(position);
      stops.add(position,stop);
      stop.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                int pos=(Integer) v.getTag();

                stopStreamingAudio(pos);
                starts.get(pos).setEnabled(true);
                 stops.get(pos).setEnabled(false);
                 pauses.get(pos).setEnabled(false);


                  Log.d("StreamingAudio", "StopEvent");

            } 
    });  

      pause=(Button)itemLayout.findViewById(R.id.pause_pc);

    pause.setTag(position);
      pauses.add(position,pause);
      pause.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            int pos=(Integer) v.getTag();
            pauseStreamingAudio();
            starts.get(pos).setEnabled(true);
            pauses.get(pos).setEnabled(false);
            stops.get(pos).setEnabled(true);

        }
    });

      //chronometre
      chrono=(Chronometer) itemLayout.findViewById(R.id.chrono);
      chrono.setTag(position);
      chrono.setOnChronometerTickListener(this);
      chronos.add(position,chrono);






    if(position%2==0)
    {

        //itemLayout.setBackgroundResource(R.drawable.cellule);
        itemLayout.setBackgroundColor(Color.rgb(134, 137, 142));
        //ImageView iv=(ImageView) itemLayout.findViewById(R.id.bg_cellule);
        //iv.setVisibility(4);
        tvCategory.setTextColor(Color.WHITE);
        tvDate.setTextColor(Color.WHITE);

    }



    return itemLayout;
}
protected void pauseStreamingAudio() {
    // TODO Auto-generated method stub
    IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();
    myService.pause();

}

private void startStreamingAudio(String link, int position) {

     IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();

     if(myService.isPaused())
     {
         myService.start();
         chrono=chronos.get(position);
         chrono.start();

         return;
     }

    if(myService.isPlaying())
    {
        Log.d("startStream", "Stop");
        myService.stop();
    }
    //préparation du lien (insérer %20 dans le nom du fichier)

    link=link.replaceAll("\\s", "%20");
    Log.d("ListAdapter", link);


    myService.setSource(link);

    //myService.start();

    //myService.setProgressBar(progressBar);
    progressBar=progressBars.get(position);
    myService.setProgressBar(progressBar);
    progressBar.setVisibility(ProgressBar.VISIBLE);
    progressBar.setProgress(0);
    progressBar.setMax(myService.getDuration());
    //new Thread(this).start();//fo progress bar
    //progressBar.setMax(100);
    //progressBar.setProgress(50);
    chrono=chronos.get(position);
    //chrono.setBase(SystemClock.elapsedRealtime());


    //chrono.start();
    Log.d("duration", String.valueOf(myService.getDuration()));



}


private void stopStreamingAudio(int position) {

    IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();

    Log.d("stopStream","isplaying"+myService.isPlaying());
    if(myService.isPlaying())
    {

        myService.stop();
        //myService.getProgressBar().setProgress()
        //chrono=chronos.get(position);
        //chrono.stop();


    }
    else if(myService.isPaused())
    {

        myService.stop();
        //myService.getProgressBar().setProgress()
        //chrono=chronos.get(position);
        //chrono.stop();


    }
    //chrono.setText("00:00");

}


public void run() {
IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();
int currentPosition= 0;
int total = myService.getDuration();

Log.d("run", "dans le thread"+total+myService.isPlaying());
while (myService.isPlaying()/* && currentPosition<total*/) {
    try {
        Thread.sleep(1000);
        Log.d("run", "while dans le thread");
        currentPosition= myService.getPosition();
        Log.d("currentPosition", String.valueOf(currentPosition));

    } catch (InterruptedException e) {
        return;
    } catch (Exception e) {
        return;
    }
    //progressBar=myService.getProgressBar();
    //progressBar.setProgress(currentPosition);
    if(myService.getProgressBar()!=null)
    myService.getProgressBar().setProgress(currentPosition);
    //Log.d("getProgress", String.valueOf(myService.getPosition()));
    //Log.d("getDuration", String.valueOf(myService.getDuration()));
}
if(myService.getProgressBar()!=null)
myService.getProgressBar().setProgress(0);
}

public void onChronometerTick(Chronometer chronometer) {
// TODO Auto-generated method stub
IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();

//Log.d("chrono", "chronotick");
//Log.d("chrono", ""+myService.getPosition());

if(myService.isPaused()) chronometer.stop();
else if(myService.isPlaying())
{
    String m,s,currentTime;
    long minutes=((myService.getPosition())/1000)/60;

    if(minutes<10) m="0"+minutes;
    else m=String.valueOf(minutes);
    long seconds=((myService.getPosition())/1000)%60;
    if(seconds<10) s="0"+seconds;
    else s=String.valueOf(seconds);

    currentTime=m+":"+s;
    chronometer.setText(currentTime);

    /*chronometer.setEms(myService.getPosition())*/;
}
else { chronometer.stop(); chronometer.setText("00:00");}

}

public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();
myService.start();
new Thread(this).start();
chrono.start();

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:55:05+00:00Added an answer on May 31, 2026 at 11:55 pm

    you can perform this below way

    package com.CommonMethod.Activity;
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.ListView;
    import android.widget.TextView;
    
    public class ListViewDemo2 extends Activity {
        ListView LS;
        LayoutInflater Linflater;
    
        public static LinearLayout LL;
        public static TextView txtOne;
        public static TextView txtTwo;
        public static Button btnone;
        public static Button btnTwo;
        public static Button btnThree;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listviewdemo);
            LS = (ListView) findViewById(R.id.LSOne);
    
            Linflater = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
            LS.setAdapter(new ListAdapter(this));
    
        }
    
        public class ListAdapter extends BaseAdapter {
    
            public ListAdapter(Context con) {
    
            }
    
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return 10;
            }
    
            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return null;
            }
    
            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }
    
            @Override
            public int getViewTypeCount() {
                // TODO Auto-generated method stub
                return 10;
            }
    
            @Override
            public int getItemViewType(int position) {
                // TODO Auto-generated method stub
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View ReturnView = convertView;
                int _intPostion = getItemViewType(position);
    
                if (ReturnView == null) {
                    ReturnView = Linflater.inflate(R.layout.rawlistview2, null);
                    LL = (LinearLayout) ReturnView.findViewById(R.id.LLOne);
                    txtOne = (TextView) ReturnView
                            .findViewById(R.id.textView1list2);
                    txtTwo = (TextView) ReturnView.findViewById(R.id.textViewlist2);
    
                    btnone = (Button) ReturnView.findViewById(R.id.button1);
                    btnTwo = (Button) ReturnView.findViewById(R.id.button2);
                    btnThree = (Button) ReturnView.findViewById(R.id.button3);
    
                    btnone.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.i("ButtonOne", btnone.getText().toString());
                        }
                    });
                    btnTwo.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.i("ButtonTwo", btnTwo.getText().toString());
                        }
                    });
                    btnThree.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.i("ButtonThree", btnThree.getText().toString());
                        }
                    });
                }
                return ReturnView;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used a custom array adapter to populate my list view.The problem i
I'm making a custom adapter so that I can display a list of items
My problem is that I don't know whether I should use multiple list view
Currently, I have a custom list adapter that has some logic that hide/show a
i have a list view and custom list adapter for it. There are various
Problem I have a custom tab control using Chrome-shaped tabs that binds to a
My problem is similar to Django Passing Custom Form Parameters to Formset Ive have
I am trying to create a list view which as TextView that can display
I have a listview with custom array adapter, the view allows the user to
I am developing an app which has a list view with custom layout as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.