我正在使用this library来创建一个应用程序,其中列出了需要折叠方面的内容。但是每个视图(该列表中的每个元素)都有6个按钮,我无法从适配器向每个按钮添加clickListener
,因为它会覆盖最后一个位置的clickListener
。我使用another library但它适用于OpenGL,而且它有点儿错误。
如何设置每个按钮的特定功能?
我已在适配器中设置OnClickListener
,但它不起作用:
public class CardQueryAdapter extends BaseAdapter{
private int cant;
LayoutInflater inflater;
private ArrayList<Cards> data;
DataBaseConnectionManager mDataBase;
int[] buttons=new int[]{R.id.button1,R.id.button2,R.id.button3,
R.id.button4,R.id.button5,R.id.button6};
public CardQueryAdapter(Context context, DataBaseConnectionManager mDataBase) {
inflater = LayoutInflater.from(context);
this.mDataBase = mDataBase;
data = mDataBase.getUserCards();
cant = data.size();
System.out.println("Size= "+cant+" "+(int) Math.ceil(cant/6.0));
}
@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;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return (int) Math.ceil(cant/6.0);
}
public void setCount(int cant) {
this.cant = cant;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
static class ViewHolder{
private Button[] buttons = new Button[6];
}
}
在下面的图像中,您可以看到它在第一个和第二个位置的外观,但是按钮7正在替换按钮1 clickListener:
The library已更新,但存在问题。现在效果很好。
答案 0 :(得分:0)
第7个按钮覆盖第1个按钮的问题在于您编写的逻辑。我在这段代码中怀疑它
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);
你能做什么?
将log.d
慷慨地放入代码中,看看日志是否应该打印出来。根据日志,您可以检查哪个按钮绑定了呼叫。在i
内打印for loop
,并在每次选择哪个按钮时进行检查。