user3117785
user3117785

Reputation: 247

onItemClick listView off by one, and last item clicked causes crash?

It appears when I set up my onClickListener for my listview, and set the positioning every listview cell seems to be one off, and the last listview cell causes a crash. For example If I had a listview of 5 items, If I clicked first one, the second one details show up, if I click the second one, the third ones show up - The last item causes a crash, and obviously the first item details are never shown up since its off by one...not sure what the issue is here or how I would go about fixing this? Never ran into this issue...

private static void updateUI() {
        SimpleAdapter adapter = new SimpleAdapter(
                context, bookList,
                R.layout.list_view, new String[] { "bookName",
                        "bookDetails", "bookYear" },
                new int[] { R.id.bookName,
                        R.id.bookDetails,
                        R.id.bookYear });
        mBookList.setAdapter(adapter);
        mBookList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override

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


                    String externalURL = bookList.get(position).get("site_detail_url");

                    Intent intent = new Intent(context, BookWebViewActivity.class);
                    intent.setData(Uri.parse(externalURL));
                    context.startActivity(intent);


            }
        });

Upvotes: 1

Views: 971

Answers (2)

Submersed
Submersed

Reputation: 8870

I had this occur when adding a header view to my ListView. I solved the problem by using the ListView to get my items, rather than the adapter. See ListView.getItemAtPosition(int position) : http://developer.android.com/reference/android/widget/ListView.html.

Upvotes: 4

stefana
stefana

Reputation: 2626

use

String externalURL = bookList.get(position-1).get("site_detail_url");

Upvotes: 1

Related Questions