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