在 Android 中通过 getItemAtPosition 方法获取 id

Getting id by getItemAtPosition method in Android

我想在单击项目后从数据库中获取项目的 ID,然后我想将此 ID 发送给另一个 activity。

我收到一个错误:

java.lang.ClassCastException: java.lang.String cannot be cast to com.example.myPackage.Place

该错误由以下行引起:

Place mPlace = (Place)(lv.getItemAtPosition(position));

当然,如果我在下面的行中更改该行,应用程序正在运行,但我在屏幕上显示的是单行名称而不是 id。

String str = (String)(lv.getItemAtPosition(position));

这是我的完整代码。

public class VisitedPlaces extends ListActivity {

    private ArrayList<String> results = new ArrayList<String>();

    protected void onCreate(Bundle savedInstanceState) {
        .
        .
        .
        fillTheList();
        displayResultList();
    }

    public void fillTheList(){
        DataBaseHelper dbHelper = new DataBaseHelper(this.getApplicationContext());

        List<Place> placesList = new ArrayList<>();
        String sql = "SELECT _id, Name, Description, Latidute, Longtidute FROM Places WHERE Visited=1";
        placesList = dbHelper.getAllPlaces(sql);

        for (Place place : placesList){
            results.add(place.getPlaceName());
        }
    }

    public void displayResultList() {

        ListView lv = getListView();
        lv.addHeaderView(tvNew);
        lv.setTextFilterEnabled(true);
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, results));

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

//      String str = (String)(lv.getItemAtPosition(position));
        Place mPlace = (Place)(lv.getItemAtPosition(position)); //HERE IS A PROBLEM
        int mID = mPlace.getID();

        Toast.makeText(getApplicationContext(),"Click ListItem: " + mID + ", " + id + ", " , Toast.LENGTH_LONG).show();                              

        goToPlaceDescriptions(view, id);

            }
        });     
    }

    public void goToPlaceDescriptions(View v, long id) {
        Intent intent = new Intent(this, PlaceDescriptions.class);
        intent.putExtra("id", id);
        startActivity(intent);
    }
}

谁能帮帮我?

没有铸造,这样做:

设置placesList为全局变量,然后写这行得到正确的Place:

Place mPlace=placesList.get(position);

问题在于您使用了 ArrayAdapter。我认为将从 BaseAdapter 扩展的自定义适配器将是更好的解决方案。

请看下面的代码:

自定义适配器class:

        public class PlaceListAdapter extends BaseAdapter {

        private List<Place> mPlaceList;
        private Context mContext;

        PlaceListAdapter(List<Place> placeList, Context c){
            mPlaceList = new LinkedList<>(placeList);
            mContext = c;
        }
        @Override
        public int getCount() {
            return mPlaceList.size();
        }

        @Override
        public Place getItem(int i) {
            return mPlaceList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            //here inflate and return some view it can be text view
        }
    }

现在您的代码如下所示

      public class VisitedPlaces extends ListActivity {

        private List<Place> results = new ArrayList<Place>();

        protected void onCreate(Bundle savedInstanceState) {
            .
            .
            .
            fillTheList();
            displayResultList();
        }

        public void fillTheList(){
            DataBaseHelper dbHelper = new DataBaseHelper(this.getApplicationContext());
            String sql = "SELECT _id, Name, Description, Latidute, Longtidute FROM Places WHERE Visited=1";
            List<Place> placesList  = dbHelper.getAllPlaces(sql);

            for (Place place : placesList){
                results.add(place);
            }
        }

        public void displayResultList() {

            ListView lv = getListView();
            lv.addHeaderView(tvNew);
            lv.setTextFilterEnabled(true);
            setListAdapter(new PlaceListAdapter(results, this));

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position,
                                        long id) {
                    Place mPlace = (((PlaceListAdapter)parent.getAdapter()).getItem(position)); //HERE IS A PROBLEM
                    int mID = mPlace.getID();

                    Toast.makeText(getApplicationContext(),"Click ListItem: " + mID + ", " + id + ", " , Toast.LENGTH_LONG).show();

                    goToPlaceDescriptions(view, id);

                }
            });
        }

        public void goToPlaceDescriptions(View v, long id) {
            Intent intent = new Intent(this, PlaceDescriptions.class);
            intent.putExtra("id", id);
            startActivity(intent);
        }

    }

我可以在代码中犯一些错误,但想法是最重要的。 请阅读自定义列表适配器。