簡體   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