我试图创建一个列表,其中该列表上的每个元素都有6个按钮。当我在该列表的第二个位置时,我设置OnClickListener的按钮将覆盖第一个位置按钮的功能。
我在适配器中实现了getView函数,如下所示:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder = null;
System.out.println("position: "+position);
if (convertView == null) {
mViewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.activity_card_query, parent, false);
mViewHolder.buttons[0] = (Button) convertView.findViewById(R.id.button1);
mViewHolder.buttons[1] = (Button) convertView.findViewById(R.id.button2);
mViewHolder.buttons[2] = (Button) convertView.findViewById(R.id.button3);
mViewHolder.buttons[3] = (Button) convertView.findViewById(R.id.button4);
mViewHolder.buttons[4] = (Button) convertView.findViewById(R.id.button5);
mViewHolder.buttons[5] = (Button) convertView.findViewById(R.id.button6);
convertView.setTag(mViewHolder);
}else{
mViewHolder = (ViewHolder) convertView.getTag();
}
int j;
if(cant<=6) j=cant;
else if(getCount()-1!=position)j=6*position+6;
else j=cant;
System.out.println("Caso: "+6*position+" "+j);
for(int i=6*position;i<j;i++){
System.out.println("Bank: "+data.get(i).mID+" "+data.get(i).mDicName);
mViewHolder.buttons[i%6].setGravity(Gravity.CENTER_HORIZONTAL);
if(data.get(i).mDicName.equals("Mercantil")) {
mViewHolder.buttons[i%6].setBackgroundResource(R.drawable.bank_mercantil);
mViewHolder.buttons[i%6].setText(data.get(i).mName);
mViewHolder.buttons[i%6].setTextColor(Color.WHITE);
//button.setHint(Integer.toString(data.get(i).mID));
mViewHolder.buttons[i%6].setHint(Integer.toString(i));
mViewHolder.buttons[i%6].setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(inflater.getContext(), Advertisement.class);
//i.putExtra("id", ((Button) v).getHint().toString());
i.putExtra("id", Integer.toString(data.get(Integer.parseInt(((Button) v).getHint().toString())).mID));
i.putExtra("name", data.get(Integer.parseInt(((Button) v).getHint().toString())).mName);
i.putExtra("bank", BanksInfo.MERCANTIL);
inflater.getContext().startActivity(i);
}
});
}
if(data.get(i).mDicName.equals("Venezuela")) {
mViewHolder.buttons[i%6].setBackgroundResource(R.drawable.bank_venezuela);
mViewHolder.buttons[i%6].setText(data.get(i).mName);
mViewHolder.buttons[i%6].setTextColor(Color.BLACK);
//button.setHint(Integer.toString(data.get(i).mID));
mViewHolder.buttons[i%6].setHint(Integer.toString(i));
mViewHolder.buttons[i%6].setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(inflater.getContext(), Advertisement.class);
//i.putExtra("id", ((Button) v).getHint().toString());
i.putExtra("id", Integer.toString(data.get(Integer.parseInt(((Button) v).getHint().toString())).mID));
i.putExtra("name", data.get(Integer.parseInt(((Button) v).getHint().toString())).mName);
i.putExtra("bank", BanksInfo.VENEZUELA);
inflater.getContext().startActivity(i);
}
});
}
if(data.get(i).mDicName.equals("Provincial")) {
mViewHolder.buttons[i%6].setBackgroundResource(R.drawable.bank_bbva);
mViewHolder.buttons[i%6].setTextColor(convertView.getResources().getColor(android.R.color.black));
mViewHolder.buttons[i%6].setText(data.get(i).mName);
//button.setHint(Integer.toString(data.get(i).mID));
mViewHolder.buttons[i%6].setHint(Integer.toString(i));
mViewHolder.buttons[i%6].setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(inflater.getContext(), Advertisement.class);
//i.putExtra("id", ((Button) v).getHint().toString());
i.putExtra("id", Integer.toString(data.get(Integer.parseInt(((Button) v).getHint().toString())).mID));
i.putExtra("name", data.get(Integer.parseInt(((Button) v).getHint().toString())).mName);
i.putExtra("bank", BanksInfo.BBVA);
inflater.getContext().startActivity(i);
}
});
}
if(data.get(i).mDicName.equals("Commerce")) {
mViewHolder.buttons[i%6].setBackgroundResource(R.drawable.bank_commerce);
mViewHolder.buttons[i%6].setTextColor(convertView.getResources().getColor(android.R.color.white));
mViewHolder.buttons[i%6].setText(data.get(i).mName);
//button.setHint(Integer.toString(data.get(i).mID));
mViewHolder.buttons[i%6].setHint(Integer.toString(i));
mViewHolder.buttons[i%6].setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent i = new Intent(inflater.getContext(), Advertisement.class);
//i.putExtra("id", ((Button) v).getHint().toString());
i.putExtra("id", Integer.toString(data.get(Integer.parseInt(((Button) v).getHint().toString())).mID));
i.putExtra("name", data.get(Integer.parseInt(((Button) v).getHint().toString())).mName);
i.putExtra("bank", BanksInfo.COMMERCE);
inflater.getContext().startActivity(i);
}
});
}
}
if(getCount()-1==position)
if(cant%6!=0){
for(int i=(cant%6);i<6;i++){
mViewHolder.buttons[i].setBackgroundResource(R.drawable.extra);
mViewHolder.buttons[i].setText("");
mViewHolder.buttons[i].setHint("");
mViewHolder.buttons[i].setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
}
});
}
}
return convertView;
}
由于
答案 0 :(得分:0)
永远不要在getView
中附加onClicklistener。而是使用:
yourGridView.setOnItemClickListener(..
那是因为getView
重用旧视图及其点击方法。因此,经过一些滚动后,您的图像视图是正确的,但您的点击方法完全混乱,可能在每个图像后面!希望它有所帮助。