Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView setText on particular position

I defined the following code to be able to set text in a certain position.

if(position == 0) {
                holder.text.setText("blah");
            }

What I would like is to check if the position is equals 0, and then if it is, set text on position p - every time it's p = position + 1 (next position). Are we able to set text to a textview from a holder but in a certain position.

Thanks.

like image 584
dynamitem Avatar asked Jun 22 '16 13:06

dynamitem


1 Answers

I think that you are still using ListView, if not the please update the Title of Question.

In your getView(int position, View convertView, ViewGroup parent) method, check the position for which your view is being created. Since you are already using holder, I am assuming you have done (convertView != null) check before extracting the holder.

Here in getView(int position, View convertView, ViewGroup parent), you can check

if (position == 0) {
    holder.text.setText("blah");
} else {
    holder.text.setText("");
}

Remember the else part is required as the views tend to get reused and so your earlier "blah" needs to be reset or else will be visible in other views when your view scrolls.

For a detailed explanation on how reuse of views work, you can check this great talk in which Romain Guy, which explains everything about ListView.

like image 193
Roadblock Avatar answered Sep 27 '22 23:09

Roadblock