0

私のAndroidアプリケーションListviewでは、ボタンとテキストビューを持つものを使用しています。ここでは、ボタンクリック時にボタンアイコンを変更しています。リストビューに customAdapter を使用しました。ボタンアイコンを変更する機能は完全に機能しています。

問題は、リストビューをスクロールすると、他のボタンのアイコンも変更されることです。コードの何が問題なのかわかりません。この問題の解決を手伝ってください。

customAdapter :

public class CustomListViewAdapter_ringtone extends 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;
}

}

4

1 に答える 1