1

I am showing a List in a ListView through a custom adapter SongsListAdapter.java with custom itemview music_item.xml and there is a method i want to call in my Activity Class MainActivity.java so i cannot use setOnClickListener method
what i did is i added an attribute to music_item.xml onClickto call method in MainActivity.java
but this is not working, by clicking the list item it is not invoking the method from Activity class

SongsListAdapter.java

public View getView(final int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.music_item, null);
        this.mView = view;
        holder.title = (TextView) view.findViewById(R.id.songName);
        holder.descr = (TextView) view.findViewById(R.id.songArtists);
        holder.dp = (ImageView) view.findViewById(R.id.albumIcon);
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }
    // Set the results into TextViews
    holder.title.setText(SongsListItemslist.get(position).getDisplayName());
    holder.descr.setText(SongsListItemslist.get(position).getArtist());
    holder.dp.setImageBitmap(SongsListItemslist.get(position).getBitmap());
    // Listen for ListView Item Click
    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
        }
    });
    return view;
}

MainActivity.java

public void songPicked(View view) {
    Log.v("clicked", "clicked");
    musicSrv.setSong(Integer.parseInt(view.getTag().toString()));
    musicSrv.playSong();
}

Music_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/card"
android:clickable="true"
android:onClick="songPicked" >

<ImageView
    android:id="@+id/albumIcon"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/abc_ab_bottom_solid_dark_holo" />
</RelativeLayout>

And i couldn't able to see clicked in my Logs..

4 Answers4

2

Delete this from your SongsListAdapter.java

   view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
        }
    });

you are setting a onClick Listener with an empty action in your adapter that isn´t necessary, use only the method defined in your Layout:

android:onClick="songPicked" 
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
0

You say you cannot use setOnClickListener, I'm not sure why? In your adapter constructor, you can pass in the Activity, set it as a variable in your adapter class and use it in your on click listener to call a method defined in Activity.

Alternatively, use setOnItemClickListener in your activity or fragment (ie where you initially set the adapter).

FreewheelNat
  • 2,907
  • 1
  • 20
  • 12
0

You don't want to set a listener on the Layout of your list item Music_item. This will be covered under the setOnItemClickListener of your ListView.

see: setOnItemClickListener on custom ListView

Community
  • 1
  • 1
Scott
  • 3,663
  • 8
  • 33
  • 56
0

Firstly you have to remove

view.setOnClickListener(new OnClickListener() {

   @Override
       public void onClick(View arg0) {
   }
});

and then if you want ot handle the click on the ListView you need to call this from the MainActivity

    list.setAdapter(yourCustomAdapter);
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Log.v("clicked", "clicked");
            musicSrv.setSong(Integer.parseInt(view.getTag().toString()));
            musicSrv.playSong();

        }
    });

Don't forget to remove onClick attribute from the RelativLayout.

Good luck..

Youcam39
  • 102
  • 5