Sunday, 28 October 2012

OnClickListener to the layout of the listview item and displays the toast


 Displays the list of items in the form of list view and onclick of listitem its respective toast appears
Main point here is onclick listener is implemented on the layout

package com.example.weeknamesdisplay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] sports = getResources().getStringArray(R.array.sports_array);

        final ListView lv1 = (ListView) findViewById(R.id.listV_main);
        lv1.setAdapter(new ItemListBaseAdapter(this, sports));
    }

}


package com.example.weeknamesdisplay;

import java.io.Serializable;
import java.util.Arrays;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ItemListBaseAdapter extends BaseAdapter {

    private Context context;
    private List<String> displayitems;
    private LayoutInflater l_Inflater;

    public ItemListBaseAdapter(MainActivity mainActivity, String[] sports) {
        // TODO Auto-generated constructor stub

        l_Inflater = LayoutInflater.from(mainActivity);
        this.displayitems = Arrays.asList(sports);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return displayitems.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return displayitems.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = l_Inflater.inflate(R.layout.item_details_view, null);
            holder = new ViewHolder();
            holder.txt_itemName = (TextView) convertView
                    .findViewById(R.id.text1);
            holder.row = (RelativeLayout) convertView

            .findViewById(R.id.row);

            holder.itemImage = (ImageView) convertView
                    .findViewById(R.id.image1);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txt_itemName.setText(displayitems.get(position));

        holder.itemImage.setImageResource(R.drawable.arrow);

        /*
         * holder.row.setOnClickListener(new OnClickListener() {
         *
         * @Override public void onClick(View arg0) { // TODO Auto-generated
         * method stub // Toast.makeText(ItemListBaseAdapter.this,
         * "You have chosen : " + " " + displayitems.get(position),
         * Toast.LENGTH_LONG).show();
         *
         *
         *
         * } });
         */

        View ll = (RelativeLayout) holder.txt_itemName.getParent();
        ll.setFocusable(true);
        ll.setSelected(true);
        ll.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 250
                Toast.makeText(v.getContext(), displayitems.get(position), 250)
                        .show();
                // notifyDataSetChanged();
            }
        });

        return convertView;
    }

    static class ViewHolder {
        TextView txt_itemName;
        RelativeLayout row;
        ImageView itemImage;
    }

}


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"
    android:id="@+id/row" >

   
     <ListView
         android:layout_height="wrap_content"
         android:layout_width="fill_parent" android:id="@+id/listV_main"/>

</RelativeLayout>





Items_Detail.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layercontainer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:padding="10dp"
        android:textColor="#000000"
        android:textSize="16sp"
        android:typeface="sans" />

    <ImageView
        android:id="@+id/image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

</RelativeLayout>

1 comment:

  1. Awesome post. Helped me big time! Thanks!

    ReplyDelete

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...