如何修复Android Studios中的listview动作侦听器?

时间:2017-09-20 06:20:45

标签: java android listview android-studio actionlistener

我想为listview设置一个动作监听器,以便根据所点击的位置显示具体的细节。但是,在使用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);

        }
    }); 

2 个答案:

答案 0 :(得分:0)

  

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

&#13;
&#13;
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);
        }
    });
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用此

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);

        }
    });