简体   繁体   English

Android Studio-单击列表视图中的项目,从该位置获取值

[英]Android Studio - Click item in listview, get a value from that position

I am trying be able to click on a item in a listview and get the stopIdRoutes (String) value and pass it to another intent. 我试图能够在列表视图中单击一个项目,并获取stopIdRoutes(字符串)值,并将其传递给另一个意图。

The values are put into a HashMap before being put into a list. 这些值先放入HashMap,再放入列表。

I have tried to use listview.getItemAtPosition(position); 我试图使用listview.getItemAtPosition(position); but this just throws an error: java.util.HashMap cannot be cast to ie.*.student.*.dublinbuses.getStopId . 但这只会引发错误: java.util.HashMap cannot be cast to ie.*.student.*.dublinbuses.getStopId

How can I remedy this. 我该如何补救。 I've tried some solutions on here, and they haven't helped with this situation. 我在这里尝试了一些解决方案,但这种情况并没有帮助。

I also created a getStopId class. 我还创建了一个getStopId类。

Also, the stopIdRoutes that is being sent to the next intent is the last stop id in the list. 另外,要发送到下一个意图的stopIdRoutes是列表中的最后一个停止ID。

My code is: 我的代码是:

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent i = new Intent(getApplicationContext(), StopIdResultsActivity.class);

            getStopId getstopid_fromclass = (getStopId)lv.getItemAtPosition(position);
            Log.i("Item", "Selected: " + getstopid_fromclass.getStopid());

            //Create the bundle
            Bundle bundle = new Bundle();

            //Add your data to bundle
            bundle.putString("stopid", stopidRoutes);

            //Add the bundle to the intent
            i.putExtras(bundle);

            //Fire that second activity
            startActivity(i);
        }

    });
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent i = new Intent(getApplicationContext(), StopIdResultsActivity.class);

            GetStopId getstopid_fromclass = list[position];


            //Create the bundle
            Bundle bundle = new Bundle();

            //Add your data to bundle
            bundle.putString("stopid", getstopid_fromclass.getStopid());

            //Add the bundle to the intent
            i.putExtras(bundle);

            //Fire that second activity
            startActivity(i);
        }

    })

and in other activity get it as:- 在其他活动中将其获取为:-

String stopID = getIntent().getExtras().getString("stopid");

Try using this, if still error appears, let me know the error 尝试使用此方法,如果仍然出现错误,请告诉我错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM