在我正在使用的 android 应用程序中Listview
,它将具有按钮和 textview。在这里,我正在更改按钮单击时的按钮图标。我已将 customAdapter 用于列表视图。更改按钮图标的功能运行良好。
问题是,当我滚动列表视图时,其他按钮的图标也会改变。我不知道我的代码有什么问题。请帮我解决这个问题。
自定义适配器:
公共类 CustomListViewAdapter_ringtone 扩展 ArrayAdapter {
Context context;
public static MediaPlayer mp = null;
int btnId;
String rowTag;
View previouView = null;
ListView mListView;
public CustomListViewAdapter_ringtone(Context context, int resourceId,
List<RowItem_ringtone> items, ListView lst) {
super(context, resourceId, items);
this.context = context;
this.mListView = lst;
}
private class ViewHolder {
TextView txtTitle;
ImageButton btnPlay;
ImageButton btnSet;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final RowItem_ringtone rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.ringtone_row, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView
.findViewById(R.id.ringtoneTitle);
holder.btnPlay = (ImageButton) convertView
.findViewById(R.id.btnPlay);
holder.btnSet = (ImageButton) convertView.findViewById(R.id.btnSet);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtTitle.setText(rowItem.getRingTitle());
holder.btnPlay.setTag(rowItem.getRingId());
holder.btnSet.setTag(rowItem.getRingId());
holder.btnPlay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (CustomListViewAdapter_ringtone.mp != null) {
if (CustomListViewAdapter_ringtone.mp.isPlaying()) {
CustomListViewAdapter_ringtone.mp.stop();
CustomListViewAdapter_ringtone.mp.release();
CustomListViewAdapter_ringtone.mp = null;
}
}
if (previouView != null) {
if (previouView != v) {
previouView.setBackgroundResource(R.drawable.play_icon);
v.setBackgroundResource(R.drawable.pause_icon);
CustomListViewAdapter_ringtone.mp = MediaPlayer.create(
getContext(),
Integer.parseInt(v.getTag().toString()));
CustomListViewAdapter_ringtone.mp.start();
previouView = v;
} else {
previouView.setBackgroundResource(R.drawable.play_icon);
previouView = null;
}
} else {
CustomListViewAdapter_ringtone.mp = MediaPlayer.create(
getContext(),
Integer.parseInt(v.getTag().toString()));
CustomListViewAdapter_ringtone.mp.start();
v.setBackgroundResource(R.drawable.pause_icon);
previouView = v;
}
}
});
holder.btnSet.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name = rowItem.getRingTitle();
name = "one_direction_" + name.toLowerCase().replace(" ", "_");
Uri path = Uri.parse("android.resource://"
+ getContext().getPackageName() + "/raw/" + name);
RingtoneManager.setActualDefaultRingtoneUri(getContext(),
RingtoneManager.TYPE_RINGTONE, path);
RingtoneManager.getRingtone(getContext(), path).play();
Toast.makeText(getContext(), name + " is set as ringtone.",
Toast.LENGTH_LONG);
}
});
return convertView;
}
}