目前我正在开发音乐播放器应用程序,我想更改按钮的文本。
这是我的代码。 playM是一个“播放”按钮,而stopM是停止的,逻辑是当按下播放按钮时,playM的文本从“Play”(playM的默认文本是“Play”)变为“Pasue”。然后当按下stopM时,playM从“Pause”变为“Play”
然而,实际行为是当我点击playM是从播放到暂停的更改文本但是当我点击stopM时,playM不会从暂停变为播放。如何解决问题?谢谢
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;
}
}
答案 0 :(得分: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;
}
}
答案 1 :(得分:0)
请尝试使用无效进行更改。
playM.setText("Play");
playM.invalidate();