0

enter image description here

I have one list field I my demo .In that list field I have number of rows .Each rows have textview and image buttons .I want to change the background of image button when user click on image button . In other words there is favourite icon is present in each row .I want to change the background image if user click on image and if is selected it become favourite and if user again select the selected image it become deselected. I apply the concept but it works fine for first item .But fail in other item ..

I will explain more I take one variable boolean isPressed=false; when I pressed Image button it become true and image will change .Again if I select the same Item it works fine .But when I select the different item of row it fail . because boolean isPressed= become true; what is best way to save the state of each Image button .

here is my code

public class CustomAdapter extends BaseAdapter implements View.OnClickListener {

    private Activity activity;
    private ArrayList data;
    private static LayoutInflater inflater = null;
    boolean isPressed=false;


    public CustomAdapter(Activity a, ArrayList d) {

        /********** Take passed values **********/
        activity = a;
        data = d;
        inflater = (LayoutInflater) activity.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    }


    @Override
    public int getCount() {
        if (data.size() <= 0)
            return 1;
        return data.size();
    }

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

    @Override
    public void onClick(View v) {



    }

    /*********
     * Create a holder Class to contain inflated xml file elements
     *********/
    public static class ViewHolder {

        public TextView text;

        public ImageButton imageButton;

    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View vi = convertView;
        final ViewHolder holder;

        if (convertView == null) {

            /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
            vi = inflater.inflate(R.layout.row_layout, null);

            /****** View Holder Object to contain tabitem.xml file elements ******/

            holder = new ViewHolder();
            holder.text = (TextView) vi.findViewById(R.id.station_name);

            holder.imageButton = (ImageButton) vi.findViewById(R.id.favorite);
            holder.imageButton.setBackgroundResource(R.drawable.off);

            /************  Set holder with LayoutInflater ************/
            vi.setTag(holder);
        } else
            holder = (ViewHolder) vi.getTag();

        if (data.size() <= 0) {
            holder.text.setText("No Data");

        } else {

            String string = (String) data.get(position);

            /************  Set Model values in Holder elements ***********/

            holder.text.setText(string);

            // this is for overall row click
            vi.setOnClickListener(new View.OnClickListener() {



                @Override
                public void onClick(View v) {
                    Log.d("row is click","row click"+position);
                }
            });
            // this is for image button onclick
            holder.imageButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(isPressed){
                        holder.imageButton.setBackgroundResource(R.drawable.off);
                    }else{
                        holder.imageButton.setBackgroundResource(R.drawable.on);
                    }
                    isPressed = !isPressed; // reverse

                }
            });
            ;


        }
        return vi;
    }
}

row xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:descendantFocusability="blocksDescendants">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/station_name"
        android:padding="10dp"
        android:textColor="#eee345"
        android:textAppearance="?android:textAppearanceLarge"
        />

    <ImageButton android:id="@+id/favorite"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"

        android:background="#00ffffff"
     />


</LinearLayout>

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#325633"
   >

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list_view">
    </ListView>

</LinearLayout>

fragment.java

public class Fragmentone  extends Fragment{

    ArrayList<String> name;
    boolean isPressed=false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        name=new ArrayList<String>();
        name.add("First Station");
        name.add("Second Station");

        ListView listView = (ListView) view.findViewById(R.id.list_view);

        CustomAdapter customAdapter =new CustomAdapter(getActivity(),name);
        listView.setAdapter(customAdapter);



        return view;
    }


}
Iamat8
  • 3,888
  • 9
  • 25
  • 35
user944513
  • 12,247
  • 49
  • 168
  • 318

3 Answers3

2

You can use getTag() and setTag(Object tag) methods of a view to handle their individual states. Be sure to handle getTag() properly, take care of casting to boolean and handling null returns (for default value).

From the documentation,

Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure

Edit: An even easier method is to use a selector drawable for the image button background, so then depending on the selection state you can change its image. See this answer to learn how to do this. Also check out the relevant documentation.

Community
  • 1
  • 1
Santhosh
  • 3,781
  • 2
  • 14
  • 11
1
  • Create class for your item
  • and a boolean field for pressed
  • create adapter for that class

check also here

Community
  • 1
  • 1
gmetax
  • 3,853
  • 2
  • 31
  • 45
1

You only have a single isPressed variable for your entire ListView, so it's being shared by all of your rows. One solution would be to use a ToggleButton, and a custom state list drawable for the background. This will save you the hassle of manually tracking the toggled state, looking up drawable resources, etc...