1. Download our Official Android App: Forums for Android!

Apps Cant update a single row in listview

Discussion in 'Android Development' started by Subhomoy Goswami, Oct 9, 2017.

  1. Subhomoy Goswami

    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    3
    Joined:
    Oct 9, 2017

    Oct 9, 2017
    3
    0
    15
    Hello every body, i'm new to android and i'm creating a mp3 app where there will be a list of all the mp3s and two buttons. "play" and "download" button.

    The problem i'm facing is that when ever i'm clicking on any of the play button, the music starts and the button changes from play to pause.

    but when i'm clicking on any other list, the same happens but the previous play button should change to "play" button which I cant make it work.

    Below i'm sharing my code.

    Code (Text):
    1. public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    2.  
    3.         if(convertView == null)
    4.         {
    5.             LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    6.             convertView = inflater.inflate(R.layout.songs_list,null,true);
    7.         }
    8.  
    9.         final Songs songs = getItem(position);
    10.  
    11.         TextView songs_name = (TextView) convertView.findViewById(R.id.songs_name);
    12.         songs_name.setText(songs.getName());
    13.  
    14.         final ImageButton listPlayBtn = (ImageButton) convertView.findViewById(R.id.listPlayBtn);
    15.  
    16.         final Integer play = 0;
    17.         listPlayBtn.setOnClickListener(new View.OnClickListener() {
    18.             String song_name = songs.getName();
    19.             String song_url = songs.getUrl();
    20.             Integer play1 = play;
    21.  
    22.             @Override
    23.             public void onClick(View v) {
    24.                 MucisPlayer mp = new MucisPlayer();
    25.                 if(play1 == 0) {
    26.                     mp.play1(song_url, song_name, mediaplayer);
    27.                     play1 = 1;
    28.                     System.out.println("Play");
    29.                     listPlayBtn.setImageResource(R.drawable.pause);
    30.                 }
    31.                 else
    32.                 {
    33.                     mp.pause(mediaplayer);
    34.                     play1 = 0;
    35.                     System.out.println("Pause");
    36.                     listPlayBtn.setImageResource(R.drawable.play);
    37.                 }
    38.             }
    39.         });
    40.  
    41.         final ImageButton listDownloadBtn = (ImageButton) convertView.findViewById(R.id.listDownloadBtn);
    42.  
    43.         listDownloadBtn.setOnClickListener(new View.OnClickListener() {
    44.             @Override
    45.             public void onClick(View v) {
    46.                 String song_name = songs.getName();
    47.                 String song_url = songs.getUrl();
    48.                 System.out.println("Download Clicked");
    49.                 Album_details ad = new Album_details();
    50.                 ad.downloadFileFromNet(song_url,song_name);
    51.             }
    52.         });
    53.         return convertView;
    54.     }
    [​IMG]

    Any help will be appreciated. Thank you in advance..
     

    Advertisement

  2. pabhishek

    pabhishek Lurker
    Rank:
    None
    Points:
    5
    Posts:
    2
    Joined:
    Oct 9, 2017

    Oct 9, 2017
    2
    0
    5
    Hi,
    You need to write if condition on button click and then check whether the songs is being played or not and then if not being played then action is to be performed

    This way you will avoid playing multiple tracks at once.

    A simple solution may be useful for u make a boolean variable and check whether if it is true then play or else not

    From
    abhi
     
    #2 pabhishek, Oct 9, 2017
    Last edited by a moderator: Oct 9, 2017
  3. Subhomoy Goswami

    Thread Starter
    Rank:
    None
    Points:
    15
    Posts:
    3
    Joined:
    Oct 9, 2017

    Oct 9, 2017
    3
    0
    15
    Thank you for your reply but I think you have not read my question. The music is played as usual. There is no problem with the music player. only the button is not going back to previous state.

    let me give an example,

    there are many items in the list and in one item u pressed the play button. The songs starts and the button changes from play to pause. Now if you click the play button of some other list, the new song gets started and the button also changes from play to pause. but the previous button also remains in the pause state, which should return in the previous state. i,e play..

    Understood now my friend?
     

Share This Page

Loading...