Show/Convert Integer array to Integer list in Android Java

How to convert integer[] to list integer and show using textview in android MainActivity.java programming file.

In this tutorial we are simply converting integer array to integer list using Arrays.asList() method. So here is the complete step by step tutorial for Convert Integer array to Integer list in Android Java.

android-project-download-code-button

How to Show/Convert Integer array to Integer list in Android Java.

Code for MainActivity.java file.

package com.convertintegerarraytointegerlist_android_examples.com;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 TextView textview;
 Integer[] IntegerArray = new Integer[]{1,2,3,4,5,6,7,8,9};
 List<Integer> IntegerList;
 int i = 0;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 textview = (TextView)findViewById(R.id.textView1);
 
 //Converting integer array to integer list.
 IntegerList = Arrays.asList(IntegerArray);

 while(i < IntegerList.size())
 {
 textview.setText(textview.getText() + " " + IntegerList.get(i) + ", ");
 i++;
 
 }
 
 }

}

Code for activity_main.xml layout file.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.convertintegerarraytointegerlist_android_examples.com.MainActivity" >

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text=""
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshot:

Show/Convert Integer array to Integer list in Android Java

Click here to download Show/Convert Integer array to Integer list in Android Java project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *