{"id":1878,"date":"2022-08-30T15:20:12","date_gmt":"2022-08-30T15:20:12","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/indexoutofboundsexception-while-accessing-the-listview-through-adapter-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:20:12","modified_gmt":"2022-08-30T15:20:12","slug":"indexoutofboundsexception-while-accessing-the-listview-through-adapter-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/indexoutofboundsexception-while-accessing-the-listview-through-adapter-collection-of-common-programming-errors\/","title":{"rendered":"IndexOutOfBoundsException While Accessing the listview through Adapter-Collection of common programming errors"},"content":{"rendered":"<p>I have implemented my ListView and i am populating the list with custom adapter that extends the BaseAdapter and using the .NET based service , pulling and populating the values in the list. This works fine, but when i tried to continuously do some operations on the list(like deleting the entries, Refreshing, Editing..), I am getting the following error. This error doesn&#8217;t comes all the time. But comes 1 out of 15\/20 tries.<\/p>\n<p>Trying to resolve, Roaming around the web , to find out why doesn&#8217;t it occur.. Still Searching&#8230;<\/p>\n<pre><code>11-25 21:38:39.257: ERROR\/AndroidRuntime(2169): Uncaught handler: thread main\n                    exiting due to uncaught exception\n\n11-25 21:38:39.325: ERROR\/AndroidRuntime(2169): java.lang.IndexOutOfBoundsException:\n                    Invalid location 0, size is 0\n\n11-25 21:38:39.325: ERROR\/AndroidRuntime(2169):at \n                     java.util.ArrayList.get(ArrayList.java:341)\n<\/code><\/pre>\n<p>ListActivity class : STSBankDetailsListingScreenActivity.java<\/p>\n<pre><code> public class STSBankDetailsListingScreenActivity extends ListActivity implements Runnable {\n\n\/\/ All my Declarations are here....\n\n   ...........................\n.....................\n        ...................\n\nprivate STSAddClientBankDetailsListingScreenAdapter mBankDetailsAdapter = null;\n\n\n@Override\npublic void onCreate(Bundle savedInstanceState) {\n    Log.i(TAG, \"onCreate\");\n    super.onCreate(savedInstanceState);\n    .......................\n    ......................\n    \/\/ Set the activity content from the XML resource\n    setContentView(R.layout.XXX);\n\n    .....................\n    .....................\n\n    \/\/ Set the typeface with the specified font\n    ........................................................\n    .........................................................\n    \/\/ Initializing the progress dialog to show the loading status\n    mProgressDialog = ProgressDialog.show(this,\"Loading....\",\"Please wait\");\n\n\n    \/\/ Capture the widgets that are defined in the XML resource\n    mBack = (Button) findViewById(R.id.backButton);\n\n    .................................\n    ................................\n    \/\/ Setting the custom fonts &amp; Text\n    ................................\n    ...............................\n\n    \/\/ Create a Thread for long running operations\n    mThread = new Thread(STSBankDetailsListingScreenActivity.this);\n    mThread.start();\n\n    \/\/ Instantiate the listview adapter class\n    mBankDetailsAdapter = new  STSAddClientBankDetailsListingScreenAdapter(this);\n\n    \/\/ Adding the listners for the items in the listview\n    getListView().setOnItemClickListener(new OnItemClickListener() {\n\n    @Override\n    public void onItemClick(AdapterView parent, View view, int position, long id) {\n\n    mDetailsHolder = new ArrayList();\n    mDetailsHolder.add(STSURLConstants.AUTH_KEY);\n    mDetailsHolder.add(mBankICode.get(position).toString());\n    mDetailsHolder.add(mUserTypeHolder.get(position).toString());\n    mDetailsHolder.add(mAccountNameHolder.get(position).toString());\n    mDetailsHolder.add(mBankCodeHolder.get(position).toString());\n    mDetailsHolder.add(mBankNameHolder.get(position).toString());\n    mDetailsHolder.add(mBankAddressHolder.get(position).toString());\n    mDetailsHolder.add(mOpeningBalanceHolder.get(position).toString());\n    mDetailsHolder.add(mAmtInHolder.get(position).toString());\n\n    }\n\n    });\n}\n\n@Override\nprotected void onStart() {\n    Log.i(TAG, \"onStart\");\n    super.onStart();\n}\n\n@Override\nprotected void onRestart() {\n    Log.i(TAG, \"onRestart\");\n\/\/ Show the Progress Dialog \nmProgressDialog = ProgressDialog.show(STSBankDetailsListingScreenActivity.this,     \"Loading...\", \"Please wait\");\n\/\/ Create a Thread for long running operations\n    mThread = new Thread(STSBankDetailsListingScreenActivity.this);\n    mThread.start();\n    super.onRestart();\n}\n\n@Override\nprotected void onPause() {\n    Log.i(TAG, \"onPause\");\n    super.onPause();\n}\n\n@Override\nprotected void onResume() {\n    Log.i(TAG, \"onResume\");\n    \/\/updateUI();\n    super.onResume();\n}\n\n@Override\nprotected void onStop() {\n    Log.i(TAG, \"onStop\");\n    super.onStop();\n}\n\n@Override\nprotected void onDestroy() {\n    Log.i(TAG, \"onDestroy\");\n    if(mThread != null) {\n        Thread dummyThread = mThread;\n        mThread = null;\n           \/\/ Post an interrupt request to this thread\n        dummyThread.interrupt(); \n    }\n    super.onDestroy();\n}\n\n@Override\npublic void run() {\n    \/\/ TODO Auto-generated method stub\n    try {\n        \/\/ Clear the arraylist before adding up the entries\n        mBankNameHolder.clear(); \n        ..............................\n\n        \/\/ Instantiate the class \n        mWSConsumer = new STSWebServiceConsumer();\n        \/\/ Call to fetch the bank details from the service\n        mSTSSelectBankAccountList = mWSConsumer.SelectBankAccountDetails();\n\n\/\/ Do the necessary actions to pull the values from the list\nfor(STSSelectBankAccountDetails s : mSTSSelectBankAccountList) {\n\/\/ Populate the list....\nmBankNameHolder.add(String.valueOf(s.getmBaBankName().toString()));\n...............................\n...............................\n}\n} else { Log.i(TAG, \"Something went wrong !\"); }\n        } catch(Exception err) {\n        err.printStackTrace();\n    } \n    handler.sendEmptyMessage(0);  }\n\nprivate void updateUI() {\nLog.i(TAG, \"updateUI\");\nrunOnUiThread(new Runnable() {\n    @Override\n    public void run() {\n    try {\n    \/\/ Set the data behind the list view\n mBankDetailsAdapter = new  STSAddClientBankDetailsListingScreenAdapter(STSBankDetailsListingScreenActivity.this,           mBankNameHolder, mAccountNameHolder, mIsCheckedHolder);\n    getListView().setAdapter(mBankDetailsAdapter);\n    \/\/ Notifies the attached view that the underlying data has been changed\n    mBankDetailsAdapter.notifyDataSetChanged();\n    } catch(Exception err) {\n        err.printStackTrace();      }\n        }\n        });\n    }\n\npublic Handler handler = new Handler() {\n\n    @Override\n    public void handleMessage(Message msg) {\n\n        switch (msg.what) {\n        case 0:\n            \/\/ Do Nothing...\n            break;\n        case 1:\n            \/\/ Calls the run method of the runnable object\n            mThread.run();\n            break;\n        default:\n            break;\n        }\n        \/\/ Update the UI\n        updateUI();\n\n        \/\/ Dismiss the progress dialog once done\n        if(mProgressDialog.isShowing()) {\n            mProgressDialog.dismiss();\n            \/\/mQuickAction.dismiss();\n        }\n    }\n};\n\n}\n<\/code><\/pre>\n<p>Adapter class : STSAddClientBankDetailsListingScreenAdapter.java<\/p>\n<pre><code>public class STSAddClientBankDetailsListingScreenAdapter extends\n    BaseAdapter {\n\/\/ Declare all the needed\nprivate Activity mActivity = null;\nprivate List mBankNameHolder = new ArrayList();\nprivate List mAccountNameHolder = new ArrayList();\nprivate List mIsChecked = new ArrayList();\n\nprivate static final String TAG = STSAddClientBankDetailsListingScreenAdapter.class.getSimpleName();\nprivate static LayoutInflater mLayoutInflater = null; \/\/ This class is responsible for instantiating the layout XML file in to its corresponding view objects\n\npublic STSAddClientBankDetailsListingScreenAdapter(Context context) {\n    \/\/ Obtains the layout interface from the given context\n    mLayoutInflater = LayoutInflater.from(context);\n}\n\npublic STSAddClientBankDetailsListingScreenAdapter(Activity activity, List bankName, List accountName, List isChecked) {\n    mActivity = activity;\n    mBankNameHolder = bankName;\n    mAccountNameHolder = accountName;\n    mIsChecked = isChecked;\n}\n\/* (non-Javadoc)\n * @see android.widget.Adapter#getCount()\n *\/\n@Override\npublic int getCount() {\n    \/\/ TODO Auto-generated method stub\n    return mBankNameHolder.size();\n}\n\n\/* (non-Javadoc)\n * @see android.widget.Adapter#getItem(int)\n *\/\n@Override\npublic Object getItem(int position) {\n    \/\/ TODO Auto-generated method stub\n    return position;\n}\n\n\/* (non-Javadoc)\n * @see android.widget.Adapter#getItemId(int)\n *\/\n@Override\npublic long getItemId(int position) {\n    \/\/ TODO Auto-generated method stub\n    return position;\n}\n\n\/* (non-Javadoc)\n * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)\n *\/\n@Override\npublic View getView(int position, View convertView, ViewGroup parent) {\n    \/\/ TODO Auto-generated method stub\n    ViewHolder viewHolder;\n    if(convertView == null) {\n        convertView = mLayoutInflater.inflate(R.layout.profile_client_bank_details_listing_item, null);\n        viewHolder = new ViewHolder();\n        \/\/ Capture the widgets defined in the XML resource\n        viewHolder.mBankName = (TextView) convertView.findViewById(R.id.message01);\n        viewHolder.mAccountName = (TextView) convertView.findViewById(R.id.message02);\n        viewHolder.mChecked = (ImageView) convertView.findViewById(R.id.image01);\n        convertView.setTag(viewHolder); \/\/ Set the tag associated with this view\n     } else {\n         viewHolder = (ViewHolder) convertView.getTag(); \/\/ Returns the view tag\n         }\n    \/\/ Bind the controls with the resource\n    viewHolder.mBankName.setText(mBankNameHolder.get(position).toString());\n    viewHolder.mAccountName.setText(mAccountNameHolder.get(position).toString());\n    \/\/ Set the tick mark according to the values\n    boolean checked = Boolean.parseBoolean(mIsChecked.get(position).toString());\n    if(checked) {\n        viewHolder.mChecked.setVisibility(View.VISIBLE);\n    } else {\n        viewHolder.mChecked.setVisibility(View.INVISIBLE);\n    }\n    return convertView;\n}\n\nstatic class ViewHolder {\n    TextView mBankName, mAccountName;\n    ImageView mChecked;\n}\n\n}\n<\/code><\/pre>\n<p>Please if Any sort of help is highly appreciated.<\/p>\n<p>Thank you once again&#8230;<\/p>\n<ol>\n<li>\n<p>what I strongly feel is that your adapter method &#8211; <code>getCount<\/code>() gives wrong output in that particular case. I would suggest not to take size from <code>mBankNameHolder<\/code> take size from some other parameter or check this value with log\/debug in each case.<\/p>\n<pre><code>public int getCount() {\n    \/\/ TODO Auto-generated method stub\n    return mBankNameHolder.size();\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 21:10:39. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have implemented my ListView and i am populating the list with custom adapter that extends the BaseAdapter and using the .NET based service , pulling and populating the values in the list. This works fine, but when i tried to continuously do some operations on the list(like deleting the entries, Refreshing, Editing..), I am [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1878","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1878"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1878\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}