如何在android中的listview中的特定项添加一些监听器?

时间:2016-08-10 04:34:21

标签: android listview

我有一个listview,其中我有4个项目,我有一个类,其中包含一些listner实现我想要的东西,当我点击项目在poition 1我想添加该监听器类instance.how我能做到吗 代码: -

m_OfferList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            switch (position){
                case 1:

                    MonetizationManager.createSession(getActivity(), "91707", this);


                    MonetizationManager.setRewardListener(getActivity());
            }
        }
    });

这里我想在特定的索引中设置监听器

4 个答案:

答案 0 :(得分:0)

case 0:将成为您case 1:

逻辑的第一个列表
m_OfferList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            switch (position){
                case 0:

                  // First item Clicked
                    MonetizationManager.createSession(getActivity(), "91707", this);
                    etizationManager.setRewardListener(getActivity());
                 break;
                case 1:
                  // 2nd item Clicked
                    MonetizationManager.createSession(getActivity(), "91707", this);
                    etizationManager.setRewardListener(getActivity());
                 break;
                 case 2:
                    // 3rd item Clicked
                    MonetizationManager.createSession(getActivity(), "91707", this);
                    etizationManager.setRewardListener(getActivity());
                 break;
                  case 3:
                    // 4th item Clicked
                    MonetizationManager.createSession(getActivity(), "91707", this);
                    etizationManager.setRewardListener(getActivity());
                 break;
            }
        }
    });

答案 1 :(得分:0)

试试这个

1 First Way

m_OfferList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            switch (position){
                case 1:
                  // 2nd item Clicked
                    MonetizationManager.createSession(getActivity(), "91707", this);
                    etizationManager.setRewardListener(getActivity());
                 break;
            }
        }
    });

2 Second Way

if(position==1)
{
m_OfferList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                  // 2nd item Clicked
                    MonetizationManager.createSession(getActivity(), "91707", this);
                    etizationManager.setRewardListener(getActivity());

        }
    });

}

答案 2 :(得分:0)

如果要将侦听器添加到位置1中的视图,则可以使用

[unowned self]

答案 3 :(得分:0)

请使用此示例我有完美的作品

 //first declare interface in adapter class on

        customButtonListener customListner;

        public interface customButtonListener {
            public void onButtonClick(String song_id);
        }

        public void setCustomButtonListner(customButtonListener listener) {
            this.customListner = listener;
        }

    //and use any view or button click

    button.customListner.onButtonClick(int position)

    //Adapter where use 

    adapter.setCustomButtonListner(this);


//Implement this method in activity

    @Override
    public void onButtonClick(int position)
    {

    }

我希望能帮助你