从listview中获取所选对象

时间:2014-01-12 15:23:30

标签: java android listview android-listview

我会找到一种方法来获取我的列表视图中的选定项目然后投射我的对象类型,但我得到一个错误,我认为很大,看看什么是错的。你能救我吗?

我的代码:

mListMenu = (ListView) findViewById(R.id.listView_tracks);
        mListMenu.setAdapter(new TracksListAdapter(this, TrackManager.getAllTrackFromTel(new DataBaseHelper(this))));
        mListMenu.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapter, View arg1,
                    int position, long arg3) {
                    Track selectedItem = (Track) adapter.getAdapter().getItem(position);
                    Intent intent = new Intent();
                     Bundle bundle = new Bundle();
                     bundle.putLong("trackselected",selectedItem.getTrackid());
                    intent.putExtras(bundle);                   
                   //Envoi du resultat à l'origine
                    setResult(RESULT_OK, intent);
                    finish();


            }

        });

我收到此错误:

 FATAL EXCEPTION: main
 java.lang.ClassCastException: java.lang.Integer
    at com.milesbox.sport.tracker.ListTracksActivity$1.onItemClick(ListTracksActivity.java:44)
    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
    at android.widget.ListView.performItemClick(ListView.java:3746)
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1980)
    at android.os.Handler.handleCallback(Handler.java:587)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3691)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

谢谢你FD_,当我说它太大而无法看到什么问题时,这是我的方法(getItem)返回一个错误的项目(复制粘贴太快)。