Get strings in adapter

I have an ArrayAdapter for my ListView. It has a textview and an arrow image. If the TextView has 3 lines or more, I have to show the image of the arrow, but if the line is count <3, the arrow should be hidden. But in fact, the adapter does not have their lines if it draws before the TextView. Any ideas? I need to show an element with an arrow or not, depends on the number of lines.

This code does not work (TextView gets the number of lines only after drawing)

if(holder.text.getLineCount() < 3)
{
        holder.arrow.setVisibility(View.GONE);
}
else
{
        holder.arrow.setVisibility(View.VISIBLE);
}
+3
source share
1 answer

You need to delay code execution until the text is updated. Try using TextView :: addTextChangedListener () for this:

holder.text.addTextChangedListener(new TextWatcher() {
    public void afterTextChanged(Editable target) {

        if(holder.text.getLineCount() < 3) {
             holder.arrow.setVisibility(View.GONE);
        }
        else {
             holder.arrow.setVisibility(View.VISIBLE);
        }

    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
});
0
source

All Articles