0

現在、私は音楽プレーヤー アプリケーションに取り組んでおり、ボタンのテキストを変更したいと考えています。

これが私のコードです。playM は「再生」ボタンであり、stopM は停止するためのボタンです。ロジックは再生ボタンが押されたときであり、playM のテキストは「再生」(playM のデフォルトのテキストは「再生」) から「一時停止」に変わります。その後、stopM を押すと、playM が「一時停止」から「再生」に変わります。

ただし、実際の動作は、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;

    }
}
4

2 に答える 2

1

あなたはアダプターにいるので、そのようなアイテムビューを保存することはできません.

最速ではありますが、おそらく最善ではない解決策はfinal、リスナーの正しいボタンにアクセスできるように playM を設定することです。

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;

}
}
于 2013-11-13T10:07:58.127 に答える