OnClickListener调用错误的情况

时间:2015-05-11 14:43:27

标签: android onclicklistener

我有一个使用CustomAdapter的listView,listView Item包含一个按钮,我在CustomAdapter中创建了一个OnClickListener来处理按钮,具体取决于它使用switch(position)的所有位置和所有案例工作正常,除了最后一个案例4,当我点击它OnClickListener调用案例0.我认为案例4的代码中有一些东西使应用程序在案例0中执行说明所以我添加了记录案例4的开头,但我发现它被调用了,案例0是被调用的案例。我也尝试删除案例4中的所有代码,如果我在调用案例0中再次单击它,它就可以工作。

如果有人知道如何解决这个问题,请帮助,谢谢

这是我的CustomAdapter:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View row = convertView;
    MyViewHolder holder = null;
    if(row==null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.listview_item, parent, false);
        holder = new MyViewHolder(row);
        row.setTag(holder);
        final SharedPreferences shared = context.getSharedPreferences("PrefName", Context.MODE_PRIVATE);
        final SharedPreferences.Editor editor = shared.edit();
        ........

        Button changeSet = (Button) row.findViewById(R.id.setBtn);
        changeSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (position){
                    case 0:
                        Log.d("NOTHING", "Case 1 called");
                        editor.putBoolean("Clicked", true);
                        editor.putString("TheNumber", "1");
                        editor.apply();
                        Toast.makeText(context, "Set 1 selected please press the save button", Toast.LENGTH_SHORT).show();
                        break;

                   ...........

                    case 4:
                        Log.d("NOTHING", "Case 5 called");
                            Toast.makeText(context, "Set is Locked", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }
    else
    {
        holder = (MyViewHolder) row.getTag();
    }

    holder.myImage.setImageResource(images[position]);
    holder.myText.setText(titleArray[position]);
    holder.myText2.setText(titleArray2[position]);


    return row;
}

1 个答案:

答案 0 :(得分:0)

你应该每次都设置OnClickListener。就像这样:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
ViewHolder holder = null;
if(row==null){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = inflater.inflate(R.layout.listview_item, parent, false);
    holder = new ViewHolder();
    holder.changeSetButton = (Button) row.findViewById(R.id.setBtn);
    holder.myImage =...;
    holder.myText2 = ...;
    row.setTag(holder);
} else {
    holder = (ViewHolder)row.getTag();
} 

final SharedPreferences shared = context.getSharedPreferences("PrefName", Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = shared.edit();
........

holder.changeSetButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        switch (position){
            case 0:
                Log.d("NOTHING", "Case 1 called");
                editor.putBoolean("Clicked", true);
                editor.putString("TheNumber", "1");
                editor.apply();
                Toast.makeText(context, "Set 1 selected please press the save button", Toast.LENGTH_SHORT).show();
                break;

            ...........

            case 4:
            Log.d("NOTHING", "Case 5 called");
            Toast.makeText(context, "Set is Locked", Toast.LENGTH_SHORT).show();
            break;
        }
    }
});
//
holder.myImage.setImageResource(images[position]);
holder.myText.setText(titleArray[position]);
holder.myText2.setText(titleArray2[position]);

return row;
}

static class ViewHolder{
    Button changeSetButton;
    ImageView myImage;
    TextView myText2;
}