2

I have a list adapter which sets onClick listeners for ImageButtons when populating a list view. I'm trying to make it so the image resource changes onClick. I'm having trouble retrieving the image resource in the onClick listener.

List Adapter:

public class ListAdapter extends BaseAdapter {

Context context;
protected List<Post> cityFeed;
LayoutInflater inflater;

public ListAdapter(Context context, List<Post> cityFeed) {
    this.cityFeed = cityFeed;
    this.inflater = LayoutInflater.from(context);
    this.context = context;
}

public int getCount() {
    return cityFeed.size();
}

public Post getItem(int position) {
    return cityFeed.get(position);
}

public long getItemId(int position) {
    return cityFeed.get(position).getDrawableID();
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        // inflate the list item
        convertView = this.inflater.inflate(R.layout.row_layout, parent, false);            
        // get views 
        holder.profilePic = (ImageView) convertView.findViewById(R.id.profilePic);
        holder.username = (TextView) convertView.findViewById(R.id.username);
        holder.day = (TextView) convertView.findViewById(R.id.day);
        holder.rating = (TextView) convertView.findViewById(R.id.rating);
        holder.textPost = (TextView) convertView.findViewById(R.id.textPost);
        holder.ratingUp = (ImageButton) convertView.findViewById(R.id.ratingUp);
        holder.ratingDown = (ImageButton) convertView.findViewById(R.id.ratingDown);
        convertView.setTag(holder);

        holder.ratingUp.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View convertView) {
                // if image source equals this drawable then...
                // else change the image source to this drawable...
                Toast.makeText( context,
                        "Rate Up",
                        Toast.LENGTH_SHORT).show();                 
            }
        });

        holder.ratingDown.setOnClickListener(new OnClickListener() {                    
            @Override
            public void onClick(View convertView) {
                Toast.makeText( context,
                        "Rate Down",
                        Toast.LENGTH_SHORT).show();
            }
        });     

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    Post post = cityFeed.get(position);
    holder.profilePic.setImageResource(post.getDrawableID());
    holder.username.setText(post.getUsername());
    holder.day.setText(post.getDay());
    holder.rating.setText(post.getRating());
    holder.textPost.setText(post.getText());
    return convertView;
}

private class ViewHolder {
    ImageView profilePic;
    TextView username;
    TextView day;
    TextView rating;
    TextView textPost;
    ImageView ratingUp;
    ImageView ratingDown;
}

The toasts work fine but I can't figure out how to handle the image resources. See onClick listeners in getView method. Thanks in advance.

4
  • I don't understand what you mean - what do you want to happen, for example, when a user clicks ratingUp? Commented Oct 30, 2014 at 0:51
  • change the image resource to a different one, if it's not already changed. Commented Oct 30, 2014 at 1:15
  • are these static resources or does the second resource depend on what the first one is? Commented Oct 30, 2014 at 3:36
  • The latter. If the button is not pressed, then change the drawable on click. If the button is already pressed, do nothing on click. Commented Oct 30, 2014 at 3:42

2 Answers 2

4
Drawable myDrawable1= //get your drawable 1 here    
Drawable myDrawable2= //get your drawable 2 here
if ((Post)getItem(pos).isRatedUp){
      holder.ratingUp.setImageDrawable(myDrawable2);
}
else{
      holder.ratingUp.setImageDrawable(myDrawable1);
}

holder.ratingUp.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View convertView) {
                    // if image source equals this drawable then...
                    // else change the image source to this drawable...

                    if(((ImageButton)convertView).getDrawable()==myDrawable1){
                           ((ImageButton)convertView).setImageDrawable(myDrawable2);
                           ((Post)getItem(pos)).setRatedUp(true);
                    }else{
                           ((ImageButton)convertView).setImageDrawable(myDrawable1);
                           ((Post)getItem(pos)).setRatedUp(false);
                    }
                }
            });

Add this to your Post.class;

private boolean isRatedUp=false;

public boolean isRatedUp(){
    return this.isRatedUp;
}
public void setRatedUp(boolean israted){
    this.isRatedUp=israted;
}
Sign up to request clarification or add additional context in comments.

5 Comments

I had to make the drawables final. For some reason it takes 2 clicks to make the drawable change (only the first time, then after that it works with one click). And when it changes the first time, every 10th list item changes too! It's a good start though, thanks! I can debug from here.
oh by the way, every 10th item is changing because of recycling. If you can modify Post.class you may add methods to save item status. I ve edited my answer, take a look at edits
Awesome. I'll vote your answer up once I reach 15 rep :P
Hi I know this is an old post, wildebeest but I am currently having the two clicks for an imagebutton in listview issue. After the first two clicks it works exactly how it should. Did you ever find a way to get it working properly?
it probably has something to do with states of your listview items / buttons. Assuming that state is "1": when you click it goes to state "1" , and after second click it changes its state to "2". do you save state of items?
1
Drawable drawable1 =getContext().getResources().getDrawable(R.drawable.add);
Drawable drawable2 =getContext().getResources().getDrawable(R.drawable.checked);

If anyone wants to know, How did I solve the double click problem, I just declared and initialized both of the image drawables as global variables. I do not know why it worked, but it did. I don't have enough reputation for comment, so I am giving it as an answer. Before that I tried changing xml states and all that, but none worked.

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.