In this Very simple example i show you how to create ListView. Listview is one of the most popular controls, It can be used in many different forms and can be customized to suitable of app requirement.In this tutorial, I will demonstrate you how you can bind a simple array of Strings with ListView.
Step 1: Write code into activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Step 2: Write code into MainActivity.java
package dev.androidapplink.listviewusingstrings;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView1 = (ListView) findViewById(R.id.listView1);
String[] items = { "laptop", "mobile", "Keyboard", "PC", "Screen", "Battery" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
}
}
Step 3:Now Run Your Project:
Step 1: Write code into activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Step 2: Write code into MainActivity.java
package dev.androidapplink.listviewusingstrings;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView1 = (ListView) findViewById(R.id.listView1);
String[] items = { "laptop", "mobile", "Keyboard", "PC", "Screen", "Battery" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
listView1.setAdapter(adapter);
}
}
Step 3:Now Run Your Project:
No comments :
Post a Comment