Monday, August 10, 2015

Simple ListView OnItemClickListener


.
Simple ListView OnItemClickListener
Using Android Studio 1.4

1) Create New Project

Application Name: MyListViewItemClickListener1
Select API 14:Android 4.0 (IceCreamSandwich)
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:orientation="vertical"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent">
       <ListView
           android:id="@+id/listView1"
           android:layout_height="fill_parent"
           android:layout_width="fill_parent" />
   </LinearLayout>
</RelativeLayout>
OUTCOME.

3) Create Data Model

3.1) Product.java

package com.notarazi.mylistviewitemclicklistener1;
public class Product {
   private int id;
   private String name;
   private double price;
   public Product(){
       super();
   }
   public Product(int id, String name, double price) {
       super();
       this.id = id;
       this.name = name;
       this.price = price;
   }
   @Override
   public String toString() {
       return this.id ". " this.name " [$" this.price "]";
   }
}

4) Edit MainActivity.java

package com.notarazi.mylistviewitemclicklistener1;
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.TextView;
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);
       setListView();
       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 setListView() {
       ListView listView1 = (ListView) findViewById(R.id.listView1);
       Product[] items = {
               new Product(1"Milk"21.50),
               new Product(2"Butter"15.99),
               new Product(3"Yogurt"14.90),
               new Product(4"Toothpaste"7.99),
               new Product(5"Ice Cream"10.00),
       };
       ArrayAdapter<Product> adapter = new ArrayAdapter<Product>(this,
               android.R.layout.simple_list_item_1, items);
       listView1.setAdapter(adapter);
       listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView<?> parent, View view, int position,
                                   long id) {
               String item = ((TextView)view).getText().toString();
               Toast.makeText(getBaseContext(), item, 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