0 Replies - 1873 Views - Last Post: 01 January 2014 - 03:53 AM

#1 timbats  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-January 14

Listview onClickListener always sending 4

Posted 01 January 2014 - 03:53 AM

My application will respond to a user selection on a listview, and then pass the selected item to another activity. But, no matter what listview item is selected, this code will pass index=4.

Main.java:
setListAdapter(new MyAdapter(this,
        android.R.layout.simple_list_item_1, R.id.textView1, 
        getResources().getStringArray(R.array.titles)));

ListView lv = getListView();
   lv.setOnItemClickListener(new OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> adapter, View v, int position,
            long arg3) 
      {
            Intent i = new Intent(getApplicationContext(), Challengeview.class);
            i.putExtra("index", ""+position);
            startActivity(i);
      }
   }); 


And the 'Challengeview.class' that the intent is sent to

String value = getIntent().getStringExtra("index");

From here, the 'value' is used to refer to an array index. Logcat is giving the following error:

java.lang.ArrayIndexOutOfBoundsException: length=4; index=4



Titles Array, as stored in values:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="titles">
        <item>Challenge1</item>
        <item>Challenge2</item>
        <item>Challenge3</item> 
    </string-array>

</resources>



Is This A Good Question/Topic? 0
  • +

Page 1 of 1