这个问题困扰了我半天,在此我写个最简单的程序让大家看看。
其实就是因为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这样做可以取出每次所对应的点击事件