Varun Vishnoi
Varun Vishnoi

Reputation: 328

On Click Listener on subview of list item from custom adapter

I am using getview method of baseadapter class for filling the data into listview. And it is working great. I have a relative layout in every listitem through which i am playing a audiofile. I am using onclicklistener in getview method as shown below.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View vi = convertView;

    if (convertView == null)
        vi = inflater.inflate(R.layout.chatbubbles_listrow, null);
    context = parent.getContext();

    TextView date = (TextView) vi.findViewById(R.id.date);
    RelativeLayout playaudio = (RelativeLayout) vi
            .findViewById(R.id.audio_image);
    HashMap<String, String> events = new HashMap<String, String>();
    events = data.get(position);
    final String restype = events.get(Conversation.TAG_USERTYPE);
    audiopath = events.get(Conversation.TAG_AUDIOPATH);
    playaudio.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            stopPlayBack();
            Toast.makeText(context, audiopath, Toast.LENGTH_SHORT).show();
            Log.e("On Click path", audiopath);
            new LoadChats().execute();
        }
    });

     date.setText(restype + audiopath);
    return vi;
}

My listview is showing all information correct as i want but when i click the relative layout then a audio play which is correct to be played but when i click on another relative layout the same audio play again which is wrong. every listitem have is own audio path that must be played but here only one audio play which is played earlier. I know the problem is with onClicklistner of playaudio but I am not getting the problem. It blows up my mind. Please help.

Thanks in advance

Upvotes: 0

Views: 1040

Answers (1)

Charan
Charan

Reputation: 940

Log.e("On Click path", audiopath);

-- is audio path of the view inflated in last.

You need to get the audio path from the clicked view.

playaudio.setTag(audiopath); // Important

    @Override
    public void onClick(View v) {
        stopPlayBack();
        String pathOfAudio = String.valueOf(v.getTag());
        Toast.makeText(context, pathOfAudio, Toast.LENGTH_SHORT).show();
        Log.e("On Click path", pathOfAudio);
        new LoadChats().execute(); // In aynctask use pathOfAudio - path to be played
    }
});

 date.setText(restype + audiopath);
return vi;

}

Upvotes: 2

Related Questions