i have a gridview, when an item is clicked. it will iterate through a list and it should remove an object from the list when it matches a string. here’s my code so far but it doesn’t work. i don’t know what is wrong. pls help.
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
String sel_id = list2.get(arg2).stud_id.toString();
for(int i=0; i<late_list.size(); i++)
{
if(late_list.get(i).stud_id.toString() == sel_id.toString())
{
late_list.remove(i);
}
}
}
});
Don’t compare String with
==, useequals()==checks if the operands reference the same object instance (only checks the same value for primitive types)equals()checks for same object contents (for classes that override theequalsmethod, andStringcertainly does).Consequently your
ifcondition must beAlso, there is no need to call
toString()on aStringobject, as you did onsel_idIf your
stud_idis aStringalso, you can write