【问题标题】:How to fix my listview Action Listener in Android Studios?如何在 Android Studios 中修复我的 listview Action Listener?
【发布时间】:2018-03-01 01:51:22
【问题描述】:

我想为我的列表视图设置一个动作监听器,以便根据单击的位置显示特定的详细信息。但是,我在使用 getPosition() 和 getName() 方法时遇到了错误。请帮助我纠正这种情况,并解释它是如何运作的,因为我很困惑。

    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?>adapter, View v, int position, long id) {

            String item = yourData.getPosition(position).getName(NAMES);

            Intent intentProduct = new Intent(Scrollable.this, ProductDetail.class);

            intentProduct.putExtra("Key", item);

            startActivity(intentProduct);

        }
    }); 

【问题讨论】:

    标签: java android listview android-studio actionlistener


    【解决方案1】:

    使用这个

    String item = yourData.getPosition(position).getName();
    

    插入

    String item = yourData.getPosition(position).getName(NAMES);
    

    代码

    listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?>adapter, View v, int position, long id) {
    
                String item = yourData.getPosition(position).getName();
    
                Intent intentProduct = new Intent(Scrollable.this, ProductDetail.class);
    
                intentProduct.putExtra("Key", item);
    
                startActivity(intentProduct);
    
            }
        });
    

    【讨论】:

      【解决方案2】:

      String item = yourData.getPosition(position).getName(NAMES); //名字不重要

      listView.setOnItemClickListener(new OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?>adapter, View v, int position, long id) {
                  String item = yourData.getPosition(position).getName();
                  Intent intentProduct = new Intent(Scrollable.this, ProductDetail.class);
                  intentProduct.putExtra("Key", item);
                 startActivity(intentProduct);
              }
          });

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-17
        • 2016-06-25
        • 1970-01-01
        • 2023-01-30
        • 2020-02-21
        相关资源
        最近更新 更多