Home » Android » How to change image of ImageView inside custom adapter when click button on main activity?

How to change image of ImageView inside custom adapter when click button on main activity?

Posted by: admin November 1, 2017 Leave a comment

Questions:

Now, I set image for each item listView from MainActivity class

private void setBackground(int position, int drawable){
    ViewListItem item = adapter.getItem(itemID);

    switch (position){
        case 0:
            item.ivPlayerAnswer1 = (ImageView)findViewById(R.id.ivPlayerAnswer1);
            item.ivPlayerAnswer1.setImageResource(drawable);
            break;
        case 1:
            item.ivPlayerAnswer2 = (ImageView)findViewById(R.id.ivPlayerAnswer2);
            item.ivPlayerAnswer2.setImageResource(drawable);
            break;
        case 2:
            item.ivPlayerAnswer3 = (ImageView)findViewById(R.id.ivPlayerAnswer3);
            item.ivPlayerAnswer3.setImageResource(drawable);
            break;
        case 3:
            item.ivPlayerAnswer4 = (ImageView)findViewById(R.id.ivPlayerAnswer4);
            item.ivPlayerAnswer4.setImageResource(drawable);
            break;
    }
}

onClick method to get the button clicked will be set background for image of position (image1: position = 0, image2: position = 1, …)

@Override
public void onClick(View view) {
    switch (view.getId()){
        case R.id.ivChoice1:
            setBackground(position, intRes[0]);
            break;
        case R.id.ivChoice2:
            setBackground(position, intRes[1]);
            break;
        case R.id.ivChoice3:
            setBackground(position, intRes[2]);
            break;
        case R.id.ivChoice4:
            setBackground(position, intRes[3]);
            break;
        case R.id.ivChoice5:
            setBackground(position, intRes[4]);
            break;
        case R.id.ivChoice6:
            setBackground(position, intRes[5]);
            break;
    }
    position++; //postion (0, 1, 2, 3...) of each image on item of listview (position is from left to right)
    if(position > 3){
        itemID++; //id of listview 0, 1, 2, 3...
        position = 0;
    }
}

getView method like below:

@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = ((Activity) context).getLayoutInflater();

    ViewListItem item; //item of listview
    if(view==null)
    {
        view = inflater.inflate(R.layout.item_list, null);
        item = new ViewListItem(this.context);

        item.ivSuggestAnswer1 = (ImageView)view.findViewById(R.id.ivSuggestAnswer1);
        item.ivSuggestAnswer2 = (ImageView)view.findViewById(R.id.ivSuggestAnswer2);
        item.ivSuggestAnswer3 = (ImageView)view.findViewById(R.id.ivSuggestAnswer3);
        item.ivSuggestAnswer4 = (ImageView)view.findViewById(R.id.ivSuggestAnswer4);
        item.ivPlayerAnswer1 = (ImageView)view.findViewById(R.id.ivPlayerAnswer1);
        item.ivPlayerAnswer2 = (ImageView)view.findViewById(R.id.ivPlayerAnswer2);
        item.ivPlayerAnswer3 = (ImageView)view.findViewById(R.id.ivPlayerAnswer3);
        item.ivPlayerAnswer4 = (ImageView)view.findViewById(R.id.ivPlayerAnswer4);

        view.setTag(item);
    } else {
        item = (ViewListItem)view.getTag();
    }

    return view;
  }
}

This only set for first item listView, the other item of listView can not set for them.

Answers: