Inside each row of my ListView
, I want a TextView
(which was originally given android:visibility="gone"
in XML) to become visible only if a condition is satisfied.
So I do something like the following pseudocode.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = layoutInflater.inflate(R.layout.row_item, parent, false);
} else {
view = convertView;
}
...
if (ape.getAlpha() != null && ape.getAlpha().equals("RON") ) {
TextView textView = (TextView) view.findViewById(R.id.Item_textView5);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setBackgroundColor(Color.CYAN);
}
});
textView.setVisibility(View.VISIBLE);
}
...
}
Now, since each row is recycled and getView()
is called again, once the (ape.getAlpha() != null && ape.getAlpha().equals("RON"))
condition is satisfied in a row, and thus the textview
's visibility
is set to View.VISIBLE
, it remains visible for the next rows as well.
The question is that how can I reset the visibility of the textview
after setting it to View.VISIBLE
for the next rows for which this row's layout will be recycled, so that the reset visibility to gone
is NOT reflected in the current row, but is NOT visible for the next rows which will use this same row's layout?
Simply add an else part as suggested in comments and move your textView
initialization to out of the if block as-
TextView textView = (TextView) view.findViewById(R.id.Item_textView5);
if (ape.getAlpha() != null && ape.getAlpha().equals("RON") ) {
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setBackgroundColor(Color.CYAN);
}
});
textView.setVisibility(View.VISIBLE);
}else{
textView.setVisibility(View.GONE);
}