Creating a List in Android

Code in onCreate method of MainActivity Class

private ListView listView ;

private ArrayAdapter<String> listAdapter ;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById( R.id.listView );
String[] content = new String[] { “1”, “2”, “3”, “4”};
ArrayList<String> list = new ArrayList<String>();
list.addAll( Arrays.asList(content) );
listAdapter = new ArrayAdapter<String>(this, R.layout.activity_main, R.id.textView,list);
listView.setAdapter(listAdapter);

activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent” >

<ListView
android:id=”@+id/listView”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentTop=”true”>
</ListView>

<TextView xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:id=”@+id/textView”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textSize=”16sp” />

</RelativeLayout>
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: