Search code examples
androidandroid-layoutandroid-listviewsettext

setText is working for the first time only in android


Currently I am working on a music player application and I would like to change the text of a button.

Here is my code . playM is a "Play" button while stopM is to stop, the logic is when the play button is pressed , the text of playM change from "Play"(the default text of playM is "Play") to "Pasue". Then when the stopM is pressed , the playM change from "Pause" to "Play"

However, the actual behavior is when I hit playM is change text from Play to Pause but when I hit stopM the playM does not change from Pause to Play. How to fix the problem ? Thanks

public class Adapter extends ArrayAdapter<Song> {
    Button playM = null;
    Button stopM = null;

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = convertView;

        if (v == null) {

            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.list_view, null);

        }

        p = items.get(position);

        if (p != null) {

            TextView tt = (TextView) v.findViewById(R.id.textView1);
            tt1 = (TextView) v.findViewById(R.id.textView2);
            Button del = (Button) v.findViewById(R.id.button1);
            playM = (Button) v.findViewById(R.id.button2);
            stopM = (Button) v.findViewById(R.id.button3);

            String title = p.getTitle();
            String singer = p.getSinger();

            if (tt != null) {
                tt.setText(title);
            }
            if (tt1 != null) {
                tt1.setText(singer);
            }

            del.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    db.deleteSong(p);
                }
            });

            playM.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //AudioManager manager = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
                    //if (!manager.isMusicActive()) {
                        playM.setText("Pause");
                        Intent svc=new Intent(getContext(), Music.class);
                        svc.putExtra("uri", tt1.getText().toString());
                        getContext().startService(svc);
                    //}
                }
            });

            stopM.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    playM.setText("Play");
                    Intent svc=new Intent(getContext(), Music.class);
                    getContext().stopService(svc);
                }
            });
        }

        return v;

    }
}

Solution

  • You are in an adapter so you can't save item Views like that.

    fastest, but probably not the best, solution is to set playM as final so that you will be able to access the right button in the listener.

    public class Adapter extends ArrayAdapter<Song> {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        View v = convertView;
    
        if (v == null) {
    
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.list_view, null);
    
        }
    
        final Song p = items.get(position);
    
        if (p != null) {
    
            TextView tt = (TextView) v.findViewById(R.id.textView1);
            tt1 = (TextView) v.findViewById(R.id.textView2);
            Button del = (Button) v.findViewById(R.id.button1);
            final Button playM = (Button) v.findViewById(R.id.button2);
            Button stopM = (Button) v.findViewById(R.id.button3);
    
            String title = p.getTitle();
            String singer = p.getSinger();
    
            if (tt != null) {
                tt.setText(title);
            }
            if (tt1 != null) {
                tt1.setText(singer);
            }
    
            del.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    db.deleteSong(p);
                }
            });
    
            playM.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    //AudioManager manager = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
                    //if (!manager.isMusicActive()) {
                        playM.setText("Pause");
                        Intent svc=new Intent(getContext(), Music.class);
                        svc.putExtra("uri", tt1.getText().toString());
                        getContext().startService(svc);
                    //}
                }
            });
    
            stopM.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    playM.setText("Play");
                    Intent svc=new Intent(getContext(), Music.class);
                    getContext().stopService(svc);
                }
            });
        }
    
        return v;
    
    }
    }