自定义列表视图包含我想在按钮上设置onclicklistener的按钮

时间:2012-08-01 08:38:02

标签: android

我制作了一个自定义列表视图,其中包含按钮和文本视图。 我想做的是 - 点击一个按钮, 我想得到listview的索引。

我使用HashMap存储键值对。

这是我的活动代码:

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;
    }
    }

这里是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>

这是我的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>

1 个答案:

答案 0 :(得分:6)

您可以使用SimpleAdapter.setViewBinder,然后通过比较绑定的视图的ID与按钮ID(onClickListener()),在ViewBinder中设置按钮的R.id.remove并设置其onClickListener()。

更新

    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;
     }