Reputation: 8932
I am actually facing a problem that I dont know how to solve...
In fact, I want to change color and display a picture on a listView when I click on it.
The problem is when I click my first item, the 0th , 9th, 18th etc... change too... it's just like the View onItemClick takes only the visible part of the listview (the displayed item, without scroll)
I dont know how to solve it.
My adapter :
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater
.inflate(R.layout.item_anomalies, null);
holder = new ViewHolder();
holder.txtTitre = (TextView) convertView
.findViewById(R.id.titre);
holder.img = (ImageView) convertView
.findViewById(R.id.img);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final AnomalieRowItem rowItem = (AnomalieRowItem) getItem(position);
holder.txtTitre.setText(rowItem.getLibelle());
if (position % 5 == 0) {
holder.img.setBackgroundColor(0xffED695A);
} else {
if (position % 4 == 0) {
holder.img.setBackgroundColor(0xffEB5443);
} else {
if (position % 3 == 0) {
holder.img.setBackgroundColor(0xffEA4735);
} else {
if (position % 2 == 0) {
holder.img.setBackgroundColor(0xffE93825);
} else {
holder.img.setBackgroundColor(0xffD52815);
}
}
}
}
return convertView;
}
My listener :
private void initListener() {
m_ListeAnomalie.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AnomalieRowItem item = m_ListeItem.get(position);
ImageView imgCheck = (ImageView) view.findViewById(R.id.check);
LinearLayout layout = (LinearLayout) view.findViewById(R.id.item);
if (item.isChecked()) {
layout.setBackgroundColor(0xffefefef);
item.setChecked(false);
imgCheck.setVisibility(View.GONE);
} else {
layout.setBackgroundColor(0xffdddddd);
item.setChecked(true);
imgCheck.setVisibility(View.VISIBLE);
}
m_Adapter.notifyDataSetChanged();
}
});
m_Valider.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
end();
}
});
}
Thanks for your help
EDIT : The part with the modulo and the background works fine, my problem is in the listener, it's like it's not the correct view that is injected in the OnItemClick function
EDIT 2 : With a picture, it's better I think
Upvotes: 1
Views: 964
Reputation: 3875
if (position % 5 == 0) {
holder.img.setBackgroundColor(0xffED695A);
} else {
if (position % 4 == 0) {
holder.img.setBackgroundColor(0xffEB5443);
} else {
if (position % 3 == 0) {
holder.img.setBackgroundColor(0xffEA4735);
} else {
if (position % 2 == 0) {
holder.img.setBackgroundColor(0xffE93825);
} else {
holder.img.setBackgroundColor(0xffD52815);
}
}
}
}
// add this lines in getView() after above logic.
if (rowItem.isChecked()) {
layout.setBackgroundColor(0xffefefef);
imgCheck.setVisibility(View.GONE);
} else {
layout.setBackgroundColor(0xffdddddd);
imgCheck.setVisibility(View.VISIBLE);
}
Upvotes: 1