Hisham Muneer
Hisham Muneer

Reputation: 8742

Custom Listview contains a button I want to set onclicklistener on the button

I made a custom listview with a button and textviews in it. What i want to do is - On the click of a button, I want to get the index of listview.

I used HashMap to store the key value pair.

Here is my Activity code:

package com.hisham.listviewexhisham;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {

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

        ListView lv= (ListView)findViewById(R.id.listview);
        final TextView tvTesting = (TextView)findViewById(R.id.tvTesting);

        // create the grid item mapping
        String[] from = new String[] {"quantity", "item", "cost", "remove"};
        int[] to = new int[] { R.id.quantity, R.id.item, R.id.cost, R.id.remove};
        ArrayList<String> productNames = new ArrayList<String>();
        productNames.add("Trumbull 125");
        productNames.add("Trumbull 135");
        productNames.add("Trumbull 17'' Monitor");
        productNames.add("Trumbull Keyboard");
        productNames.add("Trumbull 2TB HD");

        // prepare the list of all records
        List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
        for(int i = 0; i < productNames.size(); i++){
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("quantity", "" + i);
            map.put("item", productNames.get(i));
            map.put("cost", "$ 600.99");
            map.put("remove", "X");
            fillMaps.add(map);
        }

        // fill in the grid_item layout
        SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid, from, to);
        lv.setAdapter(adapter);



        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

                    tvTesting.setText("Id is - " +arg2);


            }
        });



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    }

here is grid.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/quantity"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"
            android:width="20dip" />

        <TextView
            android:id="@+id/item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1.01"
            android:text="Trumbull"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/cost"
            android:layout_width="75dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:gravity="right"
            android:text="$ 500.99" />

        <Button
            android:id="@+id/remove"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:text="X" />
    </LinearLayout>

</LinearLayout>

and this is my main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvTesting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

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

</LinearLayout>

Upvotes: 0

Views: 2291

Answers (1)

Mohamed_AbdAllah
Mohamed_AbdAllah

Reputation: 5322

You can use SimpleAdapter.setViewBinder and then set the onClickListener() of your Button in the ViewBinder by comparing the Id of the View being bound to the Button Id (R.id.remove) and set its onClickListener().

Update:

    adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
             @Override
             public boolean setViewValue(View view, Object data, String textRepresentation){
                 if (view.getId() == R.id.remove) {
                     Button b=(Button) view;
                     b.setOnClickListener(/*your desired implementation*/);
                     return true; 
                }
                 return false;
     } 

Upvotes: 6

Related Questions