2

I am Using Gridview with custom Adapter extending BaseAdapter class in my Activity.

Everything is working fine, we get desired result as we set value.But Problem comes when we click on particular cell of gridview and i want to change color of button in that cell.OnClickListner we changed the color of button like this:

 gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            Toast.makeText(BookingActivity.this, "Hi click working" + position, Toast.LENGTH_SHORT).show();

            try {

                Button button = (Button) view.findViewById(R.id.button3);
                button.setBackgroundColor(Color.GREEN);

            } catch (Exception e) {
                e.printStackTrace();
            }

         }
    });

then color of button has been changed but when we scroll the gridview up then color repeated in another cell. i don't want this color is repeated further. maybe cell is reused but how to avoid this type of repetition.

Here is my Adapter Class

public class CustomGrid extends BaseAdapter {

private Context mContext;
private final ArrayList<String> web;



public CustomGrid(Context c,ArrayList<String> web) {
    mContext = c;
    this.web = web;

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return web.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return web.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid = convertView;

    CoachHolder holder = null;

    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (grid == null) {

        grid = inflater.inflate(R.layout.grid_single,parent,false);


        holder = new CoachHolder(grid);

        grid.setTag(holder);

    } else {
        holder =(CoachHolder)grid.getTag();
    }

    holder.button.setText(web.get(position));



    return grid;
}

static class CoachHolder {


    Button button;


    //TextView descriptions;


    public CoachHolder(View v) {


        button = (Button)v.findViewById(R.id.button3);

    }


}
 }

This is my grid_item.xml file:

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="wrap_content"
  android:layout_height="wrap_content"
 android:background="@android:color/white">
 <Button
    android:layout_width="150dp"
    android:layout_height="100dp"
    android:text="New Button"
    android:id="@+id/button3"
    android:clickable="false"
    android:focusable="false"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
 </RelativeLayout>

i want multiple selection of cell ,when i click on cell particular cell color changed but not repeated. I think i am not able to get particular button of cell using position.

Please Help me to get out of this problems ,it already taken too much time. Thanks in advance for your help

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Anand
  • 429
  • 2
  • 17
  • you should save the position of the button that you have clicked, then load it again when you scroll. take a look at http://stackoverflow.com/a/37041096/5381331. it is about CheckBox, but the idea is same – Linh Oct 07 '16 at 07:05

1 Answers1

0

Try this

  1. Set the color of the cell INSIDE your getView method.
  2. Add a method in your adapter to save the currently selected position
  3. In your onItemClickListener, call saveSelectedItemPosition and then notifyDatasetChanged

Pseudo-code

public void saveSelectedItemPosition(int position){
    currentSelectedPosition = position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// do what you are currently doing
if(position == currentSelectedPosition){
    //change color 
} else {
    //use default color
}




return grid;

}

momoja
  • 938
  • 9
  • 23
  • actually i am getting same issue.. my problem is that when i clicked on that cell or button then color should be changed and not be repeated while scrolling – Anand Oct 07 '16 at 09:20
  • i think my cell is repeat that's why color of cell is being repeated when user click on cell ..please help me with some working solution – Anand Oct 07 '16 at 09:39