ListView中嵌入布局中有Button,如何响应Button点击事件

这个问题困扰了我半天,在此我写个最简单的程序让大家看看。

其实就是因为listview 的getview()里面的重复机制

public class ExpandableListViewTest extends Activity
{
private MyListAdapter adapter;
String[] a=new String[]{"1","2","3","4"};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建一个BaseExpandableListAdapter对象
ListView expandListView = (ListView) findViewById(R.id.list);
adapter=new MyListAdapter(ExpandableListViewTest.this);
expandListView.setAdapter(adapter);

}

class MyListAdapter extends BaseAdapter{


private Context mContext;
// private List<ShopBean> shopCollectList;


public MyListAdapter(Context context) {
// TODO Auto-generated constructor stub
this.mContext = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return a.length;
}


@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}


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


@Override
public View getView(int position, View convertView, ViewGroup arg2) {
final int b;
b=position;
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.openshop_manage_item_layout, null);
viewHolder = new ViewHolder();
viewHolder.tvManageProductName = (TextView) convertView
.findViewById(R.id.tvManageProductName);
viewHolder.tvManageProductPrice = (TextView) convertView
.findViewById(R.id.tvManageProductPrice);
viewHolder.tvManageProductType = (TextView) convertView
.findViewById(R.id.tvManageProductType);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}




viewHolder.tvManageProductName.setText(a[position]);
viewHolder.tvManageProductPrice.setText(a[position]
+ "元" );
   viewHolder.tvManageProductType.setText(a[position]);
viewHolder.tvManageProductType.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

//Toast.makeText(ExpandableListViewTest.this, b+1, Toast.LENGTH_LONG).show();
Log.i("ccccccccc",String.valueOf(b));
}
});
return convertView;
}
class ViewHolder {


TextView tvManageProductName;
TextView tvManageProductPrice;
TextView tvManageProductType;
}
}


布局文件有两个

第一个main.xml

<?xml version="1.0" encoding="utf-8"?>
<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/list"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
/>
</LinearLayout>

第二个openshop_manage_item_layout.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="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:gravity="clip_vertical"
    android:orientation="vertical"
    android:state_pressed="true" >


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:layout_marginTop="10dip"
        android:orientation="horizontal" >


        <TextView
            android:id="@+id/tvManageProductName"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="商品名称"
            android:textSize="18sp" />


        <TextView
            android:id="@+id/tvManageProductPrice"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="单价"
            android:textSize="18sp" />


        <TextView
            android:id="@+id/tvManageProductType"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="类型"
            android:textSize="18sp" />
    </LinearLayout>
</LinearLayout>

主要是用final int b来重新获取position这样做可以取出每次所对应的点击事件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值