Michael Joseph
Michael Joseph

Reputation: 47

Set part of text on TextView

I have created an app to read RSS Feed from xml file on web site and showing it in cards using cardview, it work perfectly but the card contains all the description , I want to show just the first two lines of description so I modify my code but it is not working and says:

StringIndexOutOfBoundsException: length=0; regionStart=0; regionLength=20

this is part of my code :

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    YoYo.with(Techniques.FadeIn).playOn(holder.cardView);
    start= holder.Description.getText().toString().substring(0,20);
    FeedItem current=feedItems.get(position);
    holder.Title.setText(current.getTitle());
    holder.Description.setText(start);
    holder.Date.setText(current.getPubDate());
    Picasso.with(context).load(current.getThumbnailUrl()).into(holder.Thumbnail);

}

Upvotes: 0

Views: 35

Answers (1)

user8959091
user8959091

Reputation:

Instead of

start= holder.Description.getText().toString().substring(0,20);

do this:

start = holder.Description.getText().toString();
if (start.length() > 20) {
    start = start.substring(0,20);
}

if holder.Description.getText().toString() is less than 21 chars long you don't need the substring

Upvotes: 2

Related Questions