繁体   English   中英

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

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

我试图能够在列表视图中单击一个项目,并获取stopIdRoutes(字符串)值,并将其传递给另一个意图。

这些值先放入HashMap,再放入列表。

我试图使用listview.getItemAtPosition(position); 但这只会引发错误: java.util.HashMap cannot be cast to ie.*.student.*.dublinbuses.getStopId

我该如何补救。 我在这里尝试了一些解决方案,但这种情况并没有帮助。

我还创建了一个getStopId类。

另外,要发送到下一个意图的stopIdRoutes是列表中的最后一个停止ID。

我的代码是:

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

    })

在其他活动中将其获取为:-

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

尝试使用此方法,如果仍然出现错误,请告诉我错误

暂无
暂无

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

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