Friday 12 June 2015

Simple Listview in android -2

No comments
In this android example we learn how to create a listview in android by using java.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.here we create one another XML for Listview called Row.in this example we bind Row file(list_adupt_main.xml) with listview using ArrayAdapter in java.

Let's start coding


CREATE NEW ANDROID EXAMPLE

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/mainListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>


</LinearLayout>

Step 2: Write code into list_adupt_main.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

 android:id="@+id/rowTextView" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"
 android:padding="10dp"
 android:textSize="16sp" >


</TextView>

Step 3: Write code into MainActivity.java

package dev.androidapplink.listviewwithjavaapp;

import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

// create variable
private ListView mainListView;
private ArrayAdapter<String> listAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Find the ListView resource.
mainListView = (ListView) findViewById(R.id.mainListView);

// Method 1
// Create and populate a LIST of planet names.

String[] planets = new String[] { "Earth[Method 1]", "Mars[Method 1]",
"Mercury[Method 1]", "Venus[Method 1]", "Jupiter[Method 1]",
"Saturn[Method 1]", "Uranus[Method 1]", "Neptune[Method 1]" };

ArrayList<String> planetList = new ArrayList<String>();
planetList.addAll(Arrays.asList(planets));

// Create ArrayAdapter using the planet list.

listAdapter = new ArrayAdapter<String>(this, R.layout.list_adupt_main,
planetList);

// Method 2
// Add more passed a STRING[] instead of a <String>List.
// May Exception will occur if you add more items into constructor of ArrayAdapter.

listAdapter.add("Ceres[Method 2]");
listAdapter.add("Pluto[Method 2");
listAdapter.add("Haumea[Method 2]");
listAdapter.add("Makemake[Method 2]");

listAdapter.add("Eris[Method 2]");
// Set the ArrayAdapter as the ListView's adapter.
mainListView.setAdapter(listAdapter);

}
}

Step 4:Now Run Your Project:



No comments :

Post a Comment

Follow me Share