Reputation: 67
My code has a ListView of notifications and when the user clicks on the notifications, its color changes. But my problem is that when the user clicks on the 1st row, another row color is also changing automatically same as for other rows.
notiId.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adpterView, View view, int position,
long id) {
String sr_msg=arrayList.get(position);
String sr_title= list_tittle.get(position);
ArrayList<String> arr_intent = new ArrayList<String>();
arr_intent.add(sr_msg);
arr_intent.add(sr_title);
Intent intent = new Intent(getActivity(), NotiPerList.class);
Bundle args = new Bundle();
args.putSerializable("NOTI", (Serializable) arr_intent);
intent.putExtra("NOTI",arr_intent );
startActivity(intent);
String yn=arr2.get(position);
String s2= arr3.get(position).toString();
notiId.getChildAt(position).setBackgroundColor(Color.GRAY);
}
}
EDIT
@Override
public View getView(int position, View v, ViewGroup parent)
{
View mView = v ;
if(mView == null) {
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(id, null);
}
listView=(ListView)mView.findViewById(R.id.notiID);
String sd= yORn.get(position);
if(sd.equals("Y")){
mView.setBackgroundColor(Color.GRAY);
}
}
Upvotes: 0
Views: 45
Reputation: 1385
Remove this line
notiId.getChildAt(position).setBackgroundColor(Color.GRAY);
If your list has multiple selection then
Upvotes: 0
Reputation: 10871
that's how ListView
works. It is reusing views that go out of screen. Instead of changing view directly, you have to tell Adapter
to change the color of specific index view.
Something like this:
@Override
public void onItemClick(AdapterView<?> adpterView, View view, int position,
long id) {
...
yourAdapter.setHighLightedViewIntex(position);
yourAdapter.notifyDataSetChanged();
}
And in Adapter
:
getView(...) {
if(highlightedPosition == position) {
//set background to highlighted
} else {
//set background to regular
}
}
Upvotes: 1