Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't display both Persian and English texts together in RecyclerView

I have a RecylcerView which loads it's data from an external server. And each of my items have a TextView. My problem is that some of my items don't have their related values for TextView when both English and Persian texts are used, and it shows nothing in the TextView. But it perfectly works fine when just one language, either English or Persian is used. By one language I mean all of the data in adapter be in one language not just one item. Besides, my adapter gets it's data from external server, so I can't store my texts in /res folder. In addition, getText() in my code always shows the correct value.

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
    if(viewHolder instanceof ItemViewHolder) {
        final CategoryItem nature = mItems.get(i);
        String itemName = mItems.get(i).getName();

        //This line of code doesn't work for some items when both English and Arabic texts are used
        ((ItemViewHolder) viewHolder).dsc.setText(itemName);

        //This line of code shows the correct values at all times
        System.out.println("itemName " + ((ItemViewHolder) viewHolder).dsc.getText());

    }else{
        ((ProgressViewHolder)viewHolder).progressBar.setIndeterminate(true);
    }
}

As shown in the picture, there are some items which have empty TextView. The three values for empty items of this picture should be: bb, سلام , bbسلام

enter image description here

like image 896
Pouria Faraji Avatar asked Nov 19 '25 09:11

Pouria Faraji


2 Answers

A short example of multiple viewholders.

May be after this your code will be clearer

public class yourAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
class ItemViewHolder extends RecyclerView.ViewHolder {
    ...
}

class ProgressViewHolder extends RecyclerView.ViewHolder {
    ...
}

@Override
public int getItemViewType(int position) {
    //here is the place where viewholder may choose
    return position;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     switch (viewType) {
         case 0: return new ItemViewHolder(...);
         case 1: return new ProgressViewHolder(...);
         ...
     }
  }
}
like image 182
Vlad Avatar answered Nov 21 '25 22:11

Vlad


After researching I figured out that my TextViews become visible after pressing Request Layout button in Hierarchy Viewer tool in Android Studio. So, I added one line to my code after setText()

((ItemViewHolder) viewHolder).dsc.requestLayout();

And after that, everything worked fine.

I would be really thankful If anyone could explain the reason.

like image 40
Pouria Faraji Avatar answered Nov 21 '25 23:11

Pouria Faraji



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!