【问题标题】:setText is working for the first time only in androidsetText 首次仅在 android 中工作
【发布时间】:2013-11-13 09:59:12
【问题描述】:

目前我正在开发一个音乐播放器应用程序,我想更改按钮的文本。

这是我的代码。 playM 是“播放”按钮,stopM 是停止,逻辑是当按下播放按钮时,playM 的文本从“播放”(playM 的默认文本为“播放”)变为“暂停”。然后当stopM被按下时,playM从“Pause”变为“Play”

但是,实际行为是当我点击 playM 时将文本从 Play 更改为 Pause,但是当我点击 stopM 时 playM 并没有从 Pause 更改为 Play。如何解决问题?谢谢

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;

    }
}

【问题讨论】:

    标签: android android-layout android-listview settext


    【解决方案1】:

    你在一个适配器中,所以你不能像那样保存项目视图。

    最快但可能不是最好的解决方案是将 playM 设置为 final 以便您能够访问侦听器中的右键。

    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;
    
    }
    }
    

    【讨论】:

    • 比我还快...!这就是答案;)
    • 谢谢,还有一个问题,如果我使用 ListView 来实现一个歌曲列表,每一行对应一首歌曲,如何确保我点击的按钮实际上是停止/播放该行? (例如,我按下 row1 中的播放按钮,然后按下 row2 中的停止按钮......)
    • 好吧,我猜你想要正确的“p”实例。为播放按钮定义一个局部最终变量,而不是跟踪它。或者您也可以将 position 定义为 final 并使用侦听器中的 getItem() 。编辑:我更新了代码。
    • Sorry.confusing Ptype 因为它应该是一个 Song 对象,将 p 从 Song 更改为 Ptype 后我该怎么办?谢谢
    • 应该成功了。仅此而已,现在在 OnClickListener 中,您使用的是在同一个 getView(...) 调用中检索到的 Song 对象。
    【解决方案2】:

    请尝试使更改无效。

     playM.setText("Play");
     playM.invalidate();
    

    【讨论】:

    • 谢谢。试过但不工作。 stopM 功能(停止服务)在起作用,但只有 setText 部分不起作用
    • 那是因为您正在更改适配器中“随机”视图的文本。像这样在适配器中保留列表项视图是错误的,因为每次创建/绑定项目时它都会更改
    猜你喜欢
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多