Simple List View Example using array list
Create New Project > ListViewDemo
Create Class >
MainActivity.java
package com.andr.listviewdemo;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
ListView listview;
ArrayList<String> list=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView)findViewById(R.id.listViewset);
list=new ArrayList<String>();
list.clear();
list.add("Android");
list.add("Iphone");
list.add("Blackberry");
list.add("Java");
list.add("Php");
list.add("Asp.net");
list.add("Sap");
list.add("Seo");
ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,list);
listview.setAdapter(adapter);
}
}
Write code in xml: activity_main.xml
<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:background="@android:color/white"
>
<ListView
android:id="@+id/listViewset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:cacheColorHint="#00000000"
android:listSelector="@android:color/transparent"
>
</ListView>
</RelativeLayout>
Run The Project
very well explained
ReplyDelete