0

In my android application, I am using Listviewand each row of listview will have textview and play/pause button. Here I am displaying audio file in each list row.When user click play button, audio file will start and the icon of play button will be changed to pause .If i click same button again, it will stop that audio file and icon of pause button will be to play.

The problem is that if i am accessing the play button of 1st row and i click on play button of another row, then icon of 1st row should change.I don't know how to achieve this functionality. Please help me to solve this problem.

getView() method of customAdapter :

  public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        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

                Toast.makeText(getContext(), v.getTag().toString(),
                        Toast.LENGTH_LONG).show();
                if (CustomListViewAdapter_ringtone.mp != null) {
                    if (CustomListViewAdapter_ringtone.mp.isPlaying()) {
                        CustomListViewAdapter_ringtone.mp.stop();
                        CustomListViewAdapter_ringtone.mp.release();
                    }
                }
                mp = MediaPlayer.create(getContext(),
                        Integer.parseInt(v.getTag().toString()));
                mp.start();
                v.setBackgroundResource(R.drawable.set_icon);
                btnId = Integer.parseInt(v.getTag().toString());
            }
        });
return convertView;
    }
2
  • 2
    post you code.. what have you done so far Commented Aug 6, 2013 at 9:28
  • But i will suggest instead of using click listener inside your custom adapter; use on list item click listener inside your activity where you are displaying your list view. Commented Aug 6, 2013 at 10:16

4 Answers 4

1

Just store a reference to the last button that was clicked in your OnListItemClick method.

public class Something {
    private CustomListItem previouslyClickedListItem;

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        CustomListItem item = (CustomListItem) view;
        if (item != previouslyClickedListItem) {
            // Set previouslyClickedListItem to not clicked
            prevouslyClickedListItem = item;
            // Set item to clicked
        } else {
            // Set prevouslyClickListItem to not clicked
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

On List view item click listener you are changing icon right? so there you get id for row also . So you can store that id in variable as currently playing row or file. And when you click on another row just check that id and change icon according to that.

Use like this

yourlistview.setOnItemClickListener(new OnItemClickListener()
{
    @Override public void onItemClick(AdapterView<?> arg0, View arg1,int position, long         arg3)
{ 
    Toast.makeText(SuggestionActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});

This sample code works for me:

View view = null;

 private OnItemClickListener onItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            if(view != null)
            {
                TextView count = (TextView) view.findViewById(R.id.downloadCount);
                count.setText("007");
            }
            view = arg1;

        }
    };

8 Comments

but how i will get id of that row?
when first time you click on row you will get id of that row so you can save it into temp variable and next time on click of another row you just check that temp id and according to that change your ui for that row
i got your point. but next time if i click on another row then icon of previously accessed row should be changed,too. How to get this functionality ?
I know how to get id of previously accessed row. But my question is that, how to access previous row from current row ?
congo... :) Happy coding
|
0

You must be using the custom adapter of listview and your code must be inside the getView method.

In this method, after the findviewbyid, just assign tags to the buttons.

eg, btn.setTag(position);

and then when you click the play button, just get that tag. e.g, int clickedposition = (Intger)btn.getTag();

You will now be able to change the icon of that particular row only.

Comments

0

Below is The Code Snippet for your salvation. I used toggle button for e.g. you can change accordingly

public class CustomListviewAdapter extends BaseAdapter {

  private View PreviousView=null;

  public View getView(final int position, View convertView, final ViewGroup parent) {

  togglebutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

        View CurrentView = v;
          if(PreviousView != null && PreviousView != CurrentView){
             ToggleButton tb=(ToggleButton)PreviousView.findViewById(R.id.togglebutton);
             tb.setChecked(false);
            }

          PreviousView=CurrentView;
         }
    });
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.