View.getId()不起作用

时间:2016-03-05 19:01:30

标签: android android-layout android-view

我是Android编程新手。我想更新recyclerview的卡片视图布局中的项目。这是recyclerview的ListRowViewHolder

public static class ListMenuRowViewHolder extends RecyclerView.ViewHolder
{
        protected NetworkImageView thumbnail;
        protected TextView itemname;
        protected TextView price;
        protected TextView itemtype;
        protected TextView quantity;
        protected ImageView add;
        protected ImageView sub;
        protected ImageView imageView;
        protected CardView item_layout;

        public ListMenuRowViewHolder(View itemView) {
            super(itemView);


            this.thumbnail=(NetworkImageView)itemView.findViewById(R.id.recom);
            this.imageView=(ImageView)itemView.findViewById(R.id.categ);
            this.itemname=(TextView)itemView.findViewById(R.id.itemvalue);
            this.add=(ImageView)itemView.findViewById(R.id.add);
            this.sub=(ImageView)itemView.findViewById(R.id.sub);
            this.price=(TextView)itemView.findViewById((R.id.price));
            this.quantity=(TextView)itemView.findViewById((R.id.quantity));
            this.item_layout=(CardView)itemView.findViewById((R.id.item_layout));
            itemView.setClickable(true);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.e("myname", "rohit");
                    ItemscardClickListener.onClick(v, getAdapterPosition());
                }
            });

        }

    }

这是我尝试更新数量的地方

 mAdapter.setItemsCardClickListener(new ItemsCardClickListener() {
                @Override
                public void onClick(View v, int position) {

                    int q;
                    switch (v.getId())
                    {
                        case R.id.add : q= Integer.parseInt(mAdapter.getmOrder().get(position).getQuantity());
                            q++;
                            Log.e("mishra", String.valueOf(q));
                            mAdapter.getmOrder().get(position).setQuantity(String.valueOf(q));
                            mAdapter.notifyDataSetChanged();
                            break;
                        case R.id.sub : q=Integer.parseInt(mAdapter.getmOrder().get(position).getQuantity());
                            if(q>0)
                                q--;
                            mAdapter.getmOrder().get(position).setQuantity(String.valueOf(q));
                            mAdapter.notifyDataSetChanged();
                            break;
                        case R.id.quantity:
                            Log.e("mishra", "hihi");
                            break;
                        default:Log.e("mishra", "hi");


                    }
                }
            });

每当我点击视图中的项目时,他们都会被点击,但我没有使用v.getId()获取id。交换机始终进入默认情况。请提供一些解决方案。

2 个答案:

答案 0 :(得分:2)

您在OnClickListener上设置的View包含所有其他子视图,而不是单个子视图本身。您应该让ListMenuRowViewHolder实现View.OnClickListener并在每个单独的视图上设置一个监听器,如下所示:

sub.setOnClickListener(this);
price.setOnClickListener(this);
//etc

您可以实施

@Override
public void onClick(View v) {
    //Now the correct subview gets passed to your switch in the Activity
    ItemscardClickListener.onClick(v, getAdapterPosition());
}
<{1>}中的

所以它看起来像这样:

ListMenuRowViewHolder

答案 1 :(得分:0)

您正在设置容器的单击侦听器(itemView)。您需要在各个视图上设置单击侦听器:

Error   C2065   'aliasName_': undeclared identifier X\password.cpp  7
Error   C2511   'void Password::setAliasName(std::string)': overloaded member function not found in 'Password'  X\password.cpp  6
Error   C3646   'aliasName_': unknown override specifier    X\password.h    6
Error   C4430   missing type specifier - int assumed. Note: C++ does not support default-int    X\password.h    6
Error   C2061   syntax error: identifier 'string'   X\password.h    9
Error   C3646   'getAliasName': unknown override specifier  X\password.h    11
Error   C2059   syntax error: '('   X\password.h    11
Error   C2334   unexpected token(s) preceding '{'; skipping apparent function body  X\password.h    11

然后声明一个监听器:

this.add.setOnClickListener(mListener);
this.sub.setOnClickListener(mListener);
this.price.setOnClickListener(mListener);
this.quantity.setOnClickListener(mListener);