Jan 5, 2014

Implement OnItemClickListener for custom ListView

This example implement OnItemClickListener for custom List in last post "Create custom ListView of custom object" to get and display the clicked item.

Implement OnItemClickListener for custom ListView
Implement OnItemClickListener for custom ListView

package com.example.androidcustomlistview;

import java.util.ArrayList;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;

public class MainActivity extends Activity {

 private class MyObject{
  private int number;
  private String name;
  
  MyObject(int num, String nam){
   number = num;
   name = nam; 
  }
  
  public int getNumber(){
   return number;
  }
  
  public String getName(){
   return name;
  }
 } 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ListView listView = (ListView)findViewById(R.id.listview);
  
  //Init ArrayList of MyObject
  ArrayList<MyObject> myArrayList = new ArrayList<MyObject>();
  myArrayList.add(new MyObject(0, "Sunday"));
  myArrayList.add(new MyObject(1, "Monday"));
  myArrayList.add(new MyObject(2, "Tuesday"));
  myArrayList.add(new MyObject(3, "Wednesday"));
  myArrayList.add(new MyObject(4, "Thursday"));
  myArrayList.add(new MyObject(5, "Friday"));
  myArrayList.add(new MyObject(6, "Saturday"));
  
  MyAdapter myAdapter = new MyAdapter(this, myArrayList);
  listView.setAdapter(myAdapter);
  
  listView.setOnItemClickListener(new OnItemClickListener(){

   @Override
   public void onItemClick(AdapterView<?> parent, 
     View view, int position, long id) {
    MyObject clickedObj = (MyObject)parent.getItemAtPosition(position);
    Toast.makeText(MainActivity.this, 
      "Clicked item:\n" +
      clickedObj.getNumber() + ": " +
      clickedObj.getName(), 
      Toast.LENGTH_LONG).show();
   }});
  
 }

 private class MyAdapter extends BaseAdapter {
  
  private ArrayList<MyObject> myList;
  
  private Activity parentActivity;
     private LayoutInflater inflater; 
  
     public MyAdapter(Activity parent, ArrayList<MyObject> l) {
      parentActivity = parent;
         myList=l;
         inflater = (LayoutInflater)parentActivity
           .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     }

  @Override
  public int getCount() {
   return myList.size();
  }

  @Override
  public Object getItem(int position) {
   return myList.get(position);
  }

  @Override
  public long getItemId(int position) {
   return position;
  }

  @Override
  public View getView (int position, View convertView, 
    ViewGroup parent) {
   View view = convertView;
         if(convertView==null)
             view = inflater.inflate(R.layout.row, null);
  
         TextView text1 = (TextView)view.findViewById(R.id.text1);
         TextView text2 = (TextView)view.findViewById(R.id.text2);
         MyObject myObj = myList.get(position);
         text1.setText(String.valueOf(myObj.getNumber()));
         text2.setText(myObj.getName());
         return view;
  }
 }
}

2 comments:

Infolinks In Text Ads