Android ListView Tutorial

Total Views : 157
Zoom In Zoom Out Read Later Print

In this tutorial i will be show you how to build simple android ListView. This article is about creating ListView and launching new activity on selecting single list item and passing data to new created activity.

Android ListView is a view which groups items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter. We can provide data to adapter from multiple source such as an array or database.  The adapter pulls the items from a data source, an array for example, and then converts each item into a view and show them into the ListView.

The onListItemClick() method is used to handle the clicks on android ListView item or row. This method receives 4 parameters when a row or item is clicked:

  • ListView : ListView which hold all of items.
  • View : selected item.
  • Position : position of item in ListView.
  • Id : id of selected item.
Let’s get start by creating a project.

1. First of all create a list of items you want to show.

string.xml

<resources>
<string name="app_name">ListView</string>

<string-array name="country">
<item>India</item>
<item>America</item>
<item>Russia</item>
<item>South Africa</item>
<item>Australia</item>
<item>Ireland</item>
<item>Bangladesh</item>
<item>England</item>
<item>Sri Lanka</item>
<item>West Indies</item>
</string-array>
</resources>


2 .Creating row view for ListView.

list_view_row.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp"
android:textStyle="bold" >
</TextView>

3. Create a class MainActivity and write following code.

MainActivity.java

package com.prodevsblog.listexample;

import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
void init(){
String[] country = getResources().getStringArray(R.array.country);
// Binding resources to ListAdapter
this.setListAdapter(new ArrayAdapter<>(this, R.layout.list_view_row, R.id.textView, country));

ListView lv = getListView();

//handle list item on click
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

// selected item
String team = ((TextView) view).getText().toString();

// Launching new Activity and sending data to home activity
Intent i = new Intent(getApplicationContext(), HomeActivity.class);
i.putExtra("country", team);
startActivity(i);

}
});
}
}


4. Demo activity for showing result of ListView item click.

HomeActivity.java

package com.prodevsblog.listexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class HomeActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
init();
}
void init(){
TextView txtProduct = findViewById(R.id.textView);
Intent i = getIntent();
String product = i.getStringExtra("country");
txtProduct.setText(product);
}
}

If you have any problem with above code you can comment or simply download our working code for this project.

See More

Latest Photos