Here I will start From The Basic View and Examples of Android. here we will discuss the most useful and important Part for most of the Android App That is How to Implement ListView in Android App and How To Set a Listeners in That ListView.
4. Now finally run Your Project
You Will See A Below Output

For More Tutorial Share And Subscribe to my Blog So you will get all the updates on your email, We Never Spam.
Thank You
Below Screen Shot Is the Final Output Of Our Tutorial
1. Create a new project by going to File ⇒ New Android Project. Fill all the details and name your activity as MainActivity.
2. Once the project is created open your activity_main.xml file and Add Below Code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.targetandroid.info.listviewexample.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lv_city">
</ListView>
</LinearLayout>
After this, You Can See an Output Shown in Below Screenshot
3. Now Open Your MainActivity.java And Add Below Code in it.
I have Used ArrayList and ArrayAdapter in This Scope to Populate Our List With Data
ArrayList:- The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.it is an implementation of java.util.List that's backed by an array. You can use it anywhere you would use a java.util.List. E.g. where you need to maintain an order of a collection of objects where duplicates are allowed.
ArrayAdapter:- is an Android SDK class for adapting an array of objects as a data source. Adapters are used by Android to treat a result set uniformly whether it's from a database, file, or in-memory objects so that it can be displayed in a UI element. The ArrayAdapter is useful for the latter. Use it anywhere you would use an Adapter.
3. Now Open Your MainActivity.java And Add Below Code in it.
I have Used ArrayList and ArrayAdapter in This Scope to Populate Our List With Data
ArrayList:- The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.it is an implementation of java.util.List that's backed by an array. You can use it anywhere you would use a java.util.List. E.g. where you need to maintain an order of a collection of objects where duplicates are allowed.
ArrayAdapter:- is an Android SDK class for adapting an array of objects as a data source. Adapters are used by Android to treat a result set uniformly whether it's from a database, file, or in-memory objects so that it can be displayed in a UI element. The ArrayAdapter is useful for the latter. Use it anywhere you would use an Adapter.
package com.targetandroid.info.listviewexample;
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.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
//Creating Variable Of ListView
ListView list_city;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Connecting Our ListView Id With Java Variable
list_city=(ListView)findViewById(R.id.lv_city);
/*Creating a List Variable and Intializing
it by ArrayList Beacuse ArratList is its Parent Class*/
List<String> city=new ArrayList<>();
//Adding Values to list
city.add("Nagpur");
city.add("Raipur");
city.add("Kolhapur");
city.add("Solapur");
city.add("Bilaspur");
city.add("Kanpur");
city.add("Nasik");
//Passing that list data into ArrayList So that we can Show it in Our List
ArrayAdapter cityadap=new ArrayAdapter(this,android.R.layout.simple_list_item_1,city);
//Finally Set the adapter into our listview
list_city.setAdapter(cityadap);
//setting a listener into our listview you can use intent also by checking which item is clicked
list_city.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Toast.makeText(MainActivity.this,"You clicked item no.:- "+position,Toast.LENGTH_SHORT).show();
}
});
}
}
4. Now finally run Your Project
You Will See A Below Output

Thank You



1 comments so far
Very Good Nice Content Keep it up
EmoticonEmoticon