简体   繁体   中英

Array to TextView

I create this simple method:

private void getCheckedItems() {
int len = mGrid.getCount();
SparseBooleanArray checked = mGrid.getCheckedItemPositions();
List <Integer> numberList = new ArrayList<>();
for (int i = 0; i < len; i++)
    if (checked.get(i)) {
        Integer item = mNumber.get(i);
        numberList.add(item);
        Integer[] mNumberArray = numberList.toArray(new Integer[numberList.size()]);
        Log.d("Played Number: ", ""+ Arrays.toString(mNumberArray));
    }
  }

With this control method such elements were selected from a grid and I do them return. Now I would like to split the array to put in some TextView ! How can I do?

Arrays.toString(mNumberArray) will give you string like this [2,3,45,67] Just remove brackets [] and you will have a string like this "2,3,45,67" . Now you can easily split string and get a string array. Set each element of this array to each text view

When you want to insert each value from the list to a single textView and if you have the textViews defined you could do something like this:

firstTextView.setText(numberList.get(0));
secondTextView.setText(numberList.get(1)); //and so on

If you don't have the textViews defined in your layout xml, you could get the parent layout and insert the textViews there like this:

LinearLayout parentLayout = (LienarLayout)findViewById(R.id.myTextBoxesParent);
for(int i = 0; i < numberList.size(); i++){
    TextView t = new TextView(getApplicationContext());
    t.setText(numberList.get(i));
    parentLayout.addView(t);
}

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