简体   繁体   中英

How do I turn a string Array into a textview Array?

I have a string array as shown below, what is the best way to reformat this to obtain a TextView array

public static List<AthenaPanel> getData()  {

    //created an object for ur Drawer recyclerview array
    List<AthenaPanel> data= new ArrayList<>();
    //this is where you would add all your icons for the drawer list
    //arrays of icons
    int[] icons={R.drawable.ic_search_black_24dp};

    String[] titles = { "MyQuestions","     MyAnswers","     Calendar","     Setting","     Send FeedBack"};
    //this is our for loop to cycle through the icons and the titles
    for(int i=0;i<5;i++){
        AthenaPanel current=new AthenaPanel();

        //i%().length allows ups to loop over the array any number of times we want to
        current.iconId=icons[i%icons.length];
        current.title=titles[i%titles.length];
        data.add(current);
    }
    return data;
}

Here is my class

public class AthenaPanel {
int iconId;
String title;

}

I don't know exactly what you want to achieve without more details, my thought is that there is a different way to get what you want, but I'm going to answer the question anyway.

In this case I guess you want to generate as many textviews as AthenaPanel objects are returned by getData() , the best is not to restrict the layout to a determined number of TextViews defined in your xml. Instead, remove all TextView tags from your xml and leave a blank LinearLayout You should create the same number of TextView with a base context:

public List<TextView> createTextViews(Context context, List<AthenaPanel> athenaPanelList) {
   ArrayList<TextView> textViewList = new ArrayList<>();

   if(athenaPanelList != null && athenaPanelList.size() > 0) {
      for(AthenaPanel athenaPanel : athenaPanelList) {
         TextView textView = new TextView(context);
         textView.setText(athenaPanel.getTitle());
         textViewList.add(textView);
      }
   }

   return textViewList;
}

And call that method to add the views to the LinearLayout . Let's say a linear layout in your activity:

LinearLayout myLinearLayout = findViewById(R.id.myLinearLayout);
ArrayList<TextView> textViews = createTextViews(this, getData());
for(TextView textView : textViews){
   myLinearLayout.addView(textView);
}

You'll need to specify LayoutParams for the TextView's if you don't want them to use the default LayoutParams from the root layout. The LinearLayout can be actually any subclass of ViewGroup ie FrameLayout, RelativeLayout...

If the only thing you want is to convert that String array to TextView Array:

String[] titles = { "MyQuestions","     MyAnswers","     Calendar","     Setting","     Send FeedBack"};
TextView[] textViews = new TextView[titles.length];
for(int i=0; i < titles.length; i++){    // I'd suggest to use lenght propery rather than 5 value
   TextView textView = new TextView(context);
   textView.setText(athenaPanel.getTitle());
   textViews[i] = textView;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM