Reputation: 3723
I have an application with a custom list view which has a textview and an image(delete), when I click the image the background color of that row should change and when I click the same image again its background should change to default color. I'm able to change the background color but only once, Im not able to change it twice, I mean Im not able to revert back to its default color.
Here is my code ...
CustomListView.java
public View getView(final int position, View convertView, ViewGroup parent) {
holder = null;
DataFields rowItems = (DataFields) getItem(position);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.home_field_row, null);
holder = new ViewHolder();
holder.dataFields = items.get(position);
holder.mName = (TextView) convertView
.findViewById(R.id.hmFieldName);
holder.mDeleteImage = (ImageView) convertView
.findViewById(R.id.hmFieldDeleteImage);
holder.mDeleteCheck = (ImageView) convertView
.findViewById(R.id.hmFieldDeleteCheck);
holder.mDeleteMainRL = (RelativeLayout) convertView
.findViewById(R.id.hmFieldMainRL);
holder.mDeleteImage.setTag(position);
final View clickView = convertView;
holder.mDeleteImage
.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View v) {
int status = 0;
HomeActivity.mDeleteFieldLL
.setVisibility(View.VISIBLE);
HomeActivity.hmAddField
.setVisibility(View.INVISIBLE);
holder.dataFields = items.get(position);
if (mFieldId.size() == 0) {
mFieldId.add(holder.dataFields);
++count;
HomeActivity.hmDeleteSelected
.setText("Delete (" + count + ")");
clickView.setBackgroundColor(R.color.list_row_bg);
} else {
for (int i = 0; i < mFieldId.size(); i++) {
if (mFieldId.get(i).getId() == holder.dataFields
.getId()) {
status = 1;
}
}
if (status == 0) {
mFieldId.add(holder.dataFields);
++count;
HomeActivity.hmDeleteSelected
.setText("Delete (" + count + ")");
clickView.setBackgroundColor(R.color.list_row_bg);
} else if (status == 1) {
mFieldId.remove(holder.dataFields);
--count;
if (count < 0)
count = 0;
clickView.setBackgroundColor(R.color.list_row_bg_default); //doesnt changes back to default color
HomeActivity.hmDeleteSelected
.setText("Delete (" + count + ")");
}
}
}
});
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.mName.setText(rowItems.getName());
return convertView;
}
And one more problem is, that background color is not the color I mentioned in color.xml, I tested by putting different colors but when I click the image that color is changing to only one particular color.
So, to put it simple, I need to change the background color of listview row when I click the image and revert back to the default color when I click it again.
Any kind of help is much appreciated. Thanks !
Upvotes: 0
Views: 492
Reputation: 13348
I think the color may already changed, but you make mistake at setBackgroundColor(), so it seem like the color is not changing.
For the color problem try using this code
clickView.setBackgroundColor(context.getResources().getColor(R.color.list_row_bg));
You have to pass Context object when instantiate adapter.
or you can use something like
layout.setBackgroundColor(0xFFFFFFFF);
Upvotes: 1