To create Listview we need the following:-
MainActivity class containing Listview will be the main screen.
MainActivity.java
package com.androidruler.simplelistview; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ListView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView mainactivity; // creating arraylist of MyItem type to set to adapter ArrayList<String> myitems=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mainactivity=(ListView)findViewById(R.id.mainactivitylistview); //Adding data to arraylist to be passed to ArrayAdapter. u can use arrays also. myitems.add("Christ Redeemer: Rio de Janeiro Brazil")); myitems.add("Great Wall of China: China"); myitems.add("Machu Picchu: Peru"); myitems.add("Petra: Jordan"); myitems.add("Pyramid at Chichén Itzá:YucataPeninsula, Mexico"); myitems.add("Roman Colosseum: Rome, Italy"); myitems.add("Taj Mahal: Agra, India"); //Creating Adapter object for setting to listview ArrayAdapter adapter=new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1,myitems); mainactivity.setAdapter(adapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. //getMenuInflater().inflate(R.menu.menu_main, menu); //this shows three d ots at right corner on click settings open return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } //layout file for MainActivity.java
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" tools:context=".MainActivity"> <ListView android:id="@+id/mainactivitylistview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:divider="@android:color/black" android:dividerHeight="2dp"></ListView> </RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.androidruler.simplelistview" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>