Monday, August 10, 2015

Basic List View


.

Basic ListView
Using Android Studio 1.4

1) Create New Project

Application Name: MyBasicListView
Follow the wizard to create New Blank Activity.

2) Edit res/layout/content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   app:layout_behavior="@string/appbar_scrolling_view_behavior"
   tools:showIn="@layout/activity_main" tools:context=".MainActivity">
   <LinearLayout
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical" >
       <ListView
           android:id="@+id/list"
           android:layout_height="wrap_content"
           android:layout_width="match_parent">
       </ListView>
   </LinearLayout>
</RelativeLayout>
OUTCOME.

3)Edit MainActivity.java

3.1) Passing items as string

package com.notarazi.mybasiclistview1;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);
       setBasicListView();
       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
       fab.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                       .setAction("Action"null).show();
           }
       });
   }
   private void setBasicListView() {
       // Get ListView object from xml
       final ListView listView = (ListView) findViewById(R.id.list);
       // Defined Array values to show in ListView
       String[] values = new String[]{"Android List View",
               "Adapter implementation",
               "Simple List View In Android",
               "Create List View Android",
               "Android Example",
               "List View Source Code",
               "List View Array Adapter",
               "Android Example List View"
       };
       // Define a new Adapter
       // First parameter - Context
       // Second parameter - Layout for the row
       // Third parameter - ID of the TextView to which the data is written
       // Forth - the Array of data
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
               android.R.layout.simple_list_item_1, android.R.id.text1, values);
       // Assign adapter to ListView
       listView.setAdapter(adapter);
       // ListView Item Click Listener
       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view,
                                   int position, long id) {
               // ListView Clicked item index
               int itemPosition = position;
               // ListView Clicked item value
               String itemValue = (String) listView.getItemAtPosition(position);
               // Show Alert
               Toast.makeText(getApplicationContext(),
                       "Position :" + itemPosition + "  ListItem : " + itemValue, Toast.LENGTH_LONG)
                       .show();
           }
       });
   }
   @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);
       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);
   }
}
OUTCOME

3.2) Passing items as ArrayList

(this allows you to add string items later)
package com.notarazi.mybasiclistview1;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
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.Arrays;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
       setSupportActionBar(toolbar);
       setBasicListView();
       FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
       fab.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                       .setAction("Action"null).show();
           }
       });
   }
   private void setBasicListView() {
       // Get ListView object from xml
       final ListView listView = (ListView) findViewById(R.id.list);
       // Defined Array values to show in ListView
       String[] values = new String[]{"Android List View",
               "Adapter implementation",
               "Simple List View In Android",
               "Create List View Android",
               "Android Example",
               "List View Source Code",
               "List View Array Adapter",
               "Android Example List View"
       };
       ArrayList<String> valueList = new ArrayList<String>();
       valueList.addAll(Arrays.asList(values));
       // Define a new Adapter
       // First parameter - Context
       // Second parameter - Layout for the row
       // Third parameter - ID of the TextView to which the data is written
       // Forth - the Array of data
       ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
               android.R.layout.simple_list_item_1, android.R.id.text1valueList);
       // Add more planets. If you passed a String[] instead of a List<String>
       // into the ArrayAdapter constructor, you must not add more items.
       // Otherwise an exception will occur.
       adapter.add("Extra Item 1");
       adapter.add("Extra Item 2");
     
       // Assign adapter to ListView
       listView.setAdapter(adapter);
       // ListView Item Click Listener
       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view,
                                   int position, long id) {
               // ListView Clicked item index
               int itemPosition = position;
               // ListView Clicked item value
               String itemValue = (String) listView.getItemAtPosition(position);
               // Show Alert
               Toast.makeText(getApplicationContext(),
                       "Position :" + itemPosition + "  ListItem : " + itemValue, Toast.LENGTH_LONG)
                       .show();
           }
       });
   }
   @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);
       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);
   }
}
OUTCOME.

DOWNLOAD

REFERENCES


.

No comments:

Post a Comment