1

I wanna to implement multiple button in my ListView items. But the problem is that i couldn't handle click event when button is clicked, i mean they are Play button and Image button like this

enter image description here

Here is my Adapter:

public class ArtistsAdapter extends BaseAdapter implements SectionIndexer {

private HashMap<String, Integer> alphaIndexer;
private String[] sections;
private LayoutInflater inflater;
private int resId;
private Context context;
private ArrayList<Artist> artistList;

public ArtistsAdapter(Context context, int resId, ArrayList<Artist> artistList) {
    this.resId = resId;
    this.context = context;
    this.artistList = artistList;
    this.inflater = LayoutInflater.from(context);
}

@Override
public Object[] getSections() {
    return sections;
}

@Override
public int getPositionForSection(int section) {
    return alphaIndexer.get(sections[section]);
}

@Override
public int getSectionForPosition(int position) {
    return 1;
}

private static class ViewHolder {
    ImageView artistImgItem;
    ImageButton artistPlayItem;
    TextView artistNameItem;
    TextView artistAgeItem;
    LinearLayout artistItem;
}

@Override
public int getCount() {
    return artistList.size();
}

@Override
public Object getItem(int position) {
    return artistList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    Artist artist = (Artist) getItem(position);
    ViewHolder viewHolder;
    if (convertView == null) {
        convertView = this.inflater.inflate(resId, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.artistImgItem = (ImageView) convertView.findViewById(R.id.artistImg);
        viewHolder.artistPlayItem = (ImageButton) convertView.findViewById(R.id.artistPlay);
        viewHolder.artistNameItem = (TextView) convertView.findViewById(R.id.artistName);
        viewHolder.artistAgeItem = (TextView) convertView.findViewById(R.id.artistAge);
        viewHolder.artistItem = (LinearLayout) convertView.findViewById(R.id.artistItem);
        convertView.setTag(viewHolder);
    } else viewHolder = (ViewHolder) convertView.getTag();
    viewHolder.artistNameItem.setText(artist.getName());

    viewHolder.artistPlayItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.i("aaaaa", position+"ssss");
            Toast.makeText(context, "Music playin " + position+1 +"", Toast.LENGTH_SHORT).show();
        }
    });
    viewHolder.artistItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((ArtistsActivity) context).showArtistView();
        }
    });
    viewHolder.artistImgItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((ArtistsActivity) context).showArtistView();
        }
    });
    return convertView;
}

}

And my activity looks like that

public class ArtistsActivity extends Activity {

private IndexableListView artistListUI;
private ImageView maleArtist, femaleArtist, groupArtist, artistImg;
private ArtistsAdapter artistsAdapter;
private ArrayList<Artist> maleArtistList, femaleArtistList, groupArtistList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.artist_list);

    artistListUI = (IndexableListView) findViewById(R.id.artistListView);
    maleArtist = (ImageView) findViewById(R.id.maleArtist);
    femaleArtist = (ImageView) findViewById(R.id.femaleArtist);
    groupArtist = (ImageView) findViewById(R.id.groupArtist);
    artistImg = (ImageView) findViewById(R.id.artistImg);

    artistListUI.setFastScrollEnabled(true);     

    artistsAdapter = new ArtistsAdapter(ArtistsActivity.this, R.layout.artist_item, artistList);
    artistListUI.setAdapter(artistsAdapter);



public void showArtistView() {
    startActivity(new Intent(ArtistsActivity.this, AlbumsActivity.class));
}

}

2
  • Please post your code for where you call the listview adapter (Artists Adapter). That is where you need to handle the button clicks. Commented Sep 13, 2014 at 13:44
  • 1
    This issue is acctually concerned with IndexableListView.Here this issue is solved. github.com/gabrielemariotti/cardslib/issues/70 Commented Sep 15, 2014 at 5:34

2 Answers 2

1
 you have to set clickable property of listview to false
 because of entire listview click is fire
Sign up to request clarification or add additional context in comments.

1 Comment

That is not necessary. You can have buttons and have the listview to be clickable.
0

This is resolved in the adapter. Look at this example:

https://github.com/m-alcu/SkoltiAlert/blob/master/src/com/alcubier/skoltialert/tracks/TrackAdapter.java

In the bindView or getView method you have to define your clickhandler linked to your view:

holder.hLayout.setOnClickListener(new MentionClickhandler());

And do sour stuff in this function:

public class MentionClickhandler implements View.OnClickListener 
{
public void onClick( View view ){

    Track trackTag = new Track();
    trackTag = (Track) view.getTag();
    Log.i(TAG, "item: "+trackTag.getSearchTrack());

    Bundle bundle = new Bundle();
    bundle.putInt("idTrack", trackTag.getIdTrack());
    bundle.putString("searchTrack", trackTag.getSearchTrack());
    bundle.putInt("topTrack", trackTag.getTopTrack());
    bundle.putLong("lastId", trackTag.getLastId());
    bundle.putInt("count", trackTag.getCount());
    bundle.putString("type", "LastMentions");

    Intent i = new Intent(context, GDMentionsList.class);
    i.putExtras(bundle);
    context.startActivity(i);

}
}

Hope it helps...

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.