Search code examples
androidadapterbuttonclick

Updating Custom Adapter Content On Inner-Button Click


So I'm trying to be able to change the look of a List Item from my Custom Adapter that I made on the fly. I'd like it to change when I click a Button from within the List Item of my Custom Adapter. This is an example of a Custom List Item looks like. When I click on the Button, I want it to perform an action to modify the layout. For example, after clicking Button, I want it to hide another button, or change the text of the button. For some reason, these commands seem to have no effect when clicking them.

My ultimate goal is to be able to resize the List Item on a button click. But as I mentioned, nothing seems to take effect. Setting visibility outside of the onClick method seems to work, but doing it inside doesn't. I know the code is being reached, but I believe it has to do with the adapter not updating, but I have the notifyDataSetChanged method in there. Any help is appreciated. Thanks.

Here is my Custom Adapter code

class CustomAdapter extends ArrayAdapter<Player> {
Button button, button2;

public CustomAdapter(Context context, ArrayList<Player> players) {
    super(context, R.layout.player_expanded, players);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater infater = LayoutInflater.from(getContext());
    final View customView = infater.inflate(R.layout.custom_list_item, parent, false);

    button = (Button) customView.findViewById(R.id.button);
    button2 = (Button) customView.findViewById(R.id.button2);
    button2.setVisibility(View.VISIBLE);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            button2.setVisibility(View.GONE);   //Doesn't take effect
            notifyDataSetChanged();
        }
    });

    return customView;
  }
}

And my Custom_List_Item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/textView3"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:layout_alignParentTop="true"
    android:focusable="false"  
    android:id="@+id/button"  />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:focusable="false"
    android:id="@+id/button2" />
 </LinearLayout>

Solution

  • Try as below:

    class CustomAdapter extends ArrayAdapter<Player> {
    
    private static class ViewHolder {
        Button button, button2;
    }
    
    public CustomAdapter(Context context, ArrayList<Player> players) {
    super(context, R.layout.player_expanded, players);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder; // view lookup cache stored in tag
       if (convertView == null) {
          viewHolder = new ViewHolder();
          LayoutInflater infater = LayoutInflater.from(getContext());
    final View customView = infater.inflate(R.layout.custom_list_item, parent, false);
    
    viewHolder.button = (Button) customView.findViewById(R.id.button);
    viewHolder.button2 = (Button) customView.findViewById(R.id.button2);
    
    viewHolder.button.setTag(position);
    viewHolder.button2.setTag(position);
    convertView.setTag(viewHolder);
    } else {
           viewHolder = (ViewHolder) convertView.getTag();
    }
    viewHolder.button2.setVisibility(View.VISIBLE);
    viewHolder.button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            viewHolder.button2.setVisibility(View.GONE);   //Doesn't take effect
            notifyItemChanged(Integer.valueOf(v.getTag().toString()));
        }
    });
    
    return customView;
    }
    }